Posts: 6,419
Threads: 74
Joined: Mar 2002
Code:
FUNCTION Factorial& (n&)
IF n& < 0 THEN
ERROR 18 ' Function not defined.
ELSEIF n& = 0 THEN
Factorial& = 1
ELSE
Factorial& = Factorial&(n& - 1) * n&
END IF
END FUNCTION
Posts: 3,368
Threads: 195
Joined: Jan 2003
0! != 1!
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
Of course!!! 0! = 1, according to factorial's definition
See it by yourself:
http://mathworld.wolfram.com/Factorial.html
Posts: 2,765
Threads: 138
Joined: Nov 2002
Code:
CLS
DEFDBL F, I
INPUT n
f = 1
FOR I = 1 TO n
f = I * f
NEXT I
z$ = LEFT$(LTRIM$(STR$(n)), 1)
FOR c = 0 TO 9
c$ = LTRIM$(STR$(c))
IF z$ = c$ THEN EXIT FOR
IF c = 9 THEN PRINT "Null": END
NEXT c
PRINT f
It goes up to 18!
Is it ok?
Eidt: If you can read Scientific Notation... It goes up to 170!
Edit: I made a mistake... Try it again...
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 3,343
Threads: 83
Joined: Mar 2003
whitetiger: you made another mistake, but not in the code...
Na_th_an's looks good, but I'll check it when I boot into my windows partition.
Whitetiger's looks too complicated for me to decide exactly what it does but I will check that too.
Nath: I'm wondering if instead of returning an error (and therefore stuffing up the whole prog) it should just return 0 or something... keeping in mind my next mini-challenge in this thread is going to be the combination and permutation forumla...
Posts: 2,765
Threads: 138
Joined: Nov 2002
whats did i do wrong
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Quote:Nath: I'm wondering if instead of returning an error (and therefore stuffing up the whole prog) it should just return 0 or something
Definitely not zero. The factorial of a negative number is NOT DEFINED, so we better find a representation for "NOT A NUMBER" (NAN). As QB doesn't suport NAN natively, I inserted that error. But we could set NAN as the maximum or the minimum long integer in range, and trap the exit so if it is that value, then it is considered NAN. But that would involve making a whole math library LOL
.
And be sure my implementation works. It is just a QB translation of the recursive definition of factorial (as in maths):
Compare that formula with my code
Yo can do it iteratively also:
Code:
FUNCTION IterativeFactorial&(n&)
IF n&<0 THEN ERROR 18
fact&=1
FOR i&=1 TO n&
fact& = fact& * i&
NEXT i&
IterativeFactorial& = fact&
END FUNCTION
Posts: 3,343
Threads: 83
Joined: Mar 2003
I'll check your new method when I get home from school. I can't really read the formula, but I'll try later, but for the moment you are de-facto winner.
whitetiger: eidt
Everyone who sees this before I get home: Take na_th_an's code and make both a permutation and a combination function. Go searching on google if you don't know the formula.
----
edit
----
I'm home. na_th_an's factorial program is the current winner, but it can be improved so the contest is not over. Hurry up, guys!
ps: nath: your prog loses out because it can only do up to 12! (hint, hint!)
Posts: 6,419
Threads: 74
Joined: Mar 2002
Quote:ps: nath: your prog loses out because it can only do up to 12! (hint, hint!)
If you want more, just use Neo's long int library. 12! is your top in integer math at least you do some tricks.
You can use the DOUBLE data type, this way you can calculate up to 170!, but you are losing lots of ciphers and precission (QB just callculates a power so it fills with trailing zeroes in floating point).
Code:
FUNCTION fact# (n#)
f# = 1
FOR i# = 1 TO n#
f# = f# * i#
NEXT i#
fact# = f#
END FUNCTION
I'd stick to the integer sollution. 12! is not a very big number, but it is enough for most caluclations. If you need bigger numbers, you better use neo's code.
Posts: 1,845
Threads: 44
Joined: Aug 2002
lol