Skip to content

Instantly share code, notes, and snippets.

@xk
Created February 10, 2014 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xk/8926509 to your computer and use it in GitHub Desktop.
Save xk/8926509 to your computer and use it in GitHub Desktop.
Concise Happy Numbers JavaScript
//2014-02-10 jorge@jorgechamorro.com concise happy numbers JavaScript
function happy (n) {
var past= [];
while (n= [].reduce.call(n.toString(), function (a,n) { return a+n*n }, 0))
if (n === 1) return 1; else if (past.indexOf(n) >= 0) return 0; else past.push(n);
}
for (var i=0 ; i<=100 ; ++i) happy(i) && console.log(i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment