Qbasicnews.com

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
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
0! != 1!
Of course!!! 0! = 1, according to factorial's definition

See it by yourself:

http://mathworld.wolfram.com/Factorial.html
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...
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...
whats did i do wrong
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 Big Grin.

And be sure my implementation works. It is just a QB translation of the recursive definition of factorial (as in maths):

[Image: fimg64.gif]

Compare that formula with my code Wink

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
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!)
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.
Wink lol
Pages: 1 2 3 4 5 6