This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2013-03-24 19:50:08

GaboAlvarez
New Scratcher
Registered: 2013-03-24
Posts: 3

Factorial of a number

Hello.
How I can calculate the factorial of a number?
Please, helpe me  smile
Thanks

Offline

 

#2 2013-03-24 20:07:22

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Factorial of a number

This should approximate factorials:

Code:

e^(n*ln(n) - n + (ln(2*π*n))/2)
or
e^(n*ln(n) - n + ln(√(2*π*n)))

To find out the exact, set the "inputNumber" variable to the number who's factorial you want to find, run the code below, and the answer should be in the "result" variable.  This should work for all whole numbers.

set [result v] to (1)
set [loop v] to (1)
repeat ((inputNumber)-(1))
change [loop v] by (1)
set [result v] to ((result)*(loop))
end

Last edited by MoreGamesNow (2013-03-24 20:08:52)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#3 2013-03-24 20:23:50

Hyperbola
Scratcher
Registered: 2013-03-15
Posts: 100+

Re: Factorial of a number

I actually found a really cool factorial calculator a while ago: http://scratch.mit.edu/projects/Molybdenum/2630470


Tip of the whatever: Don't post in threads older than 2 weeks unless your post will actually be useful.
It's the last day of 1.4! *cries* (quote from NeilWest, rest by me) by the time you read this it probably will be 2.0  sad

Offline

 

#4 2013-03-25 07:45:46

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Factorial of a number

Hyperbola wrote:

I actually found a really cool factorial calculator a while ago: http://scratch.mit.edu/projects/Molybdenum/2630470

If you'll be calculating large factorials (i.e. factorials that are so large that Scratch would normally report them as "infinity") I'd go with Molybdenum's project.  The script above is simpler, but, given the maximum size of Scratch variables, has its limits.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2013-03-25 15:21:47

GaboAlvarez
New Scratcher
Registered: 2013-03-24
Posts: 3

Re: Factorial of a number

Thank you so much  smile  it works  big_smile

Offline

 

Board footer