Sum of a series: Difference between revisions
Content deleted Content added
m →{{header|Euphoria}}: Edit {{works with}}. |
|||
Line 1,052: | Line 1,052: | ||
(sum 1 1000 (lambda (x) (/ 1 (* x x)))) ; fraction |
(sum 1 1000 (lambda (x) (/ 1 (* x x)))) ; fraction |
||
(exact->inexact (sum 1 1000 (lambda (x) (/ 1 (* x x))))) ; decimal</lang> |
(exact->inexact (sum 1 1000 (lambda (x) (/ 1 (* x x))))) ; decimal</lang> |
||
More idiomatic way (or so they say) by tail recursion: |
|||
<lang scheme>(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</lang> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |