1到40亿求和需要多少存储 [英] How much storage for summation of 1 to 4 billion

查看:38
本文介绍了1到40亿求和需要多少存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

受到这个问题的启发(在给定的 40 亿之外找到一个整数).

Inspired by this question (Find an integer not among four billion given ones).

存储一个整数(1 到 40 亿的总和)需要多少存储空间?

How much storage space would it require to store an integer that was the summation of the numbers 1 to 4 billion?

例如,1+2+3+4+5 = 15.1 到 100 万的总和 = 500,000,500,000.

For example, 1+2+3+4+5 = 15. Summation of 1 to 1 million = 500,000,500,000.

这里是一个可能有帮助的算法

Here is an algorithm that may help

推荐答案

您链接的函数来描述如何找到第 n 个 三角数,定义为1到n的n个自然数之和.

The function you link to describe how to find the n'th Triangular Number, which is defined as the sum of the n natural numbers from 1 to n.

将 40 亿作为 n 代入函数得到 8000000002000000000.

Substituting 4 billion as n into the function gives 8000000002000000000.

表示可以通过取值的基数为 2 的对数并四舍五入来计算出位数 -

Expressing that as a number of bits can be worked out by taking the base-2 logarithm of the value and rounding up -

ceil(log(8000000002000000000)/log(2)) = 63

ceil(log(8000000002000000000)/log(2)) = 63

因此,需要 63 位的存储空间.

So, 63 bits of storage are required.

这篇关于1到40亿求和需要多少存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆