将 N 以内的数字相加的最快算法

发布于 2024-08-28 18:18:18 字数 115 浏览 4 评论 0原文

我想要一个真正快速的 C 算法或代码来完成以下任务:对于任何给定的整数 N,对从 1 到 N 的所有数字求和,而不假设 N 为正数。我做了一个从 1 到 N 求和的循环,但是太慢了。

I want a really fast algorithm or code in C to do the following task: sum all numbers from 1 to N for any given integer N, without assuming N is positive. I made a loop summing from 1 to N, but it is too slow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

柠栀 2024-09-04 18:18:18

如果N为正数:int sum = N*(N+1)/2;

如果N为负数:int tempN = -N; int sum = 1 + tempN*(tempN+1)/2 * (-1);

If N is positive: int sum = N*(N+1)/2;

If N is negative: int tempN = -N; int sum = 1 + tempN*(tempN+1)/2 * (-1);.

梦幻的味道 2024-09-04 18:18:18
sum = N * (N + 1) / 2
sum = N * (N + 1) / 2
明天过后 2024-09-04 18:18:18

您正在寻找的公式是在您的问题的多个答案中发布的公式的更通用形式,这是一个 算术级数/级数差异因子为1。来自维基百科,如下:

alt text

只要 m 始终小于 n,上述公式就会处理负数。例如,要获得从 1 到 -2 的总和,请将 m 设置为 -2,将 n 设置为 1,即从 -2 到 1 的总和。这样做会导致:

(1 - -2 + 1) * (1 + -2) / 2 = 4 * -1 / 2 = -4 / 2 = -2.

这是预期的结果。

The formula you're looking for is a more general form of the one posted in multiple answers to your question, which is an Arithmetic Series/Progression with a difference factor of 1. From Wikipedia, it is the following:

alt text

The above formula will handle negative numbers as long as m is always less than n. For example to get the sum from 1 to -2, set m to -2 and n to 1, i.e. the sum from -2 to 1. Doing so results in:

(1 - -2 + 1) * (1 + -2) / 2 = 4 * -1 / 2 = -4 / 2 = -2.

which is the expected result.

瑾兮 2024-09-04 18:18:18

为了完成上述答案,这就是证明公式的方法(正整数的示例,但原理与负数或 Void 指出的任何算术套件相同)。

只需将套件写两次,如下所示并添加数字:

  1+   2+   3+ ... n-2+ n-1+   n   = sum(1..n)     : n terms from 1 to n
+ n+ n-1+ n-2+ ...   3+   2+   1   = sum(n..1)     : the same n terms in reverse order
--------------------------------
n+1+ n+1+ n+1+ ... n+1+ n+1+ n+1   = 2 * sum(1..n) : n times n+1

n * (n+1) / 2 = sum(1..n)

Just to complete the above answers, this is how you prove the formula (sample for positive integer but principle is the same for negatives or any arithmetic suite as Void pointed out).

Just write the suite two times as below and add numbers:

  1+   2+   3+ ... n-2+ n-1+   n   = sum(1..n)     : n terms from 1 to n
+ n+ n-1+ n-2+ ...   3+   2+   1   = sum(n..1)     : the same n terms in reverse order
--------------------------------
n+1+ n+1+ n+1+ ... n+1+ n+1+ n+1   = 2 * sum(1..n) : n times n+1

n * (n+1) / 2 = sum(1..n)
梦年海沫深 2024-09-04 18:18:18

为了处理整数溢出,我将使用以下函数:

sum = (N%2) ? ( ((N+1)/2)*N ) : ( (N/2)*(N+1) );

To deal with integer overflow I'd use the following function:

sum = (N%2) ? ( ((N+1)/2)*N ) : ( (N/2)*(N+1) );
⒈起吃苦の倖褔 2024-09-04 18:18:18

试试这个...

其中 n 是您需要求和的最大整数。

总和为 (n*(N+1))/2

Try this...

Where n is the maximum integer you need to sum to.

The sum is (n*(N+1))/2

回梦 2024-09-04 18:18:18

int sum(int n) {
返回 (n < 0 ? n *(-n + 1) / 2 + 1 : n * ( n + 1) / 2);
}

int sum(int n) {
return (n < 0 ? n *(-n + 1) / 2 + 1 : n * ( n + 1) / 2);
}

情仇皆在手 2024-09-04 18:18:18

你听说过顺序和顺序吗?系列 ?你想要的“快速”代码是从 1 到 N 的算术级数之和..谷歌它..事实上打开你的数学书..

have you heard about sequence & series ? The 'fast' code that you want is that of sum of arithmetic series from 1 to N .. google it .. infact open your mathematics book..

苦妄 2024-09-04 18:18:18

如果|n|足够小,查找表将是最快的。

或者使用缓存,首先查找缓存,如果找不到记录则使用n * (n + 1) / 2(如果n为正数)计算总和,并将结果记录到缓存中。

if |n| is small enough, a lookup table will be the fastest one.

or using a cache, first search the cache, if can't find the record then caculate the sum by using n * (n + 1) / 2(if n is positive), and record the result into the cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文