From: serialhex Date: 2012-02-19T13:03:08+09:00 Subject: Re: Factorial --f46d04016c1b419cd504b94941f9 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Fri, Feb 17, 2012 at 12:12 PM, Tony Arcieri wrot= e: > On Fri, Feb 17, 2012 at 1:35 AM, Junayeed Ahnaf Nirjhor < > zombiegenerator@aol.com> wrote: > > Just precalculate all the factorials and put them in Mongodb. > > -- > Tony Arcieri > this gives pretty good performance: @stat =3D [] def fact n n =3D=3D 0 ? 1 : @stat[n] ||=3D n * fact(n-1) end though for big numbers ( > 10k) you have to go up by multiples of 5k in order to not have a stack overflow... in that case you can use the inject method given above... and if you then have a *huge* table, you can stick it into something like mongodb... increase complexity only when needed. hex --=20 * my blog is cooler than yours: http://serialhex.github.com * The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience." * As a programmer, it is your job to put yourself out of business. What you do today can be automated tomorrow. ~Doug McIlroy No snowflake in an avalanche ever feels responsible. --- CFO: =93What happens if we train people and they leave?=94 CTO: =93What if we don=92t and they stay?=94 --f46d04016c1b419cd504b94941f9 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On Fri, Feb 17, 2012 at 12:12 PM, Tony Arcieri <tony.arcieri@gmail.com> w= rote:
On Fri, Feb 17, 2012 at 1:35 AM, Junayeed Ahnaf Nirjhor &= lt;
zombiegenerator@aol.com> = wrote:

Just precalculate all the factorials and put them in Mongodb.

--
Tony Arcieri

this gives pretty good perf= ormance:

@stat =3D []
def fact n
=A0 n =3D=3D 0 ? 1 : @stat[n] ||=3D n * fact(n-1)
end

though for big numbers ( > 10k) you have to go up by multiple= s of 5k in order to not have a stack overflow... =A0in that case you can us= e the inject method given above... =A0and if you then have a *huge* table, = you can stick it into something like mongodb... =A0increase complexity only= when needed.
hex


--
* =A0my blog is coole= r than yours: htt= p://serialhex.github.com
* =A0The wise man said: "Never argue w= ith an idiot. They bring you down to their level and beat you with experien= ce."
* =A0As a programmer, it is your job to put yourself out of business. What = you do today can be automated tomorrow. ~Doug McIlroy
No snowflake in an= avalanche ever feels responsible.=A0
---
CFO: =93What happens if we = train people and they leave?=94
CTO: =93What if we don=92t and they stay?=94
--f46d04016c1b419cd504b94941f9--