I know very little about javascript, but I managed to whip this up.
Code
var Numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
HeapsPermutation(Numbers, Numbers.length);
function CheckForSolution(ListPermutation)
{
//using the simplified function: a + d – f + (13b/c) + 12e +(gh/i) = 87
//split up into 4 terms
var Term1 = ListPermutation[0] + ListPermutation[3] - ListPermutation[5];
var Term2 = ((13*ListPermutation[1])/ListPermutation[2]);
var Term3 = 12*ListPermutation[4];
var Term4 = ((ListPermutation[6]*ListPermutation[7])/ListPermutation[8]);
//these two terms need to be checked for decimals
var DecimalTerm1 = ((13*ListPermutation[1])%ListPermutation[2]);
var DecimalTerm2 = ((ListPermutation[6]*ListPermutation[7])%ListPermutation[8]);
if(DecimalTerm1 != 0 || DecimalTerm2 != 0) return false;
if(Term1 + Term2 + Term3 + Term4 == 87)
{
console.log("Solution found: " + ListPermutation);
return true;
}
return false;
}
//heaps algorithm
function HeapsPermutation(list, size)
{
if (size == 1) CheckForSolution(list);
else
{
for (var i = 1; i <= size; i++)
{
HeapsPermutation(list, size - 1);
if (size % 2) var j = 1;
else var j = i;
Swap(list, j - 1, size - 1);
}
}
}
function Swap(array, Index1, Index2)
{
var temp = array[Index1];
array[Index1] = array[Index2];
array[Index2] = temp;
}
//Outputs:
/*
Solution found: 9,4,1,5,2,7,3,8,6
Solution found: 5,4,1,9,2,7,3,8,6
Solution found: 5,2,1,3,4,7,9,8,6
Solution found: 3,2,1,5,4,7,9,8,6
Solution found: 5,2,1,3,4,7,8,9,6
Solution found: 3,2,1,5,4,7,8,9,6
Solution found: 9,4,1,5,2,7,8,3,6
Solution found: 5,4,1,9,2,7,8,3,6
Solution found: 6,9,3,5,2,1,8,7,4
Solution found: 5,9,3,6,2,1,8,7,4
Solution found: 9,3,1,6,2,5,8,7,4
Solution found: 6,3,1,9,2,5,8,7,4
Solution found: 7,3,1,5,2,6,9,8,4
Solution found: 5,3,1,7,2,6,9,8,4
Solution found: 6,9,3,5,2,1,7,8,4
Solution found: 5,9,3,6,2,1,7,8,4
Solution found: 9,3,1,6,2,5,7,8,4
Solution found: 6,3,1,9,2,5,7,8,4
Solution found: 7,3,1,5,2,6,8,9,4
Solution found: 5,3,1,7,2,6,8,9,4
*/
Wrote it in vscode and I used node to run it.
I need my money now plz ty. :)
I also wrote this. Money plz.