Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created December 5, 2011 20:58
Show Gist options
  • Select an option

  • Save jakedobkin/1435312 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1435312 to your computer and use it in GitHub Desktop.
Euler 36
#!/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