Sum of a series: Difference between revisions

Content added Content deleted
No edit summary
Line 2,169: Line 2,169:
{{out}}
{{out}}
<pre>1.6439345666816</pre>
<pre>1.6439345666816</pre>

=={{header|Picat}}==
<lang Picat>go =>
% List comprehension
test(s,1000),
nl,
% Iterative
test(s2,1000),
nl.

test(Fun,N) =>
println([fun=Fun,n=N]),
Pi2_6 = math.pi**2/6,
println(Pi2_6='math.pi**2/6'),
nl,
foreach(I in 1..6)
S = apply(Fun,10**I),
printf("%f (diff: %w)\n", S,Pi2_6-S)
end,
nl.

% List comprehension
s(N) = sum([1.0/K**2 : K in 1..N]).

% Iterative
s2(N) = Sum =>
K = 1,
Sum1 = 0,
while(K <= N)
Sum1 := Sum1 + 1/K**2,
K := K + 1
end,
Sum = Sum1.</lang>

{{out}}
<pre>[fun = s,n = 1000]
1.644934066848226 = math.pi**2/6

1.549768 (diff: 0.095166335681686)
1.634984 (diff: 0.009950166663334)
1.643935 (diff: 0.000999500166665)
1.644834 (diff: 0.000099995000161)
1.644924 (diff: 0.000009999949984)
1.644933 (diff: 0.000000999999456)

[fun = s2,n = 1000]
1.644934066848226 = math.pi**2/6

1.549768 (diff: 0.095166335681686)
1.634984 (diff: 0.009950166663334)
1.643935 (diff: 0.000999500166665)
1.644834 (diff: 0.000099995000161)
1.644924 (diff: 0.000009999949984)
1.644933 (diff: 0.000000999999456)</pre>



=={{header|PicoLisp}}==
=={{header|PicoLisp}}==