Created
November 12, 2011 21:43
-
-
Save jakedobkin/1361162 to your computer and use it in GitHub Desktop.
euler 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
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.
Right- use the m and n relationship to limit scope of a and b, count down
instead of up. that all makes sense.
jake
Jake Dobkin
Gothamist.com
(p) 917 627 6915
(f) 646 349 3893
AIM, YIM, GTalk: jakedobkin
Mediakit: www.gothamistllc.com
…On Sun, Nov 13, 2011 at 11:19 AM, David Jacobs < ***@***.*** > wrote:
```
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.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1361162
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);
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
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).