Sum of a series: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add SETL) |
imported>Polarit (→{{header|Elm}}: 3 different versions, 2 with lists, 1 with recursion) |
||
Line 1,004: | Line 1,004: | ||
module Main exposing (main) |
module Main exposing (main) |
||
import Html exposing (text) |
import Html exposing (h1, div, p, text) |
||
import Html.Attributes exposing (style) |
|||
aList : List Int |
|||
aList = List.range 1 1000 |
|||
k2xSum = List.foldl (+) 0.0 |
|||
-- version a with a list |
|||
⚫ | |||
k2xSum : Float |
|||
⚫ | |||
k2xSum = List.sum |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- version b with a list |
|||
fx : Int -> Float |
|||
⚫ | |||
(\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 |
|||
⚫ | |||
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²"] |
|||
⚫ | |||
, p [] [text (" Version b with a list: Sum = " ++ String.fromFloat f2kSum)] |
|||
, p [] [text (" Recursion version c: Sum = " ++ String.fromFloat recSum)] |
|||
] |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Sum of series Σ 1/k² |
|||
⚫ | |||
Version a with a list: Sum = 1.6439345666815615 |
|||
⚫ | |||
Version b with a list: Sum = 1.6439345666815615 |
|||
⚫ | |||
⚫ | |||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |