[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: factorial in ruby

Gary Thomas

4/18/2007 4:38:00 AM

For large enough n it will also overflow the number. Probably the most
efficient way would be to use a loop up to some (not very large) number, to
use Stirling's approximation or its extension up to some larger number, and
throw an overfow exception for anything larger than that.

Cheers

Gary Thomas

> -----Original Message-----
> From: David Simas [mailto:dsimas@imageworks.com]
> Sent: Tuesday, 17 April 2007 6:58 a.m.
> To: ruby-talk ML
> Subject: Re: factorial in ruby
>
>
> On Tue, Apr 17, 2007 at 02:56:16AM +0900, Jason Roelofs wrote:
> > No and most likely not.
> >
> > def fact(n)
> > if n == 0
> > 1
> > else
> > n * fact(n-1)
> > end
> > end
>
> For large enough n, this will overflow the stack. Since Ruby doesn't
> optimize tail-recursive functions (and the above isn't tail recursive,
> anyway), you'd better write this function as a loop (left as an exercise).
>
> DGS
>
> >
> > Jason
> >
> > On 4/16/07, Trans <transfire@gmail.com> wrote:
> > >
> > >Is factorial defined anywhere in Ruby's core or standard library. If
> > >not will it be in 1.9+?
> > >
> > >Thanks,
> > >T.
> > >
> > >
> > >
>
>
>



2 Answers

Amos King

4/18/2007 3:35:00 PM

0

make it non-recursive use

class Integer
def fact
return 1 if self == 0
(1..self).inject { |i,j| i*j }
end
end

Then you can just call
120.fact and that will return the factorial of 120 with no overflow.
You may want to add in a check for negatives

On 4/17/07, Gary Thomas <gthomas@takaro.co.nz> wrote:
> For large enough n it will also overflow the number. Probably the most
> efficient way would be to use a loop up to some (not very large) number, to
> use Stirling's approximation or its extension up to some larger number, and
> throw an overfow exception for anything larger than that.
>
> Cheers
>
> Gary Thomas
>
> > -----Original Message-----
> > From: David Simas [mailto:dsimas@imageworks.com]
> > Sent: Tuesday, 17 April 2007 6:58 a.m.
> > To: ruby-talk ML
> > Subject: Re: factorial in ruby
> >
> >
> > On Tue, Apr 17, 2007 at 02:56:16AM +0900, Jason Roelofs wrote:
> > > No and most likely not.
> > >
> > > def fact(n)
> > > if n == 0
> > > 1
> > > else
> > > n * fact(n-1)
> > > end
> > > end
> >
> > For large enough n, this will overflow the stack. Since Ruby doesn't
> > optimize tail-recursive functions (and the above isn't tail recursive,
> > anyway), you'd better write this function as a loop (left as an exercise).
> >
> > DGS
> >
> > >
> > > Jason
> > >
> > > On 4/16/07, Trans <transfire@gmail.com> wrote:
> > > >
> > > >Is factorial defined anywhere in Ruby's core or standard library. If
> > > >not will it be in 1.9+?
> > > >
> > > >Thanks,
> > > >T.
> > > >
> > > >
> > > >
> >
> >
> >
>
>
>
>


--
Amos King
Ramped Media
USPS
Programmer/Analyst
St. Louis, MO
Looking for something to do? Visit ImThere.com

bw

9/1/2007 7:34:00 PM

0

On Sat, 01 Sep 2007 18:03:55 GMT, David Johnston <david@block.net>
wrote:

>On Sat, 01 Sep 2007 17:11:44 GMT, bw@barrk.net (B1ackwater) wrote:
>
>>On Fri, 31 Aug 2007 02:29:18 GMT, 3735 Dead
>><zepp22113735@finestplanet.com> wrote:
>>
>>>On Fri, 31 Aug 2007 20:36:03 -0400, B1ackwater <bw@barrk.net> wrote:
>>>
>>>>3735 Dead <zeppp@finestplanet.com> wrote:
>>>>
>>>>>On Thu, 30 Aug 2007 22:26:05 GMT, bw@barrk.net (B1ackwater) wrote:
>>>>>
>>>>>>On Thu, 30 Aug 2007 18:20:20 -0400, "Kommienezuspadt"
>>>>>><NoSpam@NoThanks.net> wrote:
>>>>>>
>>>>>>>Go look up how long passed between his arrest & the news getting out -- then
>>>>>>>smack yourself on the forehead.
>>>>>>
>>>>>>
>>>>>> I'd suggest you do a little archive-surfing yourself.
>>>>>>
>>>>>> There were TWO "big news" events ... one when he was
>>>>>> first arrested on the "scandalous" charge and again
>>>>>> after his shortsighted plea deal.
>>>>>
>>>>>And neither got out. Until the other day, when Rollcall checked on a
>>>>>tip.
>>>>
>>>> Nonsense ... the local news people had been after this
>>>> from the beginning, it's just that the national news
>>>> didn't really start to hype the story until just a
>>>> few days ago.
>>>
>>>Still trying to pretend the national media knew about the story before
>>>Monday?
>>
>> "Knew" ? Yes.
>>
>>>OK: so explain why they would hype it NOW? I mean, supposing this
>>>media is librul, which by itself requires a large stretch of the
>>>imagination.
>>
>> It's a good idea to have *eomething* to work with before
>> a highly-visible news corporation goes after a US senator.
>> Craig provided that "something" with his plea deal - the
>> appearance of impropriety.
>
>The plea happened 20 days before the story broke.
>
>>
>>>Gonzales just quit, and that was an unmitigated disaster for Putsch.
>>>Why would the "librul media" want to detract from that ?
>>
>> Actually, I was wondering about that. It wasn't just Craig, it
>> was also Vick that day. CNN was "Vick this, Vick that, Craig
>> this, Craig that ... and, oh yea, the AG resigned today ...
>> now back to Vick, Vick, Vick, Craig, Craig, Craig........".
>> It was very weird, a total flip-flop from what would normally
>> be considered "real", important news.
>
>Gonzales was old news by that point. H

"Old news" ??? His resignation wasn't ... and, after "W"s
constant backing, it was a rather sudden 180 turn. It was
looking as if his inquisitors had already thrown their
best punches and Gonzales had survived. Finally, as he was
an "important" person at the top ranks of government and
power, you'd THINK the Vick and Craig stories would have
been pushed well into the background.

But Noooo ... the juicy "scandals" ruled the airwaves.


>>>The Justice
>>>Department today announced they were opening a perjury investigation
>>>against the former A-G. Think Faux News broke away from the Swirley
>>>Senator story for that?
>>
>> Did it ? I have doubts. Maybe for five seconds ...
>>
>>>It came at a time when both the Pentagon and the GAO were making a
>>>farce of the right wing propaganda that the surge was working, or that
>>>benchmarks were being met.
>>
>> Which, in a Machiavellian interpretation, would mean Vick,
>> Craig and maybe Gonzales were pre-planned "distractions"
>> designed to manipulate the media by exploiting its main
>> weakness - the tendency to go "yellow" whenever there's
>> a tawdry 'scandal' to hype.
>>
>> Hmmm ... I'll have to check into Craigs voting record.
>> Was he moving towards opposing the Iraq war or in some
>> way threatening Halliburton/M-IC profiteering ? If so,
>> he may have become "expendable",
>
>No. The only thing he was doing in that regard was pushing for more
>funding for Veteran's Affairs.

Sounds like money not going into Halliburtons pocket :-)

Besides, veterans have always been disposable, not just
in the USA but elsewhere too. Use 'em up - throw 'em away.
The money and attention always goes towards NEW soldiers.

The closest the USA ever got to doing veterans right came
at the end of WW-2 ... and that was only because there were
so MANY of them and because we were flush with cash, being
the only un-blasted industrial power remaining. Korean vets
began to get the short end of the stick again and by
Vietnam, well, we all know about that .....