d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 500fg - Solve This Puzzle With Javascript.
Add Reply New Topic New Poll
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Jul 18 2019 09:47am
Paying 500fg for a javascript program that solves the puzzle in this link.

There are multiple soluions.

Solutions have to be wihout decimals. For example, cannot divide 26 by 3.

https://www.cosmopolitan.com/lifestyle/news/a40861/heres-the-latest-math-problem-thats-stumping-the-internet/



This post was edited by kdog3682 on Jul 18 2019 10:09am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 18 2019 07:22pm
if i'm reading that correctly.

n0 + 13 x n1 / n2 + n3 + 12 x n4 - n5 - 11 + n6 x n7 / n8 - 10 = 66

n0 + 13 x n1 / n2 + n3 + 12 x n4 - n5 + n6 x n7 / n8 = 87

a bit of python for you.
Code
import itertools

for n in itertools.permutations(range(1,10)):
if 13 * n[1] % n[2] != 0:
continue
if n[6] * n[7] % n[8] != 0:
continue
if n[0] + 13 * n[1] / n[2] + n[3] + 12 * n[4] - n[5] + n[6] * n[7] / n[8] == 87:
print(n)


This post was edited by carteblanche on Jul 18 2019 07:23pm
Member
Posts: 14,211
Joined: Dec 31 2007
Gold: 107,134.00
Jul 25 2019 08:39am
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. :)
Member
Posts: 3,797
Joined: Jun 27 2011
Gold: 5,006.00
Jul 25 2019 03:39pm
Quote (Ogrebeast @ Jul 25 2019 09:39am)
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.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll