Can you tweet a happy number function?

After my last post on happy numbers, fellow ThoughtWorkers here in Brazil's office started to come up with different languages implementations of the Happy Number function and the challenge was "Can you tweet that?", here's the results (not all are "tweetable", but are pretty cool):

Thiago Nunes got a GNU - AWK(after a update the old version of 104 got reduced to 82):

{n=7;while(!(n in d)){d[n]=n;split(n,g,//);n=0;for(i in g){n+=g[i]^2}}print n==1}

length: 82 characters.

Sam Tardif, made a very short Python version:

import sys
while d>9:d=sum(int(i)**2 for i in “%s”%d)

length: 105 characters.

Here's the shortest I've could make work in Perl:

perl -MList::Util=sum -e'$_=pop;until(/^1$/|$_{$_}++){$_=sum map$_**2,split//}print$_^1?"Un":"","Happy";' 7

length: 108 characters.

Impressive, Filipe Sabella wrote a Scala version:

scala -e 'var i=7;var s=Set(0);while(i!=1&;&; !s.contains(i)){s+=i;i=(""+i).map(a=>(a+"").toInt).map(n=>n*n).sum};println(i==1)'

length: 127 characters.

Thiago Marano's Ruby solution:

ruby -e 'i=ARGV[0].to_i;a={};until(i==1||a[i]);a[i]=1;i=i.to_s.split(//).inject(0){|s,c|s+c.to_i**2};end;p (i==1?"":"un")+"happy"' 7

length: 133 characters.

Caio Kinzel did a Haskell version...

import Data.Char h x m=case(x,any(==x)m)of(1,_)->1;(_,True)->0;(_,_)->(h(sum$map(^2)$map(digitToInt)$show x))(x:m)
main=print$h 7 []

length: 133 characters.

Luiza Pagliari made one in R:

R -e “j=7;c=0;while(j!=1&[j])){c[j]=1;j=sum(sapply(strsplit(as.character(j),”),as.numeric)^2)};

length: 160 characters.

Luiz Ribeiro took the time to do it in Prolog:

s(X,Y):-X<10,Y is X*X. s(X,Y):-I is X mod 10,J is I*I,K is X//10,s(K,L),Y is J+L. h(N):-h(N,[]). h(1,_). h(N,L):-not(member(N,L)),append([N],L,M),s(N,P),!,h(P,M).

length: 166 characters.

I did the most nasty tricks I knew in Java to get this one, but I don't think is possible to cut 60 chars of it.

import java.util.*;class h{static{Set s=new HashSet();int e=7;while((e=c(e))!=1&&s.add(e));System.out.print(c(e)==1);}static int c(int o){int m=0;for(char d:(""+o).toCharArray())m+=(o=d-'0')*o;return m;}}

length: 205 characters.

Eduardo Radanovitsck did a C++ solution:

#include <iostream>
#include <set> 
using namespace std;set<int> s;int i,a;int main(){cin>>i;while(i-1){a=i;i=0;while(a){i+=(a%10)*(a%10);a/=10;}if(!s.insert(i).second&&i){cout<<"un";goto l;}}l:cout<<"happy"<<endl;}

length: 216 characters.

Caio Kinzel (again) did a F#:

let s x=x.ToString().ToList()|> x->(int)(x.ToString()))|> x->x*x)|>Seq.sum
let rec h x m=match x,(m|>Seq.exists((=)x))with|1,_->1|_,true->0|_->(h(s x)(x::m))
printfn"%i"(h 5 [])

length: 220 characters.

People are still creating other languages implementations I will add more later. You're invited to shorten the current ones or create new ones in new languages.

Published in Aug 11, 2010