It's Sunday, time for a round of code golf!
Challenge
Write the shortest source code by character count to determine if an input number is a "happy prime", "sad prime", "happy non-prime", or "sad non-prime."
Input
The input should be a integer that comes from a command line argument or stdin. Don't worry about handling big numbers, but do so if you can/want. Behavior would be undefined for input values less than 1, but 1 has a definite result.
Output
Output should print the type of number: "happy prime", "sad prime", "happy non-prime", or "sad non-prime." The trailing newline is optional.
Examples
$ happyprime 139
happy prime
$ happyprime 2
sad prime
$ happyprime 440
happy non-prime
$ happyprime 78
sad non-prime
Definitions
Just in case your brain needs a refresher.
Happy Number
From Wikipedia,
A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).
For example,
- 139
- 1^2 + 3^2 + 9^2 = 91
- 9^2 + 1^2 = 82
- 8^2 + 2^2 = 68
- 6^2 + 8^2 = 100
- 1^2 + 0^2 + 0^2 = 1
Prime Number
A prime number is an integer greater than 1 and has precisely two divisors: 1 and itself.
Happy Prime
A happy prime, is therefore a number that is both happy and prime.
Answer Selection
Obviously the answer will be the shortest source code by character count that outputs the specified results in all cases that I test. I will mark the answer once the next (community decided) code golf challenge comes along, so we can focus all our energies on that one. :)
Decision
Well, it looks like the there is a new code golf in town and it has been about a week since this question was posted, so I've marked the shortest source code as the answer (gnibbler's 64 character Golfscript solution). That said, I enjoyed both the 99 character Mathematica solution by belisarius and the cryptic 107 character dc solution by Nabb.
To all others, great work! I've never had so many programming language environments on my computer. I hope everyone has learned some new, dirty tricks for their favorite language.
Reuse
I've re-published some of the code produced by this competition as an example for a script I wrote to test various programs against a reference implementation for auto-grading. The README in that directory explains where the source code comes from and states that all code is re-used under the CC BY-SA 2.5 license (as stated in SO's legal section). Each directory is labeled with your display name at the time of the submission.
If you have a problem with your code being re-used in this fashion or the attribution, let me know and I will correct the error.