Skip to content

Instantly share code, notes, and snippets.

@xk xk/happyNumbers.js

Created Feb 10, 2014
Embed
What would you like to do?
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
You can’t perform that action at this time.