How to resolve the algorithm Combinations and permutations step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Combinations and permutations step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
Implement the combination (nCk) and permutation (nPk) operators in the target language:
See the Wikipedia articles for a more detailed description. To test, generate and print examples of:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Combinations and permutations step by step in the FreeBASIC programming language
Source code in the freebasic programming language
Function PermBig(x As Long, y As Long) As ULongint
Dim As Long i
Dim As Longint z = 1
For i = x - y + 1 To x
z = z * i
Next i
Return (z)
End Function
Function FactBig(x As Long) As ULongint
Dim As Long i
Dim As Longint z = 1
For i = 2 To x
z = z * i
Next i
Return (z)
End Function
Function CombBig(Byval x As Long, Byval y As Long) As Double
If y > x Then
Return (0)
Elseif x = y Then
Return (1)
Else
If x - y < y Then y = x - y
Return (PermBig(x, y) / FactBig(y))
End If
End Function
Dim As Long i, j
Print "-- Long Integer - Permutations - from 1 to 12"
For i = 1 To 12
For j = 1 To i
Print "P(" & i & "," & j & ")=" & Str(PermBig(i, j)) & " ";
Next j
Print ""
Next i
Print Chr(10) & "-- Float integer - Combinations from 10 to 60"
For i = 10 To 60 Step 10
For j = 1 To i Step i \ 5
Print "C(" & i & "," & j & ")=" & Str(CombBig(i, j)) & " ";
Next j
Print ""
Next i
Print Chr(10) & "-- Float integer - Permutations from 5000 to 15000"
For i = 5000 To 15000 Step 5000
For j = 10 To 50 Step 20
Print "P(" & i & "," & j & ")=" & Str(PermBig(i, j)) & " ";
Next j
Print ""
Next i
Print Chr(10) & "-- Float integer - Combinations from 200 to 1000"
For i = 200 To 1000 Step 200
For j = 20 To 100 Step 20
Print "C(" & i & "," & j & ")=" & Str(CombBig(i, j)) & " ";
Next j
Print ""
Next i
Sleep
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Haskell programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Mian-Chowla sequence step by step in the Arturo programming language
You may also check:How to resolve the algorithm XML/Output step by step in the NetRexx programming language
You may also check:How to resolve the algorithm System time step by step in the Hoon programming language