0

I've searched around and tried myself, but couldn't quite figure out how to do it.

I have an equation, say a^2 + b^2 = c^2. I want to find what integers a, b, and c have to be to make this equation true using JavaScript (node.js). Obviously we know that it could be many things (such as a = 3, b = 4, and c = 5, but I'm only looking for the first answer the computer musters, no matter the answer. The only specification is that it must be an integer greater than 0, and solve it with brute force method, or just plugging in tons of numbers until it is true.

an example/one way I've thought of doing it is:

var
  a = 1;
  b = 1;
  c = 1;

if (Math.pow(a,2) + Math.pow(b,2) === Math.pow(c,2)) {
    console.log('you did it!');
} else {
    //something such as a++ or idk
  }

I don't know what to do for if the equation isn't true. My idea was first adding 1 to a, then subtracting 1 from a and adding 1 to b, and so on until the equation is true... sorta like this here

If you have any ideas on how to best do this, or if someone has solved this before and I couldn't find the source, please share!

Thanks

2 Answers2

0
let a,b,c
let high = 100
let low = 1
let checkedValues = []
do {
    a = Math.round(Math.random() * (high - low) + low)
    b = Math.round(Math.random() * (high - low) + low)
    c = Math.round(Math.random() * (high - low) + low)
    if(checkedValues.find(arr => arr.every((v,i)=> v === [a,b,c][i]))) continue
    checkedValues.push([a,b,c])
} while(Math.pow(a,2) + Math.pow(b,2) !== Math.pow(c,2))

console.log('You found a = ', a, ' b = ', b, ' c = ', c)

You are getting random values from low to high. You check did answer occured before. Then you put it in equation and check is it false, if yes, you are getting new random, else you have good answer.

patys
  • 34
  • 5
  • would there be a way to make it so no combination would happen again? obviously the chances are very very slim, but if `a = 1, b = 39, and c = 94` has already been proven false, could you input something to make that answer already not needed? it would probably actually make the program slower tho wouldn't it... – Fred Fredwerd Dec 12 '17 at 08:32
  • Edited, firstly we generate random values, if they were in checkedValues we skip, else we add this to checkedValues and calculate equation. – patys Dec 12 '17 at 08:41
0

I like this kind of exercises. I made it with a recursive call.

var checkValues = function(a, b, c) {
    if (Math.pow(a,2) + Math.pow(b,2) === Math.pow(c,2)) {
        return true;
    }
    return false;
}



var MIN = 1;
var MAX = 20;
var a = b = c = MIN;

var valuesGenerator = function(a, b, c) {
console.log("trying: " + a + ' - ' + b + ' - ' + c);
    if(checkValues(a, b, c)) {
        console.log('you did it!');
        console.log("Solution: a - " + a + "   b - " + b + "    c - " + c);
        process.exit();
    }

    // Not got it yet
    if(++c >= MAX) {
        c = MIN;
        if(++b >= MAX) {
            b = MIN;
            if(++a >= MAX) {
                console.log('It seams we did not get a solution :(');
                process.exit();
            }
        }
    }

    // Try new values
    valuesGenerator(a, b, c);
}

valuesGenerator(a, b, c);
David Vicente
  • 3,091
  • 1
  • 17
  • 27
  • I like the way you systematically did this, the only issue is after around the 14000th time I receive `RangeError: Maximum call stack size exceeded`, probably so my computer doesn't have a meltdown lol – Fred Fredwerd Dec 12 '17 at 09:30
  • Probably is a limit of Node for number of calls or stack size, and it could be modified. In order to let it work, just decrease the number of MAX. Maybe with 10 it will work – David Vicente Dec 12 '17 at 09:32
  • 1
    yeah haha, I just wanted to see how big it could go, it works great! Thank you – Fred Fredwerd Dec 12 '17 at 09:34