Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statistical Challenge
#1
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.
Reply
#2
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.
Reply
#3
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!
Reply
#4
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.
Reply
#5
0! = 1
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#6
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.
Reply
#7
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.
Reply
#8
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.
Reply
#9
And why do you need it? :*)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#10
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!!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)