Skip to content

Instantly share code, notes, and snippets.

@Marknel
Created May 21, 2015 23:51
Show Gist options
  • Save Marknel/b1c2ddc3b02f73d0a73d to your computer and use it in GitHub Desktop.
Save Marknel/b1c2ddc3b02f73d0a73d to your computer and use it in GitHub Desktop.
3rd Grade Maths Problem Solver (Brute Force)
var processor2 = function(v) {
return ((13*v[1])/v[2])+v[0]+v[3]+12*v[4]-v[5] + ((v[6]*v[7])/v[8]) == 87;
}
var start_time = Date.now(),
total_combinations = 0,
pv = [1,2,3,4,5,6,7,8,9],
permArr = [],
usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
if (processor2(usedChars)) {
permArr.push(usedChars.slice());
} else {
usedChars.slice();
}
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr
};
permute(pv);
var finish_time = Date.now();
var time_taken = finish_time - start_time;
console.log("Total Combinations:" + permArr.length);
console.log("Total Time: " + time_taken);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment