From: Glenn Jackman Date: 2009-07-28T04:15:08+09:00 Subject: Re: Help me with this program At 2009-07-27 02:07PM, "Prateek Agarwal" wrote: > Woah, this is my 3rd post in like an hour. Must be really annoying for > the guys who are answering. > Well, in this program after inputting the value the program doesn't > respond or do anything. On pressing 'ctrl+c', it shows some 'interrupt' > error. > > Thanks for your help. I am still in the very basic stage of ruby. > > Attachments: > http://www.ruby-forum.com/attachment/3907/Question1.rb You'd better not use k == 0. You'll hit an infinite loop in add() You might want to use: if i >= k then ... Add more trace statements while testing. Here's a version without the "loop" statements def factorial(x) result = 1 x.downto(2) {|i| result *= i} result # or: # (1..x).inject(1, :*) end def power(a,b) result = 1 b.times {result *= a} result # or simply: # a ** b end def add(k) sum = 0.0 k.times do |i| n1 = factorial(4*i) n2 = 1103+(26390*i) d1 = power(factorial(i),4) d2 = power(396,(4*i)) sum += 1.0*(n1*n2)/(d1*d2) end sum end -- Glenn Jackman Write a wise saying and your name will live forever. -- Anonymous