Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created November 27, 2011 21:38
Show Gist options
  • Select an option

  • Save jakedobkin/1398194 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1398194 to your computer and use it in GitHub Desktop.
Euler 24
#!/usr/local/bin/node
count = 0;
permutations = new Array();
while(count <= 1000000) {
for(a=0;a<=9;a++){
for(b=0;b<=9;b++){
for(c=0;c<=9;c++){
for(d=0;d<=9;d++){
for(e=0;e<=9;e++){
for(f=0;f<=9;f++){
for(g=0;g<=9;g++){
for(h=0;h<=9;h++){
for(i=0;i<=9;i++){
for(j=0;j<=9;j++){
if ((a!=b) && (a!=c) && (a!=d) && (a!=e) && (a!=f) && (a!=g) && (a!=h) && (a!=i) && (a!=j) && (b!=c) && (b!=d) && (b!=e) && (b!=f) && (b!=g) && (b!=h) && (b!=i) && (b!=j) && (c!=d) && (c!=e) && (c!=f) && (c!=g) && (c!=h) && (c!=i) && (c!=j) && (d!=e) && (d!=f) && (d!=g) && (d!=h) && (d!=i) && (d!=j) && (e!=f) && (e!=g) && (e!=h) && (e!=i) && (e!=j) && (f!=g) && (f!=h) && (f!=i) && (f!=j) && (g!=h) && (g!=i) && (g!=j) && (h!=i) && (h!=j) && (i!=j))
{
if (count == 999999)
{
console.log("this is the millionth permutation:");
console.log(a+""+b+""+c+""+d+""+e+""+f+""+g+""+h+""+i+""+j);
}
count +=1;
}
}
}
}
}
}
}
}
}
}
}
}
@djacobs
Copy link

djacobs commented Nov 30, 2011

cleaner code


#!/usr/local/bin/node

iteration = 999999;
size = 2; 
// the answer we want for the millionth iteration is 2783915460

tempi = iteration;

// figure out the size of the largest factorial smaller than our target iteration
while (tempi/size > 1) {
    size++;
    tempi = tempi/size; 
}


permutation = new Array();
answer = new Array();

for (i=0; i <= size; i++) {
    permutation[i] = i;
    answer[i]=0;
}


function factorial(a) {
    if (a<=1) {
        return 1;
    } 
    else {
        return a * factorial(a-1);
    }
}


function findTumbler(rem, b) {
    f = Math.floor(rem/factorial(b));

    if (rem == 0) {
        // fill in the rest of the answers array, we're done!
        for (j=0; j <= permutation.length; j++) {
            answer[size-b+j] = permutation.splice(0,1); 
        }
    } else {
        console.log("rem:", rem, "size:", b, "floor:", f);
        answer[size-b] = permutation.splice(f,1);
        findTumbler((rem % factorial(b)), b-1);
    }
}

findTumbler(iteration, size);

console.log ("the answer is",  answer.join(''));

@djacobs
Copy link

djacobs commented Nov 30, 2011

OK, final edit

#!/usr/local/bin/node

// basic problem setup
var iteration = 999999;
var size = 2; 
// the answer we want for the millionth iteration is 2783915460

// figure out the size of the largest factorial smaller than our target iteration
var tempi = iteration;
while (tempi/size > 1) {
    size++;
    tempi = tempi/size; 
}

// the arrays we'll use
var permutation = new Array();
var answer = new Array();
var f = new Array();

for (i=0; i <= size; i++) {
    permutation[i] = i;
}

// I wrote a slightly slower factorial function, I took this one from http://stackoverflow.com/questions/3959211/fast-factorial-function-in-javascript
function factorial(n) {
    if (n==0 || n==1) { return 1; }
    else if (f[n]>0) { return f[n]; }
    else { return f[n]=factorial(n-1)*n; }
}

// pull out the next value for the iterator by taking the floor of the divisor of the remainder and the next smallest factorial
function findTumbler(rem, b) {
    f = Math.floor(rem/factorial(b));   
    // debugging array to demonstrate "tumbling" for Jake
    // console.log ("answer:",  answer.join(''), "remaining digits:", permutation.join(''));

    if (rem == 0) {
        // fill in the rest of the answers array, we're done!
        l = permutation.length;
        for (j=0; j <= l; j++) {
            answer[size-b+j] = permutation.splice(0,1); 
        }
    } else {
        // debugging:
        // console.log("rem:", rem, "size:", b, "floor:", f);
        answer[size-b] = permutation.splice(f,1);
        findTumbler((rem % factorial(b)), b-1);
    }
}

findTumbler(iteration, size);
console.log ("final answer:",  answer.join(''));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment