Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
var permuter = require( './next-permutation.js' ).permutationGenerator; | ||
var parens = [ | ||
'(_ _) (_ _)', '((_ _) _) _', '(_ (_ _)) _', '_ ((_ _) _)', '_ (_ (_ _))' | ||
]; | ||
var operators = [ '+', '-', '*', '/' ]; | ||
var maxn = 0; | ||
|
||
for ( var a = 0; a < 7; ++a ) { | ||
for ( var b = a + 1; b < 8; ++b ) { | ||
for ( var c = b + 1; c < 9; ++c ) { | ||
for ( var d = c + 1; d < 10; ++d ) { | ||
var reachable = []; | ||
|
||
for ( var opcode = 0; opcode < 4 * 4 * 4; ++opcode ) { | ||
var o = opcode; | ||
var op1 = o % 4; | ||
o -= o % 4; | ||
o /= 4; | ||
var op2 = o % 4; | ||
o -= o % 4; | ||
o /= 4; | ||
var op3 = o; | ||
|
||
for ( var i = 0; i < parens.length; ++i ) { | ||
seq = [ a, b, c, d ]; | ||
|
||
do { | ||
var exp = parens[ i ] | ||
.replace( ' ', operators[ op1 ] ) | ||
.replace( ' ', operators[ op2 ] ) | ||
.replace( ' ', operators[ op3 ] ) | ||
.replace( '_', seq[ 0 ] ).replace( '_', seq[ 1 ] ) | ||
.replace( '_', seq[ 2 ] ).replace( '_', seq[ 3 ] ) | ||
var val = eval( exp ); | ||
// console.log( exp ); | ||
if ( Math.floor( val ) == val && val >= 1 ) { | ||
if ( typeof reachable[ val ] == 'undefined' ) { | ||
reachable[ val ] = true; | ||
} | ||
} | ||
} while ( ( seq = permuter.getNextSequence( seq ) ) != null ); | ||
} | ||
} | ||
for ( var i = 1; ; ++i ) { | ||
if ( typeof reachable[ i ] == 'undefined' ) { | ||
if ( i - 1 > maxn ) { | ||
console.log( '' + a + b + c + d + ' yields maximum of ' + ( i - 1 ) ); | ||
maxn = i - 1; | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
///////////////////////////////////////////// | ||
// Permutation Generator | ||
// - generate all permutation of given string | ||
// array | ||
// - Natthawut Kulnirundorn <m3rlinez at email by google> | ||
// http://www.solidskill.net 10 July 2011 | ||
///////////////////////////////////////////// | ||
|
||
var PermutationGenerator = (function () { | ||
var self = {}; | ||
|
||
// Get start sequence of given array | ||
self.getStartSequence = function (list) { | ||
return list.slice(0).sort(); | ||
}; | ||
|
||
// Get next sequence from given array | ||
// Ref: http://en.wikipedia.org/wiki/Permutation#Systematic_generation_of_all_permutations | ||
self.getNextSequence = function (list) { | ||
// Make clone | ||
var a = list.slice(0); | ||
|
||
//The following algorithm generates the next permutation lexicographically after a given permutation. It changes the given permutation in-place. | ||
// 1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation. | ||
var k = -1; | ||
for (var i = 0; i < a.length - 1; ++i) { | ||
if (a[i] < a[i + 1]) { k = i; } | ||
} | ||
if (k == -1) return null; // means this is the last one | ||
|
||
// 2. Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l. | ||
var l = -1; | ||
for (var i = 0; i < a.length; ++i) { | ||
if (a[k] < a[i]) { l = i }; | ||
} | ||
if (l == -1) return null; // impossible | ||
|
||
// 3. Swap a[k] with a[l]. | ||
var tmp = a[k]; a[k] = a[l]; a[l] = tmp; | ||
|
||
// 4. Reverse the sequence from a[k + 1] up to and including the final element a[n]. | ||
var next = a.slice(0, k + 1).concat(a.slice(k + 1).reverse()); | ||
|
||
return next; | ||
}; | ||
|
||
return self; | ||
} ()); | ||
|
||
module.exports = { | ||
permutationGenerator: PermutationGenerator | ||
}; |