Posts: 3,343
Threads: 83
Joined: Mar 2003
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.
Posts: 3,368
Threads: 195
Joined: Jan 2003
Here's something to start us off.
Code:
FUNCTION factorial# (n#)
IF n# < 3 THEN factorial# = n# ELSE factorial# = n# * factorial#(n# - 1)
END FUNCTION
(edit)
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!!
Code:
FUNCTION factorial2# (n#)
I# = n#: temp# = 1
DO
IF I# < 3 THEN EXIT DO
temp# = temp# * I#
I# = I# - 1: LOOP
factorial2# = temp# * I#
END FUNCTION
Now a for loop. But it still has an IF, except it is built in AFAIK.
Code:
FUNCTION factorial3# (N#)
temp# = 1
FOR I# = N# TO 3 STEP -1
temp# = temp# * I#
NEXT I#
factorial3# = temp# * I#
END FUNCTION
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,343
Threads: 83
Joined: Mar 2003
Thank goodness someone replied...
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!
Posts: 3,368
Threads: 195
Joined: Jan 2003
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!
EDIT:
Oh and besides, non-natural numbers would make it about 4 times as long.
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Posts: 3,343
Threads: 83
Joined: Mar 2003
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.
Posts: 3,368
Threads: 195
Joined: Jan 2003
Hmph! Well my function works great for whole real numbers in the set
{1...infinity}
:|
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,343
Threads: 83
Joined: Mar 2003
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.
Posts: 3,288
Threads: 167
Joined: Nov 2001
And why do you need it? :*)
Posts: 1,845
Threads: 44
Joined: Aug 2002
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!
Lol
And moreover, it did it in just 230 seconds!!!