Archive

Archive for the ‘Sums’ Category

Power sum in Ruby

March 18, 2013 Leave a comment
The logo, selected as winner of the Ruby Logo ...

The logo, selected as winner of the Ruby Logo Contest (Photo credit: Wikipedia)

1^m + 2^m + \ldots + n^m = ?

It is not easy to calculate if n is 50, nad m is 9. Here Ruby comes for help. If you had already installed Ruby 1.8 or Ruby 1.9, open terminal (in Linux) type irb and press enter. Then type following (don’t type > sign) :

>sum(n,m)
>return 1 if n == 1
>sum(n-1,m) + n^m if n > 1
>end

It is all, now type sum(50,9) and you will get the answer…

 

 

Power Sum problem

October 20, 2011 2 comments
Categories: Power Sums Tags: ,
%d bloggers like this: