# Why does mathn kill this method?

**URL:** <https://www.ruby-forum.com/t/why-does-mathn-kill-this-method/164404>\
**Category:** Ruby\
**Created:** [April 1, 2009, 12:46am UTC](https://www.ruby-forum.com/t/why-does-mathn-kill-this-method/164404 "2009-04-01T00:46:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [April 1, 2009, 12:46am UTC](https://www.ruby-forum.com/t/why-does-mathn-kill-this-method/164404/1 "2009-04-01T00:46:41Z")

</div>

Really stumped here.

def sum\_digits(n)  
sum = 0  
while n\>0  
sum += n % 10  
n /= 10  
end  
sum  
end

STDOUT.sync=true

puts sum\_digits(12) #=\> 3

require ‘mathn’  
puts sum\_digits(12) # hangs

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [April 1, 2009, 1:08am UTC](https://www.ruby-forum.com/t/why-does-mathn-kill-this-method/164404/2 "2009-04-01T01:08:00Z")

</div>

On Tue, Mar 31, 2009 at 3:46 PM, Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> STDOUT.sync=true
> 
> puts sum\_digits(12) #=\> 3
> 
> require ‘mathn’  
> puts sum\_digits(12) # hangs
> 
> What am I doing wrong?

This should illustrate the problem:

irb(main):001:0\> require ‘mathn’  
=\> true  
irb(main):002:0\> n = 10  
=\> 10  
irb(main):003:0\> n /= 10  
=\> 1  
irb(main):004:0\> n /= 10  
=\> 1/10  
irb(main):005:0\> n /= 10  
=\> 1/100

The solution:

def sum\_digits(n)  
sum = 0  
while n\>0  
sum += n % 10  
n = n.div 10  
end  
sum  
end

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [April 1, 2009, 1:09am UTC](https://www.ruby-forum.com/t/why-does-mathn-kill-this-method/164404/3 "2009-04-01T01:09:10Z")

</div>

On Wed, Apr 1, 2009 at 4:16 AM, Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> STDOUT.sync=true
> 
> puts sum\_digits(12) #=\> 3
> 
> require ‘mathn’  
> puts sum\_digits(12) # hangs

n /= 10 just keeps making n into a smaller and smaller rational. it  
never reaches 0

martin

---

<div class="post-metadata">

**Author:** ![steenslag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@steenslag](https://www.ruby-forum.com/u/steenslag)\
**Post date:** [April 1, 2009, 1:16am UTC](https://www.ruby-forum.com/t/why-does-mathn-kill-this-method/164404/4 "2009-04-01T01:16:08Z")

</div>

Martin DeMello wrote:

> On Wed, Apr 1, 2009 at 4:16 AM, Siep K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > STDOUT.sync=true
> > 
> > puts sum\_digits(12) #=\> 3
> > 
> > require ‘mathn’  
> > puts sum\_digits(12) # hangs
> 
> n /= 10 just keeps making n into a smaller and smaller rational. it  
> never reaches 0
> 
> martin

Yes, that’s clear. Thanks for the help (real fast), Christopher and  
Martin.

Siep
