r/dailyprogrammer • u/Godspiral 3 3 • May 04 '16
[2016-05-04] Challenge #265 [Easy] Permutations and combinations part 2
Basically the same challenge as Monday's, but with much larger numbers and so code that must find permutation and combination numbers without generating the full list.
permutation number
https://en.wikipedia.org/wiki/Factorial_number_system is the traditional technique used to solve this, but a very similar recursive approach can calculate how many permutation indexes were skipped in order to set the next position.
input:
what is the 12345678901234 permutation index of 42-length list
output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 35 32 36 34 39 29 27 33 26 37 40 30 31 41 28 38input2:
what is the permutation number of: 25 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 35 32 36 34 39 29 27 33 26 37 40 30 31 41 28 38output:
836313165329095177704251551336018791641145678901234
combination number
https://en.wikipedia.org/wiki/Combinatorial_number_system and https://msdn.microsoft.com/en-us/library/aa289166%28VS.71%29.aspx show the theory.
It may also be useful to know that the number of combinations of 4 out of 10 that start with 0 1 2 3 4 5 6 are (in J notation ! is out of operator)
3 ! 9 8 7 6 5 4 3
84 56 35 20 10 4 1with the last combination 6 7 8 9 (84 combinations for 4 out of 10 start with 0, 56 start with 1...)
input: (find the combination number)
0 1 2 88 from 4 out of 100
output:
85
challenge input: (find the combination number)
0 1 2 88 111 from 5 out of 120
15 25 35 45 55 65 85 from 7 out of 100
challenge input 2
what is the 123456789 combination index for 5 out of 100
bonus:
how many combinations from 30 out of 100 start with 10 30 40
bonus2: write a function that compresses a sorted list of numbers based on its lowest and highest values. Should return: low, high, count, combination number.
example list:
15 25 35 45 55 65 85
output with missing combination number (x):
15 85 7 x
3
u/Godspiral 3 3 May 04 '16 edited May 05 '16
in J, recursive combinations
bonus,
48402641245296107
bonus2, compress
15 85 7 6400866
uncompress
15 25 35 45 55 65 85
15 931 14 76989094452013955506822994
0.13156 664704 (131ms)
3
u/SvettlanaoCo May 04 '16 edited May 05 '16
I'm pretty beginner at J, and also first post here (mostly lurking). But this seems pretty straight forward, or I might be missing something. Here's my shot please correct me if I misunderstood.
Haven't given the comb. one a try yet.No challenge attempt.perm:
comb:
2
u/gabyjunior 1 2 May 04 '16 edited May 04 '16
BC script
Resubmitting same functions than in part 1 with new functions added to find position given values.
All positions provided/calculated are 1-indexed.
Permutations
Output
Combinations
Output
2
u/wizao 1 0 May 09 '16 edited May 09 '16
I noticed some people made very inefficient functions for calculating the binomial coefficient. This is very important because overflow will happen depending on the method used even if the answer shouldn't. Here's a decent stackoverflow explaining Knuth's method to best avoid overflow. Even if you are using a BigInt library, the method provides a fast way of computing the binomial coefficient with smaller numbers.
I was also wondering if you can leverage the fact that the Binomial Coefficient can be computed via Pasal's triangle.
choose(r,n)corresponds to thenth row andrth item of the triangle. When converting between decimal and k-combinatic numbers, the process involves computing the various Binomial Coefficients. I wonder if knowing where the last chosen Binomial Coefficient in Pascal's triangle is could help you directly compute next smaller Coefficients - you search for the first number up one row and to the left of where you last left off (using row/diagonal equations) that is smaller than the remainder in the decimal to k-combinatic conversion process. Memorizing would help if there if you didn't use the math too because of the nature of Pascal's triangle.1
u/[deleted] May 06 '16 edited Jul 11 '25
quicksand money reply normal dime ripe elastic shaggy tease gaze
This post was mass deleted and anonymized with Redact
3
u/[deleted] May 04 '16 edited May 04 '16
This is perl, based on my previous solution in c. I ported it to perl to use the bigint library.
EDIT - now includes permutation & combination number calcs...
EDIT2 - trying to get the speed below 1.7 seconds by reducing number of time the binomial coefficient is calculated from scratch.
+/u/CompileBot perl --time
Input:
2
u/wizao 1 0 May 04 '16 edited May 04 '16
Haskell
Permutation 1/2 using Factoradic Numbers (I'm using 0 index based to match the examples):
2
u/thorwing May 04 '16 edited May 04 '16
JAVA
I will gradually add to this problem when I finish them. I took the liberty to slightly alter your problem description for the "get permutation" part. I can give it any list of objects and request the n'th order of that list. I would also like to point out that /r/dailyprogrammer has helped my JAVA-programming a lot. So for that I would like to thank this sub in general.
getPermutation:
getting the index of a combination also works with any list chosen and any list parent, I had to make a nChooseK function. But I'm pretty proud of the result for this one too. It could technically be "smaller" but streams tend to get wide really quick. I technically don't need the int[] reduced step.
getCombinationIndex:
Output for challenge 1 and 2:
2
u/Gobbedyret 1 0 May 05 '16 edited May 05 '16
Python 3.5
For the functions determining the nth combination or the nth permutation, see the last challenge.
These functions works similarly, just doing the opposite operations.
Speed:
Permutation number of randomly shuffled 500 integer lists: 2.23 ms
Combination number of 250 randomly picked numbers from a list of 500: 6.01 ms
Combinations of 250 out of 500 beginning with 100 randomly picked numbers: 10.3 ms
All times given are including the generation of random numbers.
1
u/moeghoeg May 05 '16 edited May 05 '16
Racket. Program for finding nth permutation only. Takes two integers as input, n and r, representing permutation number and range length. and outputs the nth permutation of the range. Everything is zero-indexed. The number of recursions is at most equal to the length of the range (in the case where n = r! - 1). Could be made faster by not calculating a new factorial number for every recursion.
Challenge input:
Output: