Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • $\begingroup$ Excellent to see the comparative benchmark. +1. . I know my solution is too slow to compare with such long lists. $\endgroup$ Commented May 22, 2022 at 10:50
  • 1
    $\begingroup$ @Syed Of course, you can. My code is just an extension of the code from the answer by Chris Degnen. $\endgroup$ Commented May 22, 2022 at 23:28
  • 1
    $\begingroup$ @Syed I've added two more solutions which are even faster. $\endgroup$ Commented May 23, 2022 at 8:59
  • 2
    $\begingroup$ You write: If[(acc += v)... in the fifth version, and it works, but where did you define v. I am not familiar with this usage. Could you please say a few words about it. $\endgroup$ Commented May 23, 2022 at 9:37
  • 2
    $\begingroup$ @Syed v is a local variable of Do, it is defined in its second argument: Do[<...>, {v, arr}]. Try: Do[Print@v, {v, {1, 2, 3}}]. This syntax is document under "Generalizations&Extensions" on the Docs page for Do. $\endgroup$ Commented May 23, 2022 at 9:39