Sum of a series: Difference between revisions

(Sum of a series in Verilog)
 
(14 intermediate revisions by 10 users not shown)
Line 828:
<syntaxhighlight lang="e">pragma.enable("accumulator")
accum 0 for x in 1..1000 { _ + 1 / x ** 2 }</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
numfmt 8 0
for i = 1 to 1000
s += 1 / (i * i)
.
print s
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 971 ⟶ 980:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 977 ⟶ 986:
public program()
{
var sum := new Range(1, 1000).selectBy::(x => 1.0r / (x * x)).summarize(new Real());
console.printLine:(sum)
}</syntaxhighlight>
{{out}}
Line 989 ⟶ 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
x = 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>
 
{{Out}}
<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,236 ⟶ 1,305:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Sum_of_a_series}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
In the following function, the first parameter is the series is provided as a lambda expression. The second parameter is the number of terms to calculate
In '''[https://formulae.org/?example=Sum_of_a_series this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Sum of a series 01.png]]
 
'''Test case'''
 
The exact value (of the sum) is:
 
[[File:Fōrmulæ - Sum of a series 02.png]]
 
(click to enlarge)
 
[[File:Fōrmulæ - Sum of a series 03.png|750px||link=https://static.wikiforge.net/rosettacodewikitide/0/0f/F%C5%8Drmul%C3%A6_-_Sum_of_a_series_03.png]]
 
The approximate value is:
 
[[File:Fōrmulæ - Sum of a series 04.png]]
 
[[File:Fōrmulæ - Sum of a series 05.png]]
 
While the (approximate) value of π<sup>2</sup>/6 is:
 
[[File:Fōrmulæ - Sum of a series 06.png]]
 
[[File:Fōrmulæ - Sum of a series 07.png]]
 
=={{header|GAP}}==
Line 1,639 ⟶ 1,732:
=={{header|Lang5}}==
<syntaxhighlight lang="lang5">1000 iota 1 + 1 swap / 2 ** '+ reduce .</syntaxhighlight>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">writeln "calc.: ", fold fn{+}, map fn(.x) { 1/.x^2 }, 1..1000
writeln "known: ", pi^2/6</syntaxhighlight>
 
{{out}}
<pre>calc.: 1.643934566681559803139058023822206
exact: 1.644934066848226436472415166646025
</pre>
 
If we set a higher arbitrary maximum for division, we get more digits.
 
<syntaxhighlight lang="langur">mode divMaxScale = 100
 
writeln "calc.: ", fold fn{+}, map fn(.x) 1/.x^2, 1..1000
writeln "known: ", pi^2/6</syntaxhighlight>
 
{{out}}
<pre>calc.: 1.6439345666815598031390580238222155896521034464936853167172372054281147052136371544864376381235947140
exact: 1.6449340668482264364724151666460251892189499012067984377355582293700074704032008738336289006197587053
</pre>
 
=={{header|Lasso}}==
Line 2,024 ⟶ 2,138:
{{out}}
<pre>1.643934566681561</pre>
 
=={{header|Oberon-2}}==
{{trans|Modula-2}}
<syntaxhighlight lang="oberon2">MODULE SS;
 
IMPORT Out;
 
TYPE
RealFunc = PROCEDURE(r:REAL):REAL;
 
PROCEDURE SeriesSum(k,n:LONGINT;f:RealFunc):REAL;
VAR
total:REAL;
i:LONGINT;
BEGIN
total := 0.0;
FOR i := k TO n DO total := total + f(i) END;
RETURN total
END SeriesSum;
PROCEDURE OneOverKSquared(k:REAL):REAL;
BEGIN RETURN 1.0 / (k * k)
END OneOverKSquared;
BEGIN
Out.Real(SeriesSum(1,1000,OneOverKSquared),10);
Out.Ln;
END SS.
</syntaxhighlight>
 
{{out}}
<pre>1.64393E+00
</pre>
 
=={{header|Objeck}}==
Line 2,726 ⟶ 2,873:
-0.000999500167
</syntaxhighlight>
 
=={{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 basic 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,807 ⟶ 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,186 ⟶ 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,214 ⟶ 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