[bb] Permutations Function by Kevin_ [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : Permutations Function
Author : Kevin_
Posted : 1+ years ago

Description : A handy function that returns the number of different arrangements that are possible from a list of 'N' values.
Handy for random number generators, lottery-type programs and 'brute force' attacking.


Code :
Code (blitzbasic) Select
;
;   Non-Recursive Permutations Function By Prof
;
;                    Examples
;
;                3 will return 6
;                4 will return 24
;                5 will return 120 etc...
;
;
Graphics 640,480,32,2
SetBuffer BackBuffer()

N_Values=5                      ; Number of values
Perms=Permutations(N_Values)    ; Get the Number of permutations

Text 10,10,Str(Perms)+" possible arrangements (Permutations) of "+Str(N_Values)+" Values."
Flip
WaitKey()
End

; ***************************************************************

Function Permutations(N)
  ; Returns the number of Permutations of N Values
  ; N is an Int and must be greater than Zero otherwise Zero is returned
  ;
  If N>1
     Result=N*(N-1)
     For X=N-2 To 1 Step-1
         Result=Result*X
     Next
  ElseIf N=1
     Result=1
  Else
     Result=0
  EndIf
  Return Result
End Function

; ***************************************************************


Comments :


Diego(Posted 1+ years ago)

 It's nice. But think 0! := 1. To correct this, just change:ElseIf N=1intoElseIf N>=0