Created
December 5, 2011 20:58
-
-
Save jakedobkin/1435312 to your computer and use it in GitHub Desktop.
Euler 36
This file contains hidden or 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
| #!/usr/local/bin/node | |
| function reverse(s){ | |
| return s.split("").reverse().join(""); | |
| } | |
| function ispali(a) | |
| { | |
| a = a.toString(); | |
| for (i=0;i<=a.length-1;i++) | |
| { | |
| if (a.length%2 == 0) | |
| { | |
| b = a.slice(0, a.length/2); | |
| c = a.slice(a.length/2); | |
| c = reverse(c); | |
| if (b==c) | |
| { | |
| return true; | |
| } | |
| } | |
| else | |
| { | |
| e = a.slice(0, (a.length/2)); | |
| f = a.slice((a.length+1)/2); | |
| f = reverse(f); | |
| if (e==f) | |
| { | |
| return true; | |
| } | |
| } | |
| } | |
| } | |
| sum = 0; | |
| for (a=0;a<=1000000;a++) | |
| { | |
| // is a a palindrome? | |
| if (ispali(a)) | |
| { | |
| binary_a = Number(a).toString(2); | |
| binary_a = binary_a.toString(); | |
| if (ispali(binary_a)) | |
| { | |
| console.log(a,binary_a, "is palindrome in both base 2 and 10"); | |
| sum = sum + a; | |
| } | |
| } | |
| } | |
| console.log("sum is",sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment