Glider
"In het verleden behaalde resultaten bieden geen garanties voor de toekomst"
About this blog

These are the ramblings of Matthijs Kooijman, concerning the software he hacks on, hobbies he has and occasionally his personal life.

Most content on this site is licensed under the WTFPL, version 2 (details).

Questions? Praise? Blame? Feel free to contact me.

My old blog (pre-2006) is also still available.

See also my Mastodon page.

Sun Mon Tue Wed Thu Fri Sat
       
23
 
Powered by Blosxom &Perl onion
(With plugins: config, extensionless, hide, tagging, Markdown, macros, breadcrumbs, calendar, directorybrowse, entries_index, feedback, flavourdir, include, interpolate_fancy, listplugins, menu, moreentries, pagetype, preview, seemore, storynum, storytitle, writeback_recent)
Valid XHTML 1.0 Strict & CSS
Happy Numbers in 1 line of C

This afternoon on IRC people were discussing the assignments of the NIO, a programming contest for high school students I've participated in a few times. One particularly nice one was the "happy" assignment.

In short, the assignment says there are happy numbers and non happy numbers. To see if a number is happy, you take the square of every digit and add these squares together. You do this squaring and summing until you get either the number "1" of the number "4". If you get "1", the number is happy, if you get "4", the number is not. This is a fairly simple assignment to write a program for, but the program could not have more than 1000 characters, which makes it kinda interesting.

As soon as somebody posed that he could to it in four hundred something characters, without leaving out stuff (like whitespace and indentation), people started trying too. Before long, a couple of people were trying to make their programs as short as possible. Later we merged our tries to get even shorter versions. The end result was a very short solution for this problem (chopped into two lines for readability):

m[666],o,x;h(i){for(x=0;i;i/=10)x+=i%10*(i%10);m[x]=m[x]?:h(x);}
main(i){for(m[m[4]=scanf("%d",&i)]=2;i;o+=h(i--)-1);printf("%d\n",o);}

See more ...

 
1 comment -:- permalink -:- 02:41
Copyright by Matthijs Kooijman - most content WTFPL