Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Raku] Range sum method

1 view
Skip to first unread message

Kang-min Liu

unread,
May 28, 2020, 10:27:37 AM5/28/20
to

https://github.com/rakudo/rakudo/blob/master/src/core.c/Range.pm6#L630

昨天正好意識到,以 `+` 算符去將一串數字 1..$n 加總起來時:

my $sum = [+] 1..$n;

其實不必做 n-1 次加法,而是有個公式解: n(n+1)/2

稍後看了一下 Rakudo 源碼,發現此公式解已經實做在其中了。

😎 Cool


0 new messages