Sum of a series: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: use ^ operator, added a couple more ways to do it) |
No edit summary |
||
Line 93: | Line 93: | ||
In [http://haskell.org/haskellwiki/Pointfree point-free] style: |
In [http://haskell.org/haskellwiki/Pointfree point-free] style: |
||
(sum . map (1/) . map (^2)) [1..1000] |
(sum . map (1/) . map (^2)) [1..1000] |
||
=={{header|IDL}}== |
|||
print,total( 1/(1+findgen(1000))^1) |
|||
=={{header|J}}== |
=={{header|J}}== |
Revision as of 20:39, 4 March 2008
Sum of a series
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
Display the sum of a finite series for a given range.
For this task, use S(x) = 1/x^2, from 1 to 1000. (This approximates the Riemann zeta function. The Basel problem solved this: zeta(2) = π2/6.)
Ada
<ada>with Ada.Text_Io; use Ada.Text_Io;
procedure Sum_Series is
function F(X : Long_Float) return Long_Float is begin return 1.0 / X**2; end F; package Lf_Io is new Ada.Text_Io.Float_Io(Long_Float); use Lf_Io; Sum : Long_Float := 0.0; subtype Param_Range is Integer range 1..1000;
begin
for I in Param_Range loop Sum := Sum + F(Long_Float(I)); end loop; Put("Sum of F(x) from" & Integer'Image(Param_Range'First) & " to" & Integer'Image(Param_Range'Last) & " is "); Put(Item => Sum, Aft => 10, Exp => 0); New_Line;
end Sum_Series;</ada>
C++
#include <iostream> double f(double x); int main() { unsigned int start = 1; unsigned int end = 1000; double sum = 0; for( unsigned int x = start; x <= end; ++x ) { sum += f(x); } std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl; return 0; } double f(double x) { return ( 1 / ( x * x ) ); }
D
module series ; import std.stdio ; T series(T)(T function(int) t, int end, int start = 1 /* 0 if zero base*/ ) { T sum = 0 ; for(int i = start ; i <= end ; i++) sum += t(i) ; return sum ; } real term(int n){ return 1.0L/(n*n) ; } void main(){ writef("sum@[1..1000] = ", series(&term, 1000)) ; }
E
pragma.enable("accumulator") accum 0 for x in 1..1000 { _ + 1 / x ** 2 }
Forth
: sum ( fn start count -- fsum ) 0e bounds do i s>d d>f dup execute f+ loop drop ; :noname ( x -- 1/x^2 ) fdup f* 1/f ; ( xt ) 1 1000 sum f. \ 1.64393456668156 pi pi f* 6e f/ f. \ 1.64493406684823
Haskell
With a list comprehension:
sum [1 / x ^ 2 | x <- [1..1000]]
With higher-order functions:
sum $ map (\x -> 1 / x ^ 2) [1..1000]
In point-free style:
(sum . map (1/) . map (^2)) [1..1000]
IDL
print,total( 1/(1+findgen(1000))^1)
J
NB. sum of inverse of square of first thousand positive integers +/ % *: >: i. 1000 1.64393 (*:o.1)%6 NB. pi squared over six, for comparison 1.64493
Java
public class Sum{ public static double f(double x){ return 1/(x*x); } public static void main(String[] args){ double start = 1; double end = 1000; double sum = 0; for(double x = start;x <= end;x++) sum += f(x); System.out.println("Sum of f(x) from " + start + " to " + end +" is " + sum); } }
Perl
my $sum = 0; map { $sum += 1 / ( $_ * $_ ) } (1..1000); print "$sum\n";
Python
print sum(1.0 / x ** 2 for x in xrange(1, 1001))