Skip to content

Language Shootout

A user-supported site Fastest, Shortest, Simplest

Benchmarks

Partial Sums

Background

MathWorld: "General Series".

How to implement

We ask that contributed programs not only give the correct result, but also use the same algorithm to calculate that result.

Each program should use the same naïve iterative double-precision algorithms to calculate partial sums of these series, each over its first N terms:

  • ∑ (2/3)^k, for k = 0, 1, … — use the power function
  • ∑ k^−0.5, for k = 1, 2, … — use the power or sqrt function
  • ∑ 1/(k(k+1))
  • ∑ 1/(k³ sin²k) — Flint Hills
  • ∑ 1/(k³ cos²k) — Cookson Hills
  • ∑ 1/k — Harmonic
  • ∑ 1/k² — Riemann Zeta
  • ∑ (−1)^(k+1)/k — Alternating Harmonic
  • ∑ (−1)^(k+1)/(2k−1) — Gregory

Programs may use a single loop or several loops, and may cache recomputed values in local variables.

Each sum is printed to nine decimal places, followed by a tab and the name of its series.

Verification: Use diff to compare program output N=25000 with the reference output.

Use a larger command line argument (2500000) to check program performance.

Times are wall-clock milliseconds, with this implementation’s hello-world startup time subtracted. gz is the source in bytes with comments removed and gzipped. style is the idiomatic-code score. Click a heading to sort.

No programs have been measured for this benchmark yet. Submit one.