Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statistical Challenge
#21
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
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#22
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.
Reply
#23
Of course!!! 0! = 1, according to factorial's definition

See it by yourself:

http://mathworld.wolfram.com/Factorial.html
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#24
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...
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#25
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...
Reply
#26
whats did i do wrong
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#27
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
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#28
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!)
Reply
#29
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.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#30
Wink lol
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)