# Help me with this program

**URL:** <https://www.ruby-forum.com/t/help-me-with-this-program/172768>\
**Category:** Ruby\
**Created:** [July 27, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/help-me-with-this-program/172768 "2009-07-27T20:07:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 27, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/help-me-with-this-program/172768/1 "2009-07-27T20:07:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 27, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/help-me-with-this-program/172768/2 "2009-07-27T20:46:37Z")

</div>

On Mon, Jul 27, 2009 at 2:07 PM, Prateek A.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

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  
. . .

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 27, 2009, 9:15pm UTC](https://www.ruby-forum.com/t/help-me-with-this-program/172768/3 "2009-07-27T21:15:54Z")

</div>

At 2009-07-27 02:07PM, “Prateek A.” 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](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

```

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 28, 2009, 9:21am UTC](https://www.ruby-forum.com/t/help-me-with-this-program/172768/4 "2009-07-28T09:21:05Z")

</div>

Thanks guys. I was able to solve the problem. Cheers.
