Full Version: Statistical Challenge
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
Right, this should probably be on my site when it's up, but here goes: Create a function that calculates factorials. It should give the correct result for 0! and an appropriate result (null or 0 perhaps) for a non natural number. It should use the least amount of code as possible.
Here's something to start us off.

FUNCTION factorial# (n#)
IF n# < 3 THEN factorial# = n# ELSE factorial# = n# * factorial#(n# - 1)


Here's a recursive one. (note that starting from 1 and going up to N is less efficient because it multiplies by 1 and by 2!!

FUNCTION factorial2# (n#)
I# = n#: temp# = 1
temp# = temp# * I#
I# = I# - 1: LOOP
factorial2# = temp# * I#

Now a for loop. But it still has an IF, except it is built in AFAIK.

FUNCTION factorial3# (N#)
temp# = 1
FOR I# = N# TO 3 STEP -1
temp# = temp# * I#
factorial3# = temp# * I#
Thank goodness someone replied... Smile

Right, all three of your functions fail the challenge instantly, because I don't think you read the instructions correctly. They all give incorrect values for non-natural numbers, and for zero. But at least they are a start.

I think the function should use the long integer type rather than the double type too. Any problems with that?

I am not going to put a due date on this yet, because there is not much interest, but if interest picks up start coding quick!
When I try to do factorial for anything on my calculator less than 1, I get "invalid input function"

0 is a good result to be "invalid input function" I think.................................

EDIT: and a double can be bigger than a long int, too!

Oh and besides, non-natural numbers would make it about 4 times as long.
0! = 1
Thank you na_th_an, someone actually knows the special case.

Quote:When I try to do factorial for anything on my calculator less than 1, I get "invalid input function"

Do some research on the properties of factorials. To make this challenge serious, I don't want anyone else to submit any information about the properties of factorials. If you want to compete, find out yourself and write the appropriate code. Telling everyone here what the properties of factorials are would be a little silly if you want to win, wouldn't it?

The only hint you have is that I am talking about mathematical factorials (not some other unheard of factorial), we are working in base 10 and that, as na_th_an said, 0! = 1.
Hmph! Well my function works great for whole real numbers in the set

Like I say, check out the properties of factorials. You can easily win this, you just need to add code for a couple of special cases.
And why do you need it? :*)
Check out BIGINT (see in project section).

BIGINT was able to calculate everything up to and including 3000! on my computer.

That's quite a number! Tongue Lol

And moreover, it did it in just 230 seconds!!!
Pages: 1 2 3 4 5 6