Created
December 11, 2013 23:18
-
-
Save H2CO3/7920316 to your computer and use it in GitHub Desktop.
Solution to Project Euler problem #92 as suggested by Daniel Silverstone.
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
/* | |
* p92.spn | |
* | |
* a solution to Project Euler problem #92 | |
* using the Sparkling scripting language | |
*/ | |
function next(n) | |
{ | |
var s = 0; | |
while n != 0 { | |
var digit = n % 10; | |
s += digit * digit; | |
n /= 10; | |
} | |
return s; | |
} | |
function last(n, cache) | |
{ | |
var t = cache[n]; | |
if t != nil { | |
return t; | |
} | |
return cache[n] = n == 1 || n == 89 ? n : last(next(n), cache); | |
} | |
var i, n = 0; | |
var cache = array(); | |
for i = 1; i < 10000000; i++ { | |
if last(i, cache) == 89 { | |
n++; | |
} | |
} | |
print(n); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment