Sum of a series: Difference between revisions

(added langur language example)
 
(8 intermediate revisions by 7 users not shown)
Line 980:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 986:
public program()
{
var sum := new Range(1, 1000).selectBy::(x => 1.0r / (x * x)).summarize(new Real());
console.printLine:(sum)
}</syntaxhighlight>
{{out}}
Line 998:
<syntaxhighlight lang="elixir">iex(1)> Enum.reduce(1..1000, 0, fn x,sum -> sum + 1/(x*x) end)
1.6439345666815615</syntaxhighlight>
 
 
=={{header|Elm}}==
<syntaxhighlight lang="elm">
module Main exposing (main)
 
import Html exposing (h1, div, p, text)
import Html.Attributes exposing (style)
 
aList : List Int
aList = List.range 1 1000
 
 
-- version a with a list
k2xSum : Float
k2xSum = List.sum
<| List.map (\x -> 1.0 / x / x )
<| List.map (\n -> toFloat n) aList
 
 
-- version b with a list
fx : Int -> Float
fx =
(\n -> toFloat n |> \m -> 1.0 / m / m)
 
f2kSum : Float
f2kSum = List.sum
<| List.map fx aList
 
-- version with recursion, without a list
untilMax : Int -> Int -> Float -> Float
untilMax k kmax accum =
if k > kmax
then accum
else
}let
sx += fun(i);toFloat k
dx = 1.0 / x / x
in untilMax (k + 1) kmax (accum + dx)
 
recSum : Float
recSum = untilMax 1 1000 0.0
 
main = div [style "margin" "5%", style "color" "blue"] [
h1 [] [text "Sum of series Σ 1/k²"]
, text (" Version a with a list: Sum = " ++ String.fromFloat k2xSum)
, p [] [text (" Version b with a list: Sum = " ++ String.fromFloat f2kSum)]
, p [] [text (" Recursion version c: Sum = " ++ String.fromFloat recSum)]
]
</syntaxhighlight>
 
{{outOut}}
<pre>
Sum of series Σ 1/k²
Version a with a list: Sum = 1.6439345666815615
 
Version b with a list: Sum = 1.6439345666815615
 
Recursion version c: Sum = 1.6439345666815615
</pre>
 
=={{header|Emacs Lisp}}==
Line 1,674 ⟶ 1,734:
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln "calc.: ", fold ffn{+}, map ffn(.x) { 1/.x^2 }, 1..1000
writeln "exactknown: ", pi^2/6</syntaxhighlight>
 
{{out}}
Line 1,686 ⟶ 1,746:
<syntaxhighlight lang="langur">mode divMaxScale = 100
 
writeln "calc.: ", fold ffn{+}, map ffn(.x) 1/.x^2, 1..1000
writeln "exactknown: ", pi^2/6</syntaxhighlight>
 
{{out}}
Line 2,815 ⟶ 2,875:
 
=={{header|RPL}}==
≪ 0 1 ROT '''FOR''' k SQ INV + '''NEXT''' ≫ ''''<span style="color:blue">∑INV2</span>'''' STO
 
1000 '''<span style="color:blue">∑INV2'''</span>
The emulator immediately returns
1: 1.64393456668
A vintagebasic HP-28S calculator returns after 27.5 seconds
1: 1.64393456674
{{works with|HP|49}}
'k' 1 1000 '1/SQ(k)' ∑
returns in 2 minutes 27 seconds, with exact mode set:
1: 83545938483149689478187854264854884386044454314086472930763839512603803291207881839588904977469387999844962675327115010933903589145654299730231109091124308462732153297321867661093162618281746011828755017021645889046777854795025297006943669294330752479399654716368801794529682603741344724733173765262964463970763934463926259796895140901128384286333311745462863716753134735154188954742414035836608258393970996630553795415075904205673610359458498106833291961256452756993199997231825920203667952667546787052535763624910912251107083702817265087341966845358732584971361645348091123849687614886682117125784781422103460192439394780707024963279033532646857677925648889105430050030795563141941157379481719403833258405980463950499887302926152552848089894630843538497552630691676216896740675701385847032173192623833881016332493844186817408141003602396236858699094240207812766449 / 50820720104325812617835292273000760481839790754374852703215456050992581046448162621598030244504097240825920773913981926305208272518886258627010933716354037062979680120674828102224650586465553482032614190502746121717248161892239954030493982549422690846180552358769564169076876408783086920322038142618269982747137757706040198826719424371333781947889528085329853597116893889786983109597085041878513917342099206896166585859839289193299599163669641323895022932959750057616390808553697984192067774252834860398458100840611325353202165675189472559524948330224159123505567527375848194800452556940453530457590024173749704941834382709198515664897344438584947842793131829050180589581507273988682409028088248800576590497216884808783192565859896957125449502802395453976401743504938336291933628859306247684023233969172475385327442707968328512729836445886537101453118476390400000000
 
=={{header|Ruby}}==
Line 2,903 ⟶ 2,967:
writeln(sum digits 6 lpad 8);
end func;</syntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">print( +/[1/k**2 : k in [1..1000]] );</syntaxhighlight>
{{out}}
<pre>1.64393456668156</pre>
 
=={{header|Sidef}}==
Line 3,282 ⟶ 3,351:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var sumSeries = Fn.new { |n| (1..n).reduce(0) { |sum, i| sum + 1/(i*i) } }
 
System.print("s(1000) = %(sumSeries.call(1000))")
Line 3,310 ⟶ 3,379:
 
=={{header|Zig}}==
{{Works with|Zig|0.11.0}}
<syntaxhighlight lang="zig">const std = @import("std");
 
fn f(x: i64u64) f64 {
return 1 / @intToFloatas(f64, @floatFromInt(x * x));
}
 
fn Ssum(funcomptime func: fn (i64u64) f64, n: i64u64) f64 {
var s: f64 = 0.0;
var i: i64u64 = n;
 
while (i >!= 0) : (i -= 1) {
s += func(i);
 
while (i > 0) : (i -= 1) {
s += fun(i);
}
return s;
}
 
pub fn main() !void {
{
const stdout = std.io.getStdOut().writer();
try stdout.print("S_1000 = {d:.15}\n", .{Ssum(f, 1000)});
}</syntaxhighlight>{{out}}
{{out}}
<pre>S_1000 = 1.643934566681560</pre>
 
885

edits