Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Created December 11, 2013 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save H2CO3/7920316 to your computer and use it in GitHub Desktop.
Save H2CO3/7920316 to your computer and use it in GitHub Desktop.
Solution to Project Euler problem #92 as suggested by Daniel Silverstone.
/*
* 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