I want a program that takes in an 8 digits number and return all permutations of it.
For instance, 12345678
should return 12345687, 12345768, 12345786 .. 87654321
.
My idea is to do this:
First put every digit as a element in an array and then:
for (int y = 0; y < digits; y++)
for (int x = 0; x < digits; x++)
for (int u = 0; u < digits; u++)
for (int m = 0; m < digits; m++)
for (int z = 0; z < digits; z++)
for (int b = 0; b < digits; b++)
for (int c = 0; c < digits; c++)
for (int d = 0; d < digits; d++)
{
if (!(y == x || y == u || y == m
|| y == z || y == b || y == c
|| y == d || x == u || x == m
|| x == z || x == b || x == c
|| x == d || u == m || u == z
|| u == b || u == c || u == d
|| m == z || m == b || m == c
|| m == d || z == b || z == c
|| z == d || b == c || b == d || c == d))
{
holding[co] = (a1[y] * 10000000)
+ (a1[x] * 1000000)
+ (a1[u] * 100000)
+ (a1[m] * 10000)
+ (a1[z] * 1000)
+ (a1[b] * 100)
+ (a1[c] * 10)
+ a1[d];
co++;
}
}
And get all of the result into an array. Sort the array and get rid of the same elements (like if input is 11223344
then there will be same elements).
But the problem is I actually want to print all of permutations of numbers from 10000000
to 20000000
. This idea works too slow. Does anyone know how to do it more quickly?