Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created November 12, 2011 21:43
Show Gist options
  • Save jakedobkin/1361162 to your computer and use it in GitHub Desktop.
Save jakedobkin/1361162 to your computer and use it in GitHub Desktop.
euler 9
<html>
<body>
<script type="text/javascript">
for (a=1;a<=1000;a++)
{
for (b=a+1;b<=1000;b++)
{
c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
sum = a+b+c;
product = a*b*c;
if (sum==1000)
{
document.write(product);
}
}
}
</script>
</body>
</html>
@djacobs
Copy link

djacobs commented Nov 13, 2011

function is_int(value) { 
   return (isNaN(value));
}

a = 333; 
sum = 0;
product = 0;

while (!(sum == 1000)) {
    a--;
    b=(1000-2*a);
    while(b>10 && !(sum==1000)) {   
        b--;            
        c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
        if (Math.floor(c) == Math.sqrt(Math.pow(c,2))) {
            console.log(a, b, Math.floor(c), Math.sqrt(Math.pow(c,2)));
            sum = a+b+c;
            product = a*b*c;
        }
    }
}

console.log(sum, product, a, b, c);


Here's mine - I agree that using natural number sequences would be much faster. The other thing is to count down a from 333 (since we know the numbers sum to 1000 and is the smallest of them).

@djacobs
Copy link

djacobs commented Nov 13, 2011


function is_int(value) { 
   return (isNaN(value));
}

a = 333; 
sum = 0;
product = 0;

while (!(sum == 1000)) {
    a--;
    b=(1000-2*a);
    while(b>10 && !(sum==1000)) {   
        b--;            
        c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
        if (Math.floor(c) == c) {
            // console.log(a, b, Math.floor(c), Math.sqrt(Math.pow(c,2)));
            sum = a+b+c;
            product = a*b*c;
        }
    }
}

console.log(sum, product, a, b, c);

This is faster, saved some math.

@jakedobkin
Copy link
Author

jakedobkin commented Nov 13, 2011 via email

@djacobs
Copy link

djacobs commented Nov 13, 2011

OK, a couple other minor nits (I never finished writing that is_int function, which didn't work) and of course while b>a is better than b > 10.

a = 293; 
sum = 0;
product = 0;

while (sum != 1000) {
    a--;
    b=(1000-2*a);
    while(b>a && !(sum==1000)) {    
        b--;            
        c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
        if (Math.floor(c) == c) {
            // console.log(a, b, Math.floor(c), Math.sqrt(Math.pow(c,2)));
            sum = a+b+c;
            product = a*b*c;
        }
    }
}

console.log(sum, product, a, b, c);

@djacobs
Copy link

djacobs commented Nov 13, 2011


a = 293; 
sum = 0;

while (sum != 1000) {
    a--;
    b=(1000-2*a);
    while(b>a && !(sum==1000)) {    
        b--;            
        c = Math.sqrt(Math.pow(a,2) + Math.pow(b,2));
        if (Math.floor(c) == c) {
            // console.log(a, b, Math.floor(c), Math.sqrt(Math.pow(c,2)));
            sum = a+b+c;
        }
    }
}

product = a*b*c;
console.log(sum, product, a, b, c);

and computing product can be outside the loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment