Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------------------------------------------
- -- This code is a basic assignment given to me in my CS 2300 Discrete Math course.
- -- Matthew K. Crandall, 11.28.2016
- -- I needed to write the function for Permutations in two ways:
- -- A long way, (the full formula)
- -- and a shortcut method to optimize things.
- -- This code is available at: YubBVqbu
- -- This code should be executable in just about any LUA interpreter...
- -- An online interpreter is located at: https://repl.it/languages/lua
- ------------------------------------------------------------------------------------------------------
- -- Factorial function, with a provided Start / Stop point
- -- To perform a true "factorial" function, provide either 1 or nil as the 2nd parameter.
- -- Parameters:
- -- <start> : The number to start performing factorial at.
- -- <stop> : The number to end performing factorial at. (nil or 1 to perform full factorial)
- function factorial(start, stop)
- local cachedStop = stop
- -- Create a variable to cache the stopping location. This is here in case nil is provided for stop.
- if not cachedStop or cachedStop <= 0 then
- cachedStop = 1
- end
- if start <= cachedStop then
- return 1
- else
- return start * factorial(start - 1, cachedStop)
- end
- end
- -- Calculates the shorthand permutation.
- function permutationShort(n, s)
- return factorial(n, n - s)
- end
- function permutationLong(n, s)
- return factorial(n) / factorial(n-s)
- end
- ------------------------------------------------------------------------------------------------------
- -- Testing my stuff....
- ------------------------------------------------------------------------------------------------------
- -- Test function for unit testing my factorial function.
- function testFactorial()
- print("Unit Testing Factorial: ")
- for i = 0, 10, 1 do
- print("input of: "..i.." Resulted in: "..factorial(i))
- end
- print("\r\nTest Complete.")
- end
- -- This function tests special cases with my special factorial function, with partial factorials.
- function testSpecialCases()
- print("Testing Special Partial Factorials")
- print("Input of: 20, 19 resulted in: "..factorial(20, 19))
- print("Input of: 5, 3 resulted in: "..factorial(5, 3))
- print("Input of: 2, 0 resulted in: "..factorial(2, 0))
- end
- -- Function to test permutations, with the long-cut and the shortcut.
- function testPermutations()
- print("Testing Permutations.")
- for i = 2, 8, 1 do
- for j = 1, i, 1 do
- --print("Inputs: n="..i..", s="..j.." - Short="..permutationShortVal.." | Long="..permutationLongVal)
- print("P("..i..", "..j..") : pshort="..permutationShort(i, j).." - plong="..permutationLong(i, j))
- end
- end
- end
- ------------------------------------------------------------------------------------------------------
- -- Main Code
- ------------------------------------------------------------------------------------------------------
- --testFactorial()
- --testSpecialCases()
- testPermutations()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement