Advertisement
Romul81

combinatorics

Apr 20th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Combinations and permutations.
  2. // Counts can be verified at http://www.calculatorsoup.com/calculators/discretemathematics/permutations.php
  3. // noprotect
  4.  
  5. 'use strict'
  6.  
  7. const combinations = function (arr) {
  8.   const result = []
  9.  
  10.   for (let i = 0; i < arr.length; i++) {
  11.     const item1 = arr[i]
  12.  
  13.     for (let j = i + 1; j < arr.length; j++) {
  14.       const item2 = arr[j]
  15.  
  16.       result.push([item1, item2])
  17.     }
  18.   }
  19.  
  20.   return result
  21. }
  22.  
  23. const permutations = function (arr) {
  24.   const result = []
  25.  
  26.   for (let i = 0; i < arr.length; i++) {
  27.     const item1 = arr[i]
  28.  
  29.     for (let j = 0; j < arr.length; j++) {
  30.       const item2 = arr[j]
  31.  
  32.       if (item1 !== item2) {
  33.         result.push([item1, item2])
  34.       }
  35.     }
  36.   }
  37.  
  38.   return result
  39. }
  40.  
  41. const combinationsn = function (arr, n) {
  42.   const result = []
  43.  
  44.   const indexes = []
  45.   for (let i = 0; i < n; i++) {
  46.     indexes.push(i)
  47.   }
  48.  
  49.   let i = 0
  50.   while (i < arr.length) {
  51.     const items = []
  52.  
  53.     for (let j = 0; j < indexes.length; j++) {
  54.       const index = indexes[j]
  55.       items.push(arr[index])
  56.     }
  57.  
  58.     let idx = indexes.length - 1
  59.     let lastIndex = indexes[idx]
  60.     while (lastIndex === arr.length - 1 && idx > 0) {
  61.       idx--
  62.       lastIndex = indexes[idx]
  63.     }
  64.  
  65.     if (indexes[idx] < arr.length - 1) {
  66.       indexes[idx]++
  67.  
  68.       let count = 0
  69.       for (let k = idx + 1; k < indexes.length; k++) {
  70.         indexes[k] = indexes[idx + count] + 1
  71.         if (indexes[k] >= arr.length) {
  72.           indexes[k] = arr.length - 1
  73.         }
  74.         count++
  75.       }
  76.  
  77.       i = 0
  78.     }
  79.  
  80.     let skip = false
  81.     for (let k = 0; k < items.length - 1; k++) {
  82.       for (let l = k + 1; l < items.length; l++) {
  83.         if (items[k] === items[l]) {
  84.           skip = true
  85.           break
  86.         }
  87.       }
  88.     }
  89.  
  90.     if (n === 1) {
  91.       for (let k = 0; k < result.length; k++) {
  92.         if (result[k][0] === items[0]) {
  93.           skip = true
  94.           break
  95.         }
  96.       }
  97.     }
  98.  
  99.     if (!skip) {
  100.       result.push(items)
  101.     }
  102.  
  103.     i++
  104.   }
  105.  
  106.   return result
  107. }
  108.  
  109. const permutationsn = function (arr, n) {
  110.   const result = []
  111.  
  112.   const indexes = []
  113.   for (let i = 0; i < n; i++) {
  114.     indexes.push(i)
  115.   }
  116.  
  117.   let i = 0
  118.   while (i < arr.length) {
  119.     const items = []
  120.  
  121.     for (let j = 0; j < indexes.length; j++) {
  122.       const index = indexes[j]
  123.       items.push(arr[index])
  124.     }
  125.  
  126.     let idx = indexes.length - 1
  127.     let lastIndex = indexes[idx]
  128.     while (lastIndex === arr.length - 1 && idx > 0) {
  129.       idx--
  130.       lastIndex = indexes[idx]
  131.     }
  132.  
  133.     if (indexes[idx] < arr.length - 1) {
  134.       indexes[idx]++
  135.  
  136.       for (let k = idx + 1; k < indexes.length; k++) {
  137.         indexes[k] = 0
  138.       }
  139.  
  140.       i = 0
  141.     }
  142.  
  143.     let skip = false
  144.     for (let k = 0; k < items.length - 1; k++) {
  145.       for (let l = k + 1; l < items.length; l++) {
  146.         if (items[k] === items[l]) {
  147.           skip = true
  148.           break
  149.         }
  150.       }
  151.     }
  152.  
  153.     if (n === 1) {
  154.       for (let k = 0; k < result.length; k++) {
  155.         if (result[k][0] === items[0]) {
  156.           skip = true
  157.           break
  158.         }
  159.       }
  160.     }
  161.  
  162.     if (!skip) {
  163.       result.push(items)
  164.     }
  165.  
  166.     i++
  167.   }
  168.  
  169.   return result
  170. }
  171.  
  172. const factorial = function (n) {
  173.   let result = 1
  174.  
  175.   for (let i = n; i > 0; i--) {
  176.     result *= i
  177.   }
  178.  
  179.   return result
  180. }
  181.  
  182. const combinationCount = function (n, k) {
  183.   let result = 1
  184.  
  185.   let divisor = factorial(k) * factorial(n - k)
  186.   if (divisor) {
  187.     result = factorial(n) / divisor
  188.   }
  189.  
  190.   return result
  191. }
  192.  
  193. const permutationCount = function (n, k) {
  194.   let result = 1
  195.  
  196.   let divisor = factorial(n - k)
  197.   if (divisor) {
  198.     result = factorial(n) / divisor
  199.   }
  200.  
  201.   return result
  202. }
  203.  
  204. const list = [1, 2, 3, 4, 5, 6, 7, 8]
  205. const c = combinations(list)
  206. console.log(c.length + ' combinations.')
  207. console.log(c)
  208.  
  209. const p = permutations(list)
  210. console.log(p.length + ' permutations.')
  211. console.log(p)
  212.  
  213. for (let i = 1; i <= list.length; i++) {
  214.   console.log(combinationsn(list, i).length === combinationCount(list.length, i))
  215. }
  216.  
  217. for (let i = 1; i <= list.length; i++) {
  218.   console.log(permutationsn(list, i).length === permutationCount(list.length, i))
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement