Menu

#101 Permutations, Combinations, and Factorials.

closed
nobody
None
5
2007-03-01
2007-02-11
Anonymous
No

(Note, the same request is in the "Support" board. It was put there on accident, and can be deleted.

I request a function that allows use of Permutations (nPr), Combinations (nCr), and Factorials (!). When I say "!" I dont mean the C++ version of NOT, but factorials(ex: 5! = 5 * 4 * 3 * 2 * 1).

The equation for nPr is:
(n!) / (n-r)!

and nCr is:
nCr = n!/(r!.(n-r)!)

I kludged together a permutation calc in SmallBASIC on my Palm, so heres the source code. (Sorry I didnt port it to C or C++, I don't have alot of time.)

Yes I used GOTO. You can kill me now.

To contact me my E-Mail is zoasterboy@msn.com

[codez]

1 CLS
CONST FALSE = 0
CONST TRUE = NOT FALSE
DIM TOTAL(0)

p = 0

PRINT "Permutation Calc v1.0"
PRINT ""
INPUT "N"; N
INPUT "R"; R
PRINT ""
'Error traps
IF N < 0 OR R < 0 THEN
PRINT"You can't use negative numbers"
PAUSE
GOTO 1
ELSEIF R > N THEN
PRINT"R can't be larger than N"
PAUSE
GOTO 1
ELSEIF N = 0 THEN
PRINT "N can't be 0"
PAUSE
GOTO 1
ELSEIF N > 150 THEN
PRINT "The maximum number is 150"
PAUSE
GOTO 1
ENDIF
r1 = R
R = N - R

factorial, N 'This sends N to the factorial SUB
2
p = p + 1
IF p = 1 THEN 'this assigns N! to t1 and
t1 = TOTAL(0) 'sends R to the factorial SUB
factorial, R
ELSE
t2 = TOTAL(0)
ENDIF

SUB factorial(x)'This factorializes the
'Argument passed to it and
'Assigns it to TOTAL(0)
a = x - 2
t = x * (x - 1)
WHILE a > 2
t = t * a
a = a - 1
IF a = 2 THEN t = t * 2
WEND
IF x = 1 OR x = 2 then t = x
TOTAL(0) = t
GOTO 2

END SUB

PRINT "N! = "; t1 'prints equation and total
PRINT "/"
PRINT "(N - R)! = "; t2
PRINT "="
IF N = r1 THEN
COLOR 9
PRINT t1
ELSE
COLOR 9
PRINT t1/t2
ENDIF
PRINT""

PAUSE
COLOR 0
INPUT "Type 1 to go again. Enter to exit."; choice
IF choice = 1 then
GOTO 1
ELSE
END
ENDIF

[/codez]

Discussion

  • Ton van Overbeek

    Logged In: YES
    user_id=147684
    Originator: NO

    All three functions already exist in EasyCalc:
    fact(), ncr() and npr().
    Go to Scientific mode, tap on the S (to the right of the input field) and select
    Combinatorics. You can also use the up and down buttons (up and down on the
    fiveway navigator) to cycle through the various key layouts for scientific mode.
    These functions have been part of Easycalc at least since version 1.08.

     
  • Ton van Overbeek

    • status: open --> closed
     

Log in to post a comment.

MongoDB Logo MongoDB