Advertisement
Machuga14

CS 2300 Permutation Example

Nov 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------
  2. -- This code is a basic assignment given to me in my CS 2300 Discrete Math course.
  3. -- Matthew K. Crandall, 11.28.2016
  4. -- I needed to write the function for Permutations in two ways:
  5. --    A long way, (the full formula)
  6. --    and a shortcut method to optimize things.
  7. -- This code is available at: YubBVqbu
  8. -- This code should be executable in just about any LUA interpreter...
  9. -- An online interpreter is located at: https://repl.it/languages/lua
  10. ------------------------------------------------------------------------------------------------------
  11.  
  12. -- Factorial function, with a provided Start / Stop point
  13. --   To perform a true "factorial" function, provide either 1 or nil as the 2nd parameter.
  14. -- Parameters:
  15. --     <start> : The number to start performing factorial at.
  16. --     <stop>  : The number to end performing factorial at. (nil or 1 to perform full factorial)
  17. function factorial(start, stop)
  18.     local cachedStop = stop
  19.  
  20.     -- Create a variable to cache the stopping location. This is here in case nil is provided for stop.
  21.     if not cachedStop or cachedStop <= 0 then
  22.         cachedStop = 1
  23.     end
  24.  
  25.     if start <= cachedStop then
  26.         return 1
  27.     else
  28.         return start * factorial(start - 1, cachedStop)
  29.     end
  30.  
  31. end
  32.  
  33. -- Calculates the shorthand permutation.
  34. function permutationShort(n, s)
  35.     return factorial(n, n - s)
  36. end
  37.  
  38. function permutationLong(n, s)
  39.     return factorial(n) / factorial(n-s)
  40. end
  41.  
  42. ------------------------------------------------------------------------------------------------------
  43. --      Testing my stuff....
  44. ------------------------------------------------------------------------------------------------------
  45.  
  46. -- Test function for unit testing my factorial function.
  47. function testFactorial()
  48.     print("Unit Testing Factorial: ")
  49.     for i = 0, 10, 1 do
  50.         print("input of: "..i.." Resulted in: "..factorial(i))
  51.     end
  52.     print("\r\nTest Complete.")
  53. end
  54.  
  55. -- This function tests special cases with my special factorial function, with partial factorials.
  56. function testSpecialCases()
  57.     print("Testing Special Partial Factorials")
  58.     print("Input of: 20, 19 resulted in: "..factorial(20, 19))
  59.     print("Input of: 5, 3 resulted in: "..factorial(5, 3))
  60.     print("Input of: 2, 0 resulted in: "..factorial(2, 0))
  61. end
  62.  
  63. -- Function to test permutations, with the long-cut and the shortcut.
  64. function testPermutations()
  65.     print("Testing Permutations.")
  66.    
  67.     for i = 2, 8, 1 do
  68.         for j = 1, i, 1 do
  69.             --print("Inputs: n="..i..", s="..j.." - Short="..permutationShortVal.." | Long="..permutationLongVal)
  70.             print("P("..i..", "..j..") : pshort="..permutationShort(i, j).." - plong="..permutationLong(i, j))
  71.         end
  72.     end
  73. end
  74.  
  75. ------------------------------------------------------------------------------------------------------
  76. -- Main Code
  77. ------------------------------------------------------------------------------------------------------
  78.  
  79. --testFactorial()
  80. --testSpecialCases()
  81. testPermutations()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement