Sum of a series

From Rosetta Code
Task
Sum of a series
You are encouraged to solve this task according to the task description, using any language you may know.
Compute the n-th term of a series, i.e. the sum of the n first terms of the corresponding sequence. Informally this value, or its limit when n tends to infinity, is also called the sum of the series, thus the title of this task.

For this task, use:

and compute .

This approximates the zeta function for s=2, whose exact value

is the solution of the Basel problem.

ACL2[edit]

(defun sum-x^-2 (max-x)
(if (zp max-x)
0
(+ (/ (* max-x max-x))
(sum-x^-2 (1- max-x)))))

ActionScript[edit]

function partialSum(n:uint):Number
{
var sum:Number = 0;
for(var i:uint = 1; i <= n; i++)
sum += 1/(i*i);
return sum;
}
trace(partialSum(1000));

Ada[edit]

with Ada.Text_Io; use Ada.Text_Io;
 
procedure Sum_Series is
function F(X : Long_Float) return Long_Float is
begin
return 1.0 / X**2;
end F;
package Lf_Io is new Ada.Text_Io.Float_Io(Long_Float);
use Lf_Io;
Sum : Long_Float := 0.0;
subtype Param_Range is Integer range 1..1000;
begin
for I in Param_Range loop
Sum := Sum + F(Long_Float(I));
end loop;
Put("Sum of F(x) from" & Integer'Image(Param_Range'First) &
" to" & Integer'Image(Param_Range'Last) & " is ");
Put(Item => Sum, Aft => 10, Exp => 0);
New_Line;
end Sum_Series;

Aime[edit]

real
Invsqr(real n)
{
return 1 / (n * n);
}
 
integer
main(void)
{
integer i;
real sum;
 
sum = 0;
 
i = 1;
while (i < 1000) {
sum += Invsqr(i);
i += 1;
}
 
o_real(14, sum);
o_byte('\n');
 
return 0;
}

ALGOL 68[edit]

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d
MODE RANGE = STRUCT(INT lwb, upb);
 
PROC sum = (PROC (INT)LONG REAL f, RANGE range)LONG REAL:(
LONG REAL sum := LENG 0.0;
FOR i FROM lwb OF range TO upb OF range DO
sum := sum + f(i)
OD;
sum
);
 
test:(
RANGE range = (1,100);
PROC f = (INT x)LONG REAL: LENG REAL(1) / LENG REAL(x)**2;
print(("Sum of f(x) from", lwb OF range, " to ",upb OF range," is ", SHORTEN sum(f,range),".", new line))
)

Output:

Sum of f(x) from         +1 to        +100 is +1.63498390018489e  +0.

APL[edit]

      +/÷2*⍨⍳1000
1.64393

AutoHotkey[edit]

AutoHotkey allows the precision of floating point numbers generated by math operations to be adjusted via the SetFormat command. The default is 6 decimal places.

SetFormat, FloatFast, 0.15
While A_Index <= 1000
sum += 1/A_Index**2
MsgBox,% sum ;1.643934566681554

AWK[edit]

$ awk 'BEGIN{for(i=1;i<=1000;i++)s+=1/(i*i);print s}'
1.64393

BASIC[edit]

Works with: QuickBasic version 4.5
FUNCTION s(x%)
s = 1 / x ^ 2
END FUNCTION
 
FUNCTION sum(low%, high%)
ret = 0
FOR i = low TO high
ret = ret + s(i)
NEXT i
sum = ret
END FUNCTION
PRINT sum(1, 1000)


BBC BASIC[edit]

      FOR i% = 1 TO 1000
sum += 1/i%^2
NEXT
PRINT sum

bc[edit]

define f(x) {
return(1 / (x * x))
}
 
define s(n) {
auto i, s
 
for (i = 1; i <= n; i++) {
s += f(i)
}
 
return(s)
}
 
scale = 20
s(1000)
Output:
1.64393456668155979824

Befunge[edit]

Emulates fixed point arithmetic with a 32 bit integer so the result is not very accurate.

05558***>::"~"%00p"~"/10p"( }}2"*v
v*8555$_^#!:-1+*"~"g01g00+/*:\***<
<@$_,#!>#:<+*<v+*86%+55:p00<6\0/**
"."\55+%68^>\55+/00g1-:#^_$
Output:
1.643934

Bracmat[edit]

( 0:?i
& 0:?S
& whl'(1+!i:~>1000:?i&!i^-2+!S:?S)
& out$!S
& out$(flt$(!S,10))
);

Output:

8354593848314...../5082072010432.....  (1732 digits and a slash)
1,6439345667*10E0

Brat[edit]

p 1.to(1000).reduce 0 { sum, x | sum + 1.0 / x ^ 2 }  #Prints 1.6439345666816

C[edit]

#include <stdio.h>
 
double Invsqr(double n)
{
return 1 / (n*n);
}
 
int main (int argc, char *argv[])
{
int i, start = 1, end = 1000;
double sum = 0.0;
 
for( i = start; i <= end; i++)
sum += Invsqr((double)i);
 
printf("%16.14f\n", sum);
 
return 0;
}

C++[edit]

#include <iostream>
 
double f(double x);
 
int main()
{
unsigned int start = 1;
unsigned int end = 1000;
double sum = 0;
 
for( unsigned int x = start; x <= end; ++x )
{
sum += f(x);
}
std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
return 0;
}
 
 
double f(double x)
{
return ( 1.0 / ( x * x ) );
}

C#[edit]

class Program
{
static void Main(string[] args)
{
// Create and fill a list of number 1 to 1000
 
List<double> myList = new List<double>();
for (double i = 1; i < 1001; i++)
{
myList.Add(i);
}
// Calculate the sum of 1/x^2
 
var sum = myList.Sum(x => 1/(x*x));
 
Console.WriteLine(sum);
Console.ReadLine();
}
}

An alternative approach using Enumerable.Range() to generate the numbers.

class Program
{
static void Main(string[] args)
{
double sum = Enumerable.Range(1, 1000).Sum(x => 1.0 / (x * x));
 
Console.WriteLine(sum);
Console.ReadLine();
}
}

CLIPS[edit]

(deffunction S (?x) (/ 1 (* ?x ?x)))
(deffunction partial-sum-S
(?start ?stop)
(bind ?sum 0)
(loop-for-count (?i ?start ?stop) do
(bind ?sum (+ ?sum (S ?i)))
)
(return ?sum)
)

Usage:

CLIPS> (partial-sum-S 1 1000)
1.64393456668156

Clojure[edit]

(reduce + (map #(/ 1.0 % %) (range 1 1001)))

COBOL[edit]

       IDENTIFICATION DIVISION.
PROGRAM-ID. sum-of-series.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
78 N VALUE 1000.
 
01 series-term USAGE FLOAT-LONG.
01 i PIC 9(4).
 
PROCEDURE DIVISION.
PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
COMPUTE series-term = series-term + (1 / i ** 2)
END-PERFORM
 
DISPLAY series-term
 
GOBACK
.
Output:
1.643933784000000120

CoffeeScript[edit]

 
console.log [1..1000].reduce((acc, x) -> acc + (1.0 / (x*x)))
 

Common Lisp[edit]

(loop for x from 1 to 1000 summing (expt x -2))

D[edit]

More Procedural Style[edit]

import std.stdio, std.traits;
 
ReturnType!TF series(TF)(TF func, int end, int start=1)
pure nothrow @safe @nogc {
typeof(return) sum = 0;
foreach (immutable i; start .. end + 1)
sum += func(i);
return sum;
}
 
void main() {
writeln("Sum: ", series((in int n) => 1.0L / (n ^^ 2), 1_000));
}
Output:
Sum: 1.64393

More functional Style[edit]

Same output.

import std.stdio, std.algorithm, std.range;
 
enum series(alias F) = (in int end, in int start=1)
pure nothrow @nogc => iota(start, end + 1).map!F.sum;
 
void main() {
writeln("Sum: ", series!q{1.0L / (a ^^ 2)}(1_000));
}

Delphi[edit]

 
unit Form_SumOfASeries_Unit;
 
interface
 
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
 
type
TFormSumOfASeries = class(TForm)
M_Log: TMemo;
B_Calc: TButton;
procedure B_CalcClick(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
 
var
FormSumOfASeries: TFormSumOfASeries;
 
implementation
 
{$R *.dfm}
 
function Sum_Of_A_Series(_from,_to:int64):extended;
begin
result:=0;
while _from<=_to do
begin
result:=result+1.0/(_from*_from);
inc(_from);
end;
end;
 
procedure TFormSumOfASeries.B_CalcClick(Sender: TObject);
begin
try
M_Log.Lines.Add(FloatToStr(Sum_Of_A_Series(1, 1000)));
except
M_Log.Lines.Add('Error');
end;
end;
 
end.
 
 
Output:
1.64393456668156

DWScript[edit]

 
var s : Float;
for var i := 1 to 1000 do
s += 1 / Sqr(i);
 
PrintLn(s);
 

E[edit]

pragma.enable("accumulator")
accum 0 for x in 1..1000 { _ + 1 / x ** 2 }

EchoLisp[edit]

 
(lib 'math) ;; for (sigma f(n) nfrom nto) function
(Σ (λ(n) (// (* n n))) 1 1000)
;; or
(sigma (lambda(n) (// (* n n))) 1 1000)
1.6439345666815615
 
(// (* PI PI) 6)
1.6449340668482264
 

Eiffel[edit]

 
note
description: "Compute the n-th term of a series"
 
class
SUM_OF_SERIES_EXAMPLE
 
inherit
MATH_CONST
 
create
make
 
feature -- Initialization
 
make
local
approximated, known: REAL_64
do
known := Pi^2 / 6
 
approximated := sum_until (agent g, 1001)
print ("%Nzeta function exact value: %N")
print (known)
print ("%Nzeta function approximated value: %N")
print (approximated)
end
 
feature -- Access
 
g (k: INTEGER): REAL_64
-- 'k'-th term of the serie
require
k_positive: k > 0
do
Result := 1 / (k * k)
end
 
sum_until (s: FUNCTION [ANY, TUPLE [INTEGER], REAL_64]; n: INTEGER): REAL_64
-- sum of the 'n' first terms of 's'
require
n_positive: n > 0
one_parameter: s.open_count = 1
do
Result := 0
across 1 |..| n as it loop
Result := Result + s.item ([it.item])
end
end
 
end
 
 

Elixir[edit]

iex(1)> Enum.reduce(1..1000, 0, fn x,sum -> sum + 1/(x*x) end)
1.6439345666815615

Emacs Lisp[edit]

 
(defun serie (n)
(if (< 0 n)
(apply '+ (mapcar (lambda (k) (/ 1.0 (* k k) )) (number-sequence 1 n) ))
(error "input error") ))
 
(insert (format "%.10f" (serie 1000) ))
 

Output:

1.6439345667

Erlang[edit]

lists:sum([1/math:pow(X,2) || X <- lists:seq(1,1000)]).

Euphoria[edit]

Works with: Euphoria version 4.0.0

This is based on the BASIC example.

 
function s( atom x )
return 1 / power( x, 2 )
end function
 
function sum( atom low, atom high )
atom ret = 0.0
for i = low to high do
ret = ret + s( i )
end for
return ret
end function
 
printf( 1, "%.15f\n", sum( 1, 1000 ) )

Ezhil[edit]

 
## இந்த நிரல் தொடர் கூட்டல் (Sum Of Series) என்ற வகையைச் சேர்ந்தது
 
## இந்த நிரல் ஒன்று முதல் தரப்பட்ட எண் வரை 1/(எண் * எண்) எனக் கணக்கிட்டுக் கூட்டி விடை தரும்
 
நிரல்பாகம் தொடர்க்கூட்டல்(எண்1)
 
எண்2 = 0
 
@(எண்3 = 1, எண்3 <= எண்1, எண்3 = எண்3 + 1) ஆக
 
## ஒவ்வோர் எண்ணின் வர்க்கத்தைக் கணக்கிட்டு, ஒன்றை அதனால் வகுத்துக் கூட்டுகிறோம்
 
எண்2 = எண்2 + (1 / (எண்3 * எண்3))
 
முடி
 
பின்கொடு (எண்2)
 
முடி
 
அ = int(உள்ளீடு("ஓர் எண்ணைச் சொல்லுங்கள்: "))
 
பதிப்பி "நீங்கள் தந்த எண் " அ
பதிப்பி "அதன் தொடர்க் கூட்டல் " தொடர்க்கூட்டல்(அ)
 
 

Factor[edit]

1000 [1,b] [ >float sq recip ] map-sum

Fantom[edit]

Within 'fansh':

 
fansh> (1..1000).toList.reduce(0.0f) |Obj a, Int v -> Obj| { (Float)a + (1.0f/(v*v)) }
1.6439345666815615
 

Forth[edit]

: sum ( fn start count -- fsum )
0e
bounds do
i s>d d>f dup execute f+
loop drop ;
 
:noname ( x -- 1/x^2 ) fdup f* 1/f ; ( xt )
1 1000 sum f. \ 1.64393456668156
pi pi f* 6e f/ f. \ 1.64493406684823

Fortran[edit]

In ISO Fortran 90 and later, use SUM intrinsic:

real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result
 
result = sum(a);

F#[edit]

The following function will do the task specified.

let rec f (x : float) = 
match x with
| 0. -> x
| x -> (1. / (x * x)) + f (x - 1.)

In the interactive F# console, using the above gives:

> f 1000. ;;
val it : float = 1.643934567

However this recursive function will run out of stack space eventually (try 100000). A tail-recursive implementation will not consume stack space and can therefore handle much larger ranges. Here is such a version:

#light
let sum_series (max : float) =
let rec f (a:float, x : float) =
match x with
| 0. -> a
| x -> f ((1. / (x * x) + a), x - 1.)
f (0., max)
 
[<EntryPoint>]
let main args =
let (b, max) = System.Double.TryParse(args.[0])
printfn "%A" (sum_series max)
0

This block can be compiled using fsc --target exe filename.fs or used interactively without the main function.

GAP[edit]

# We will compute the sum exactly
 
# Computing an approximation of a rationnal (giving a string)
# Value is truncated toward zero
Approx := function(x, d)
local neg, a, b, n, m, s;
if x < 0 then
x := -x;
neg := true;
else
neg := false;
fi;
a := NumeratorRat(x);
b := DenominatorRat(x);
n := QuoInt(a, b);
a := RemInt(a, b);
m := 10^d;
s := "";
if neg then
Append(s, "-");
fi;
Append(s, String(n));
n := Size(s) + 1;
Append(s, String(m + QuoInt(a*m, b)));
s[n] := '.';
return s;
end;
 
a := Sum([1 .. 1000], n -> 1/n^2);;
Approx(a, 10);
"1.6439345666"
# and pi^2/6 is 1.6449340668, truncated to ten digits

GEORGE[edit]

 
0 (s)
1, 1000 rep (i)
s 1 i dup × / + (s) ;
]
P
 

Output:-

 1.643934566681561

Go[edit]

package main
 
import ("fmt"; "math")
 
func main() {
fmt.Println("known: ", math.Pi*math.Pi/6)
sum := 0.
for i := 1e3; i > 0; i-- {
sum += 1 / (i * i)
}
fmt.Println("computed:", sum)
}

Output:

known:    1.6449340668482264
computed: 1.6439345666815597

Groovy[edit]

Start with smallest terms first to minimize rounding error:

println ((1000..1).collect { x -> 1/(x*x) }.sum())

Output:

1.6439345654

Haskell[edit]

With a list comprehension:

sum [1 / x ^ 2 | x <- [1..1000]]

With higher-order functions:

sum $ map (\x -> 1 / x ^ 2) [1..1000]

In point-free style:

(sum . map (1/) . map (^2)) [1..1000]

HicEst[edit]

REAL :: a(1000)
a = 1 / $^2
WRITE(ClipBoard, Format='F17.15') SUM(a)
1.643934566681561

Icon and Unicon[edit]

procedure main()
local i, sum
sum := 0 & i := 0
every sum +:= 1.0/((| i +:= 1 ) ^ 2) \1000
write(sum)
end

or

procedure main()
every (sum := 0) +:= 1.0/((1 to 1000)^2)
write(sum)
end

Note: The terse version requires some explanation. Icon expressions all return values or references if they succeed. As a result, it is possible to have expressions like these below:

 
x := y := 0 # := is right associative so, y is assigned 0, then x
1 < x < 99 # comparison operators are left associative so, 1 < x returns x (if it is greater than 1), then x < 99 returns 99 if the comparison succeeds
(sum := 0) # returns a reference to sum which can in turn be used with augmented assignment +:=
 

IDL[edit]

print,total( 1/(1+findgen(1000))^2)

J[edit]

   NB. sum of reciprocals of squares of first thousand positive integers
+/ % *: >: i. 1000
1.64393
 
(*:o.1)%6 NB. pi squared over six, for comparison
1.64493
 
1r6p2 NB. As a constant (J has a rich constant notation)
1.64493

Java[edit]

public class Sum{
public static double f(double x){
return 1/(x*x);
}
 
public static void main(String[] args){
double start = 1;
double end = 1000;
double sum = 0;
 
for(double x = start;x <= end;x++) sum += f(x);
 
System.out.println("Sum of f(x) from " + start + " to " + end +" is " + sum);
}
}

JavaScript[edit]

function sum(a,b,fn) {
var s = 0;
for ( ; a <= b; a++) s += fn(a);
return s;
}
 
sum(1,1000, function(x) { return 1/(x*x) } ) // 1.64393456668156

or, in a functional idiom:

(function () {
 
function sum(fn, lstRange) {
return lstRange.reduce(
function (lngSum, x) {
return lngSum + fn(x);
}, 0
);
}
 
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
 
 
return sum(
function (x) {
return 1 / (x * x);
},
range(1, 1000)
);
 
})();
Output:
1.6439345666815615

jq[edit]

The jq idiom for efficient computation of this kind of sum is to use "reduce", either directly or using a summation wrapper function.

Directly:

def s(n): reduce range(1; n+1) as $k (0; . + 1/($k * $k) );
 
s(1000)
 
Output:
1.6439345666815615

Using a generic summation wrapper function allows problems specified in "sigma" notation to be solved using syntax that closely resembles that notation:

def summation(s): reduce s as $k (0; . + $k);
 
summation( range(1; 1001) | (1/(. * .) ) )

An important point is that nothing is lost in efficiency using the declarative and quite elegant approach using "summation".

Julia[edit]

Using a higher-order function:

julia> sum(k -> 1/k^2, 1:1000)
1.643934566681559
 
julia> pi^2/6
1.6449340668482264
 

A simple loop is more optimized:

julia> function f(n)
s = 0.0
for k = 1:n
s += 1/k^2
end
return s
end
 
julia> f(1000)
1.6439345666815615

K[edit]

  ssr: +/1%_sqr
ssr 1+!1000
1.643935

Lang5[edit]

1000 iota 1 + 1 swap / 2 ** '+ reduce .


Lasso[edit]

define sum_of_a_series(n::integer,k::integer) => {
local(sum = 0)
loop(-from=#k,-to=#n) => {
#sum += 1.00/(math_pow(loop_count,2))
}
return #sum
}
sum_of_a_series(1000,1)
Output:
1.643935

LFE[edit]

With lists:foldl[edit]

 
(defun sum-series (nums)
(lists:foldl
#'+/2
0
(lists:map
(lambda (x) (/ 1 x x))
nums)))
 

With lists:sum[edit]

 
(defun sum-series (nums)
(lists:sum
(lists:map
(lambda (x) (/ 1 x x))
nums)))
 

Both have the same result:

 
> (sum-series (lists:seq 1 100000))
1.6449240668982423
 

Liberty BASIC[edit]

 
for i =1 to 1000
sum =sum +1 /( i^2)
next i
 
print sum
 
end
 

LiveCode[edit]

repeat with i = 1 to 1000
add 1/(i^2) to summ
end repeat
put summ //1.643935

[edit]

to series :fn :a :b
localmake "sigma 0
for [i :a :b] [make "sigma :sigma + invoke :fn :i]
output :sigma
end
to zeta.2 :x
output 1 / (:x * :x)
end
print series "zeta.2 1 1000
make "pi (radarctan 0 1) * 2
print :pi * :pi / 6

Lua[edit]

 
sum = 0
for i = 1, 1000 do sum = sum + 1/i^2 end
print(sum)
 

Lucid[edit]

series = ssum asa  n >= 1000
where
num = 1 fby num + 1;
ssum = ssum + 1/(num * num)
end;

Mathematica[edit]

This is the straightforward solution of the task:

Sum[1/x^2, {x, 1, 1000}]

However this returns a quotient of two huge integers (namely the exact sum); to get a floating point approximation, use N:

N[Sum[1/x^2, {x, 1, 1000}]]

or better:

NSum[1/x^2, {x, 1, 1000}]

Which gives a higher or equal accuracy/precision. Alternatively, get Mathematica to do the whole calculation in floating point by using a floating point value in the formula:

Sum[1./x^2, {x, 1, 1000}]

Other ways include (exact, approximate,exact,approximate):

Total[Table[1/x^2, {x, 1, 1000}]]   
Total[Table[1./x^2, {x, 1, 1000}]]
Plus@@Table[1/x^2, {x, 1, 1000}]
Plus@@Table[1./x^2, {x, 1, 1000}]

MATLAB[edit]

   sum([1:1000].^(-2)) 

Maxima[edit]

(%i45) sum(1/x^2, x, 1, 1000);
835459384831496894781878542648[806 digits]396236858699094240207812766449
(%o45) ------------------------------------------------------------------------
508207201043258126178352922730[806 digits]886537101453118476390400000000
 
(%i46) sum(1/x^2, x, 1, 1000),numer;
(%o46) 1.643934566681561

MAXScript[edit]

total = 0
for i in 1 to 1000 do
(
total += 1.0 / pow i 2
)
print total

МК-61/52[edit]

0	П0	П1	ИП1	1	+	П1	x^2	1/x	ИП0
+ П0 ИП1 1 0 0 0 - x>=0 03
ИП0 С/П

ML[edit]

Standard ML[edit]

 
(* 1.64393456668 *)
List.foldl op+ 0.0 (List.tabulate(1000, fn x => 1.0 / Math.pow(real(x + 1),2.0)))
 

mLite[edit]

println ` fold (+, 0) ` map (fn x = 1 / x ^ 2) ` iota (1,1000);

Output:

1.6439345666815549

MMIX[edit]

x	IS	$1	% flt calculations
y IS $2 % id
z IS $3 % z = sum series
t IS $4 % temp var
 
LOC Data_Segment
GREG @
BUF OCTA 0,0,0 % print buffer
 
LOC #1000
GREG @

// print floating point number in scientific format: 0.xxx...ey..

// most of this routine is adopted from:
// http://www.pspu.ru/personal/eremin/emmi/rom_subs/printreal.html
// float number in z
GREG @
NaN BYTE "NaN..",0
NewLn BYTE #a,0
1H LDA x,NaN
TRAP 0,Fputs,StdOut
GO $127,$127,0
 
prtFlt FUN x,z,z % test if z == NaN
BNZ x,1B
CMP $73,z,0 % if necessary remember it is neg
BNN $73,4F
Sign BYTE '-'
LDA $255,Sign
TRAP 0,Fputs,StdOut
ANDNH z,#8000 % make number pos
// normalizing float number
4H SETH $74,#4024 % initialize mulfactor = 10.0
SETH $73,#0023
INCMH $73,#86f2
INCML $73,#6fc1 %
FLOT $73,$73 % $73 = float 10^16
SET $75,16 % set # decimals to 16
8H FCMP $72,z,$73 % while z >= 10^16 do
BN $72,9F %
FDIV z,z,$74 % z = z / 10.0
ADD $75,$75,1 % incr exponent
JMP 8B % wend
9H FDIV $73,$73,$74 % 10^16 / 10.0
5H FCMP $72,z,$73 % while z < 10^15 do
BNN $72,6F
FMUL z,z,$74 % z = z * 10.0
SUB $75,$75,1 % exp = exp - 1
JMP 5B
NulPnt BYTE '0','.',#00
6H LDA $255,NulPnt % print '0.' to StdOut
TRAP 0,Fputs,StdOut
FIX z,0,z % convert float z to integer
// print mantissa
0H GREG #3030303030303030
STO 0B,BUF
STO 0B,BUF+8 % store print mask in buffer
LDA $255,BUF+16 % points after LSD
% repeat
2H SUB $255,$255,1 % move pointer down
DIV z,z,10 % (q,r) = divmod z 10
GET t,rR % get remainder
INCL t,'0' % convert to ascii digit
STBU t,$255,0 % store digit in buffer
BNZ z,2B % until q == 0
TRAP 0,Fputs,StdOut % print mantissa
Exp BYTE 'e',#00
LDA $255,Exp % print 'exponent' indicator
TRAP 0,Fputs,StdOut
// print exponent
0H GREG #3030300000000000
STO 0B,BUF
LDA $255,BUF+2 % store print mask in buffer
CMP $73,$75,0 % if exp neg then place - in buffer
BNN $73,2F
ExpSign BYTE '-'
LDA $255,ExpSign
TRAP 0,Fputs,StdOut
NEG $75,$75 % make exp positive
2H LDA $255,BUF+3 % points after LSD
% repeat
3H SUB $255,$255,1 % move pointer down
DIV $75,$75,10 % (q,r) = divmod exp 10
GET t,rR
INCL t,'0'
STBU t,$255,0 % store exp. digit in buffer
BNZ $75,3B % until q == 0
TRAP 0,Fputs,StdOut % print exponent
LDA $255,NewLn
TRAP 0,Fputs,StdOut % do a NL
GO $127,$127,0 % return
 
i IS $5 ;iu IS $6
Main SET iu,1000
SETH y,#3ff0 y = 1.0
SETH z,#0000 z = 0.0
SET i,1 for (i=1;i<=1000; i++ ) {
1H FLOT x,i x = int i
FMUL x,x,x x = x^2
FDIV x,y,x x = 1 / x
FADD z,z,x s = s + x
ADD i,i,1
CMP t,i,iu
PBNP t,1B } z = sum
GO $127,prtFlt print sum --> StdOut
TRAP 0,Halt,0

Output:

~/MIX/MMIX/Rosetta> mmix sumseries
0.1643934566681562e1

Modula-3[edit]

Modula-3 uses D0 after a floating point number as a literal for LONGREAL.

MODULE Sum EXPORTS Main;
 
IMPORT IO, Fmt, Math;
 
VAR sum: LONGREAL := 0.0D0;
 
PROCEDURE F(x: LONGREAL): LONGREAL =
BEGIN
RETURN 1.0D0 / Math.pow(x, 2.0D0);
END F;
 
BEGIN
FOR i := 1 TO 1000 DO
sum := sum + F(FLOAT(i, LONGREAL));
END;
IO.Put("Sum of F(x) from 1 to 1000 is ");
IO.Put(Fmt.LongReal(sum));
IO.Put("\n");
END Sum.

Output:

Sum of F(x) from 1 to 1000 is 1.6439345666815612

MUMPS[edit]

 
SOAS(N)
NEW SUM,I SET SUM=0
FOR I=1:1:N DO
.SET SUM=SUM+(1/((I*I)))
QUIT SUM
 

This is an extrinsic function so the usage is:

USER>SET X=$$SOAS^ROSETTA(1000) WRITE X
1.643934566681559806

Nial[edit]

|sum (1 / power (count 1000) 2)
=1.64393

NewLISP[edit]

(let (s 0)
(for (i 1 1000)
(inc s (div 1 (* i i))))
(println s))

Nim[edit]

import math
 
var ls: seq[float] = @[]
for x in 1..1000:
ls.add(1.0 / float(x * x))
echo sum(ls)

Objeck[edit]

 
bundle Default {
class SumSeries {
function : Main(args : String[]) ~ Nil {
DoSumSeries();
}
 
function : native : DoSumSeries() ~ Nil {
start := 1;
end := 1000;
 
sum := 0.0;
 
for(x : Float := start; x <= end; x += 1;) {
sum += f(x);
};
 
IO.Console->GetInstance()->Print("Sum of f(x) from ")->Print(start)->Print(" to ")->Print(end)->Print(" is ")->PrintLine(sum);
}
 
function : native : f(x : Float) ~ Float {
return 1.0 / (x * x);
}
}
}
 

OCaml[edit]

let sum a b fn =
let result = ref 0. in
for i = a to b do
result := !result +. fn i
done;
!result
# sum 1 1000 (fun x -> 1. /. (float x ** 2.))
- : float = 1.64393456668156124

or in a functional programming style:

let sum a b fn =
let rec aux i r =
if i > b then r
else aux (succ i) (r +. fn i)
in
aux a 0.
;;

Simple recursive solution:

let rec sum n = if n < 1 then 0.0 else sum (n-1) +. 1.0 /. float (n*n)
in sum 1000

Octave[edit]

Given a vector, the sum of all its elements is simply sum(vector); a range can be generated through the range notation: sum(1:1000) computes the sum of all numbers from 1 to 1000. To compute the requested series, we can simply write:

sum(1 ./ [1:1000] .^ 2)

Oforth[edit]

: sumSerie(s, n) { 0 n seq apply(#[ s perform + ]) }

Usage :

sumSerie(#[ sq inv ], 1000) println
Output:
1.64393456668156

OpenEdge/Progress[edit]

Conventionally like elsewhere:

DEF VAR dcResult AS DECIMAL NO-UNDO.
DEF VAR n AS INT NO-UNDO.
 
DO n = 1 TO 1000 :
dcResult = dcResult + 1 / (n * n) .
END.
 
DISPLAY dcResult .

or like this:

DEF VAR n AS INT NO-UNDO.
 
REPEAT n = 1 TO 1000 :
ACCUMULATE 1 / (n * n) (total).
END.
 
DISPLAY ( ACCUM total 1 / (n * n) ) .

Oz[edit]

With higher-order functions:

declare
fun {SumSeries S N}
{FoldL {Map {List.number 1 N 1} S}
Number.'+' 0.}
end
 
fun {S X}
1. / {Int.toFloat X*X}
end
in
{Show {SumSeries S 1000}}

Iterative:

  fun {SumSeries S N}
R = {NewCell 0.}
in
for I in 1..N do
R := @R + {S I}
end
@R
end

PARI/GP[edit]

Exact rational solution:

sum(n=1,1000,1/n^2)

Real number solution (accurate to at standard precision):

sum(n=1,1000,1./n^2)

Approximate solution (accurate to at standard precision):

zeta(2)-intnum(x=1000.5,[1],1/x^2)

or

zeta(2)-1/1000.5

Panda[edit]

sum{{1.0.divide(1..1000.sqr)}}

Output:

1.6439345666815615

Pascal[edit]

Program SumSeries;
type
tOutput = double;//extended;
tmyFunc = function(number: LongInt): tOutput;
 
function f(number: LongInt): tOutput;
begin
f := 1/sqr(tOutput(number));
end;
 
function Sum(from,upto: LongInt;func:tmyFunc):tOutput;
var
res: tOutput;
begin
res := 0.0;
// for from:= from to upto do res := res + f(from);
for upTo := upto downto from do res := res + f(upTo);
Sum := res;
end;
 
BEGIN
writeln('The sum of 1/x^2 from 1 to 1000 is: ', Sum(1,1000,@f));
writeln('Whereas pi^2/6 is: ', pi*pi/6:10:8);
end.

Output

different version of type and calculation
extended low to high 1.64393456668155980263E+0000
extended high to low 1.64393456668155980307E+0000
  double low to high 1.6439345666815612E+000
  double high to low 1.6439345666815597E+000
Out:
The sum of 1/x^2 from 1 to 1000 is:  1.6439345666815612E+000
Whereas pi^2/6 is:                  1.64493407

Perl[edit]

my $sum = 0;
$sum += 1 / $_ ** 2 foreach 1..1000;
print "$sum\n";

or

use List::Util qw(reduce);
$sum = reduce { $a + 1 / $b ** 2 } 0, 1..1000;
print "$sum\n";

An other way of doing it is to define the series as a closure:

my $S = do { my ($sum, $k); sub { $sum += 1/++$k**2 } };
my @S = map &$S, 1 .. 1000;
print $S[-1];

Perl 6[edit]

(Some of these work with rakudo, and others with niecza. Eventually they'll all work everywhere...)

In general, the $nth partial sum of a series whose terms are given by a unary function &f is

[+] map &f, 1 .. $n

So what's needed in this case is

say [+] map { 1 / $^n**2 }, 1 .. 1000;

Or, using the "hyper" metaoperator to vectorize, we can use a more "point free" style while keeping traditional precedence:

say [+] 1 «/« (1..1000) »**» 2;

Or we can use the X "cross" metaoperator, which is convenient even if one side or the other is a scalar. In this case, we demonstrate a scalar on either side:

say [+] 1 X/ (1..1000 X** 2);

Note that cross ops are parsed as list infix precedence rather than using the precedence of the base op as hypers do. Hence the difference in parenthesization.

In a lazy language like Perl 6, it's generally considered a stronger abstraction to write the correct infinite sequence, and then take the part of it you're interested in. Here we define an infinite sequence of partial sums (by adding a backslash into the reduction to make it look "triangular"), then take the 1000th term of that:

constant @x = [\+] 0, { 1 / ++(state $n) ** 2 } ... *;
say @x[1000]; # prints 1.64393456668156

Note that infinite constant sequences can be lazily generated in Perl 6, or this wouldn't work so well...

A cleaner style is to combine these approaches with a more FP look:

constant ζish = [\+] map -> \𝑖 { 1 / 𝑖**2 }, 1..*;
say ζish[1000];

Perhaps the cleanest way is to just define the zeta function and evaluate it for s=2, possibly using memoization:

sub ζ($s) is cached { [\+] 1..* X** -$s }
say ζ(2)[1000];

Notice how the thus-defined zeta function returns a lazy list of approximated values, which is arguably the closest we can get from the mathematical definition.

Finally, if list comprehensions are your hammer, you can nail it this way:

say [+] (1 / $_**2 for 1..1000);

That's fine for a single result, but if you're going to be evaluating the sequence multiple times, you don't want to be recalculating the sum each time, so it's more efficient to define the sequence as a constant to let the run-time automatically cache those values already calculated.

Phix[edit]

function sumto(atom n)
atom res = 0
for i=1 to n do
res += 1/(i*i)
end for
return res
end function
?sumto(1000)
Output:
1.643934567

PHP[edit]

<?php
 
/**
* @author Elad Yosifon
*/

 
/**
* @param int $n
* @param int $k
* @return float|int
*/

function sum_of_a_series($n,$k)
{
$sum_of_a_series = 0;
for($i=$k;$i<=$n;$i++)
{
$sum_of_a_series += (1/($i*$i));
}
return $sum_of_a_series;
}
 
echo sum_of_a_series(1000,1);
 
Output:
1.6439345666816

PicoLisp[edit]

(scl 9)  # Calculate with 9 digits precision
 
(let S 0
(for I 1000
(inc 'S (*/ 1.0 (* I I))) )
(prinl (round S 6)) ) # Round result to 6 digits

Output:

1.643935

Pike[edit]

array(int) x = enumerate(1000,1,1);
`+(@(1.0/pow(x[*],2)[*]));
Result: 1.64393

PL/I[edit]

/* sum the first 1000 terms of the series 1/n**2. */
s = 0;
 
do i = 1000 to 1 by -1;
s = s + 1/float(i**2);
end;
 
put skip list (s);
Output:
1.64393456668155980E+0000

Pop11[edit]

lvars s = 0, j;
for j from 1 to 1000 do
s + 1.0/(j*j) -> s;
endfor;
 
s =>

PostScript[edit]

 
/aproxriemann{
/x exch def
/i 1 def
/sum 0 def
x{
/sum sum i -2 exp add def
/i i 1 add def
}repeat
sum ==
}def
 
1000 aproxriemann
 

Output:

 
1.64393485
 
Library: initlib
 
% using map
[1 1000] 1 range {dup * 1 exch div} map 0 {+} fold
 
% just using fold
[1 1000] 1 range 0 {dup * 1 exch div +} fold
 

PowerShell[edit]

$x = 1..1000 `
| ForEach-Object { 1 / ($_ * $_) } `
| Measure-Object -Sum
Write-Host Sum = $x.Sum

Prolog[edit]

Works with SWI-Prolog.

sum(S) :-
findall(L, (between(1,1000,N),L is 1/N^2), Ls),
sumlist(Ls, S).
 

Ouptput :

?- sum(S).
S = 1.643934566681562.

PureBasic[edit]

Define i, sum.d
 
For i=1 To 1000
sum+1.0/(i*i)
Next i
 
Debug sum

Answer = 1.6439345666815615

Python[edit]

print ( sum(1.0 / (x * x) for x in range(1, 1001)) )

R[edit]

print( sum( 1/seq(1000)^2 ) )

Racket[edit]

A solution using Typed Racket:

 
#lang typed/racket
 
(: S : Natural -> Real)
(define (S n)
(for/sum: : Real ([k : Natural (in-range 1 (+ n 1))])
(/ 1.0 (* k k))))
 

Raven[edit]

0 1 1000 1 range each 1.0 swap dup * / +
"%g\n" print
Output:
1.64393

Raven uses a 32 bit float, so precision limits the accuracy of the result for large iterations.

REXX[edit]

sums specific terms[edit]

/*REXX program sums the first   N   terms of    1/(k**2),    k=1 ──►  N.      */
parse arg N D . /*obtain optional arguments from C.L. */
if N=='' | N==',' then N=1000 /*Not specified? Then use the default.*/
if D=='' | D==',' then D= 60 /* " " " " " " */
numeric digits D /*use D digits (9 is the REXX default).*/
$=0 /*initialize the sum to zero. */
do k=1 for N /* [↓] compute for N terms. */
$=$ + 1/k**2 /*add a squared reciprocal to the sum. */
end /*k*/
 
say 'The sum of' N "terms is:" $ /*stick a fork in it, we're all done. */

output   when using the default input:

The sum of 1000 terms is: 1.64393456668155980313905802382221558965210344649368531671713

sums with running total[edit]

This REXX version shows the   running total   for every 10th term.

/*REXX program sums the first   N   terms of    1/(k**2),    k=1 ──►  N.      */
parse arg N D . /*obtain optional arguments from C.L. */
if N=='' | N==',' then N=1000 /*Not specified? Then use the default.*/
if D=='' | D==',' then D= 60 /* " " " " " " */
numeric digits D /*use D digits (9 is the REXX default).*/
w=length(N) /*max width for the formatted output. */
$=0 /*initialize the sum to zero. */
do k=1 for N /* [↓] compute for N terms. */
$=$ + 1/k**2 /*add a squared reciprocal to the sum. */
parse var k s 2 m '' -1 e /*obtain the start and end decimal digs*/
if e\==0 then iterate /*does K end with the dec digit 0 ? */
if s\==1 then iterate /* " " start " " " " 1 ? */
if m\=0 then iterate /* " " middle contain any non-zero ?*/
if k==N then iterate /* " " equal N, then skip running sum*/
say 'The sum of' right(k,w) "terms is:" $ /*display running sum.*/
end /*k*/
say /*blank line for sep. */
say 'The sum of' right(k-1,w) "terms is:" $ /*display final sum.*/
/*stick a fork in it, we're all done. */

output   when using the input of:   1000000000

The sum of         10 terms is: 1.54976773116654069035021415973796926177878558830939783320736
The sum of        100 terms is: 1.63498390018489286507716949818032376668332170003126381385307
The sum of       1000 terms is: 1.64393456668155980313905802382221558965210344649368531671713
The sum of      10000 terms is: 1.64483407184805976980608183331031090353799751949684175308996
The sum of     100000 terms is: 1.64492406689822626980574850331269185564752132981156034248806
The sum of    1000000 terms is: 1.64493306684872643630574849997939185588561654406394129491321
The sum of   10000000 terms is: 1.64493396684823143647224849997935852288561656787346272343397
The sum of  100000000 terms is: 1.64493405684822648647241499997935852255228656787346510441026
The sum of 1000000000 terms is: 1.64493406584822643697241516647935852255228323457346510444171

output   from a calculator (π²/6, using 60 digits) showing the correct number of digits [superscript digits were added after-the-fact]:

1.64493406684822643647241516664602518921894990120679843773556

sums with running significance[edit]

This is a technique to show a   running significance   (based on the previous calculation).

If the   old   REXX variable would be set to   1.64   (instead of   1), the first noise digits could be bypassed to make the display cleaner.

/*REXX program sums the first   N   terms of    1/(k**2),    k=1 ──►  N.      */
parse arg N D . /*obtain optional arguments from C.L. */
if N=='' | N==',' then N=1000 /*Not specified? Then use the default.*/
if D=='' | D==',' then D= 60 /* " " " " " " */
numeric digits D /*use D digits (9 is the REXX default).*/
w=length(N) /*max width for the formatted output. */
$=0 /*initialize the sum to zero. */
old=1 /*the new sum to compared to the old. */
p=0 /*significant decimal precision so far.*/
do k=1 for N /* [↓] compute for N terms. */
$=$ + 1/k**2 /*add a squared reciprocal to the sum. */
c=compare($,old) /*see how we're doing with precision. */
if c>p then do /*Got another significant decimal dig? */
say 'The significant sum of' right(k,w) "terms is:" left($,c)
p=c /*use the new significant precision. */
end /* [↑] display significant part of sum*/
old=$ /*use "old" sum for the next compare. */
end /*k*/
say /*display blank line for sep.*/
say 'The sum of' right(N,w) "terms is:" /*display the sum's preamble.*/
say $ /*display the sum on its own line. */
/*stick a fork in it, we're all done. */

output   when using the input of:   35000000   100

The significant sum of        2 terms is: 1.
The significant sum of        3 terms is: 1.3
The significant sum of        5 terms is: 1.46
The significant sum of       14 terms is: 1.575
The significant sum of       34 terms is: 1.6159
The significant sum of      110 terms is: 1.63588
The significant sum of      328 terms is: 1.641889
The significant sum of     1024 terms is: 1.6439579
The significant sum of     3207 terms is: 1.64462229
The significant sum of    10043 terms is: 1.644834499
The significant sum of    31782 terms is: 1.6449026029
The significant sum of   100314 terms is: 1.64492409819
The significant sum of   316728 terms is: 1.644930909569
The significant sum of  1000853 terms is: 1.6449330677009
The significant sum of  3163463 terms is: 1.64493375073899
The significant sum of 10001199 terms is: 1.644933966860219
The significant sum of 31627592 terms is: 1.6449340352302649

The sum of 35000000 terms is:

1.644934038276798273207105156927852205740478629316117966926591883437164764834567731984252290795163298

One can see a pattern in the number of significant digits computed based on the number of terms used.   (See a discussion in the talk section.)

Ring[edit]

 
sum = 0
for i =1 to 1000
sum = sum + 1 /(pow(i,2))
next
decimals(8)
see sum
 

RLaB[edit]

 
>> sum( (1 ./ [1:1000]) .^ 2 ) - const.pi^2/6
-0.000999500167
 

Ruby[edit]

puts (1..1000).inject{|sum, x| sum + 1.0 / x ** 2}
#=> 1.64393456668156

Run BASIC[edit]

 
for i =1 to 1000
sum = sum + 1 /( i^2)
next i
print sum

Rust[edit]

const LOWER: i32 = 1;
const UPPER: i32 = 1000;
 
// Because the rule for our series is simply adding one, the number of terms are the number of
// digits between LOWER and UPPER
const NUMBER_OF_TERMS: i32 = (UPPER + 1) - LOWER;
fn main() {
// Formulaic method
println!("{}", (NUMBER_OF_TERMS * (LOWER + UPPER)) / 2);
// Naive method
println!("{}", (LOWER..UPPER + 1).fold(0, |sum, x| sum + x));
}
 

SAS[edit]

data _null_;
s=0;
do n=1 to 1000;
s+1/n**2; /* s+x is synonym of s=s+x */
end;
e=s-constant('pi')**2/6;
put s e;
run;

Scala[edit]

scala> 1 to 1000 map (x => 1.0 / (x * x)) sum
res30: Double = 1.6439345666815615

Scheme[edit]

(define (sum a b fn)
(do ((i a (+ i 1))
(result 0 (+ result (fn i))))
((> i b) result)))
 
(sum 1 1000 (lambda (x) (/ 1 (* x x)))) ; fraction
(exact->inexact (sum 1 1000 (lambda (x) (/ 1 (* x x))))) ; decimal

More idiomatic way (or so they say) by tail recursion:

(define (invsq f to)
(let loop ((f f) (s 0))
(if (> f to)
s
(loop (+ 1 f) (+ s (/ 1 f f))))))
 
;; whether you get a rational or a float depends on implementation
(invsq 1 1000) ; 835459384831...766449/50820...90400000000
(exact->inexact (invsq 1 1000)) ; 1.64393456668156

Seed7[edit]

$ include "seed7_05.s7i";
include "float.s7i";
 
const func float: invsqr (in float: n) is
return 1.0 / n**2;
 
const proc: main is func
local
var integer: i is 0;
var float: sum is 0.0;
begin
for i range 1 to 1000 do
sum +:= invsqr(flt(i));
end for;
writeln(sum digits 6 lpad 8);
end func;

Sidef[edit]

say (1..^1000 -> map {|i| 1 / i**2 }«+»)

Alternatively, using the reduce method:

say (1..^1000 -> reduce { |a,b| a + (1 / b**2) })
Output:
1.64393456668155980313905802382222

Slate[edit]

Manually coerce it to a float, otherwise you will get an exact (and slow) answer:

((1 to: 1000) reduce: [|:x :y | x + (y squared reciprocal as: Float)]).

Smalltalk[edit]

( (1 to: 1000) fold: [:sum :aNumber |
sum + (aNumber squared reciprocal) ] ) asFloat displayNl.

SQL[edit]

CREATE TABLE t1 (n REAL);
-- this is postgresql specific, fill the table
INSERT INTO t1 (SELECT generate_series(1,1000)::REAL);
WITH tt AS (
SELECT 1/(n*n) AS recip FROM t1
) SELECT SUM(recip) FROM tt;
 

Result of select (with locale DE):

       sum        
------------------
 1.64393456668156
(1 Zeile)

Swift[edit]

 
func sumSeries(var n: Int) -> Double {
var ret: Double = 0
 
for i in 1...n {
ret += (1 / pow(Double(i), 2))
}
 
return ret
}
 
output: 1.64393456668156
 
 
Swift also allows extension to datatypes. Here's similar code using an extension to Int.
 
extension Int {
func SumSeries() -> Double {
var ret: Double = 0
 
for i in 1...self {
ret += (1 / pow(Double(i), 2))
}
 
return ret
}
}
 
var x: Int = 1000
var y: Double
 
y = x.sumSeries() /* y = 1.64393456668156 */
 
Swift also allows you to do this:
 
y = 1000.sumSeries()
 

Tcl[edit]

Works with: Tcl version 8.5
package require Tcl 8.5
 
proc partial_sum {func - start - stop} {
for {set x $start; set sum 0} {$x <= $stop} {incr x} {
set sum [expr {$sum + [apply $func $x]}]
}
return $sum
}
 
set S {x {expr {1.0 / $x**2}}}
 
partial_sum $S from 1 to 1000 ;# => 1.6439345666815615
Library: Tcllib (Package: struct::list)
package require Tcl 8.5
package require struct::list
 
proc sum_of {lambda nums} {
struct::list fold [struct::list map $nums [list apply $lambda]] 0 ::tcl::mathop::+
}
 
sum_of $S [range 1 1001] ;# ==> 1.6439345666815615

The helper range procedure is:

# a range command akin to Python's
proc range args {
foreach {start stop step} [switch -exact -- [llength $args] {
1 {concat 0 $args 1}
2 {concat $args 1}
3 {concat $args }
default {error {wrong # of args: should be "range ?start? stop ?step?"}}
}] break
if {$step == 0} {error "cannot create a range when step == 0"}
set range [list]
while {$step > 0 ? $start < $stop : $stop < $start} {
lappend range $start
incr start $step
}
return $range
}

TI-89 BASIC[edit]

∑(1/x^2,x,1,1000)

TXR[edit]

Reduce with + operator over a lazily generated list.

Variant A1: limit the list generation inside the gen operator.

txr -p '[reduce-left + (let ((i 0)) (gen (< i 1000) (/ 1.0 (* (inc i) i)))) 0]'
1.64393456668156

Variant A2: generate infinite list, but take only the first 1000 items using [list-expr 0..999].

txr -p '[reduce-left + [(let ((i 0)) (gen t (/ 1.0 (* (inc i) i)))) 0..999] 0]'
1.64393456668156

Variant B: generate lazy integer range, and pump it through a series of function with the help of the chain functional combinator and the op partial evaluation/binding operator.

txr -p '[[chain range (op mapcar (op / 1.0 (* @1 @1))) (op reduce-left + @1 0)] 1 1000]'
1.64393456668156

Variant C: unravel the chain in Variant B using straightforward nesting.

txr -p '[reduce-left + (mapcar (op / 1.0 (* @1 @1)) (range 1 1000)) 0]'
1.64393456668156

Variant D: bring Variant B's inverse square calculation into the fold, eliminating mapcar. Final answer.

txr -p '[reduce-left (op + @1 (/ 1.0 (* @2 @2))) (range 1 1000) 0]'
1.64393456668156

UnixPipes[edit]

term() {
b=$1;res=$2
echo "scale=5;1/($res*$res)+$b" | bc
}
 
sum() {
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
}
 
fold() {
func=$1
(while read a ; do
fold $func | $func $a
done)
}
 
(echo 3; echo 1; echo 4) | fold sum

Unicon[edit]

See Icon.

Ursala[edit]

The expression plus:-0. represents a function returning the sum of any given list of floating point numbers, or zero if it's empty, using the built in reduction operator, :-, and the binary addition function, plus. The rest the expression constructs the series by inverting the square of each number in the list from 1 to 1000.

#import flo
#import nat
 
#cast %e
 
total = plus:-0 div/*1. sqr* float*t iota 1001

output:

1.643935e+00

Vala[edit]

 
public static void main(){
int i, start = 1, end = 1000;
double sum = 0.0;
 
for(i = start; i<= end; i++)
sum += (1 / (double)(i * i));
 
stdout.printf("%s\n", sum.to_string());
}
 

Output:

1.6439345666815615

Wortel[edit]

@sum !*#~V1Sn @to 1000 ; returns 1.6439345666815615
@to 1000 ; generates a list of 1 to 1000 (inclusive)
#~V1Sn ; number expression which stands for: square push(1) swap divide
!* ; maps the number expression over the list
@sum ; sums the list

XPL0[edit]

code CrLf=9;  code real RlOut=48;
int X; real S;
[S:= 0.0;
for X:= 1 to 1000 do S:= S + 1.0/float(X*X);
RlOut(0, S); CrLf(0);
]

Output:

    1.64393

Yorick[edit]

(1./indgen(1:1000)^2)(sum)

zkl[edit]

[1.0..1000].reduce(fcn(p,n){p+1.0/(n*n)},0.0)  //-->1.64394