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
···
--
Posted via http://www.ruby-forum.com/.
Just a general suggestion, try adding a few print statements to give
you a better idea of the internal state of the code. For example:
def factorial(x)
puts "factorial{#{x})"
. . .
def power(a,b)
puts "Entering power():"
p a, b
. . .
def add(k)
puts "#{Time.now}\tadd()"; p k
. . .
i=i+1
if (i==k) then
puts sum
. . .
else
p i
end
. . .
···
On Mon, Jul 27, 2009 at 2:07 PM, Prateek Agarwal<prateek.agwl@gmail.com> 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.
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
···
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
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
Thanks guys. I was able to solve the problem. Cheers.
···
--
Posted via http://www.ruby-forum.com/.