Mathematica 115 108 107 102 100 99 91 87 Chars
87 characters
Print[If[Nest[Tr[IntegerDigits@#^2]&,#,9]>1,Sad,Happy],If[PrimeQ@#," "," non-"],prime]&
- Mr.Wizard
Yes, the monkey learned a few tricks (91 characters)
Print[ If[Nest[Plus@@(IntegerDigits@ #^2) &, #, 9] > 1, Sad, Happy ], If[PrimeQ@#, " ", " non-"], prime ] &
Call% [7]
Change 5 - 99 characters /
Nine iterations are enough. Thanks @Nabb, @mjschultz
h = Print[ If[Nest[Plus @@ (IntegerDigits@#^2) &, #, 9] > 1, "Sad ", "Happy "] , If[PrimeQ@#, "", "non-"], "prime"] &
Change 4 - 100 characters /
Same as Edit 3, replacing 10 ^ 2 with 99 (allowing 84 digits for input values) ... Thanks, @Greg
h = Print[ If[Nest[Plus @@ (IntegerDigits@#^2) &, #, 99] > 1, "Sad ", "Happy "] , If[PrimeQ@#, "", "non-"], "prime"] &
Edit 3 - 102 characters /
Recycled the loop again.
Interestingly, the depth of the recursion before reaching ultimately 1 is limited (15 + The number of digits of the argument). See here
So, for numbers with less than 85 characters (I think this limit is pretty well reflected in the OP “Don't worry about handling large numbers”), the following code works
h = Print[ If[Nest[Plus @@ (IntegerDigits@#^2) &, #, 10^2] > 1, "Sad ", "Happy "] , If[PrimeQ@#, "", "non-"], "prime"] &
I changed “NestWhile” for a shorter “socket”, and therefore, instead of specifying a stop condition for recursion, it’s enough to hardcode the desired recursion depth (10 ^ 2).
This is not very effective, but the life of a golfer: D
Change 2 - 107 characters /
Reworked Garden / Happy assignment
h = Print[ If[NestWhile[Plus @@ (IntegerDigits@#^2) &, #, # > 4 &] > 1,"Sad ","Happy "] ,If[PrimeQ@#, "", "non-"] , "prime"] &
All spaces / newlines except letters are optional and are added for readability.
Explanation:
NestWhile[Plus @@ (IntegerDigits@
Recursions using the "function" [Add sum of digits to square] until the result reaches 4 or less. The function has the property that it stagnates to "1" or enters the cycle {4, 16, 37, 58, 89, 145, 42, 20, 4, ...}.
So, when the result is "1", the number is "Happy", and when the result is "4", it is "Garden".
If the result is “2”, it is also SAD, because it will go into the SAD loop at the next iteration (2 ^ 2 = 4).
If the result is 3, the cycle is 3-> 9-> 81-> 65-> 61-> 37-> 58-> 89-> 145-> .... (included in the SAD cycle).
Thus, we can stop recursion when the result is 4 or less, knowing that only the result "1" will result in a lucky number.
Perhaps other solutions can take advantage of this fact.
In fact, results 5 and 6 also lead to SAD numbers, but this only gives us an increase in efficiency and not an advantage in golf (I think).
Change 1 - 108 characters /
Reworked loop control logic
h = Print[ NestWhile[Plus@@(IntegerDigits@#^2) &, #, #>4 &] /.{1 →"Happy ",_→"Sad "} , If[PrimeQ@#, "", "non-"] , "prime"] &
Original - 115 characters /
h = Print[ If[NestWhile[Plus @@ (IntegerDigits@#^2) &, #, Unequal, All] == 1 ,"Happy ", "Sad "], If[PrimeQ@#, "", "non-"], "prime"] &
Statement
NestWhile[Plus @@ (IntegerDigits@#^2) &, #, Unequal, All]
performs recursive application of sums of numbers in a square until a certain value is repeated. The “Unequal, All” part compares the list of previous values. Finally returns a duplicate value that is "1" for Happy Numbers.
Run example
h[7] Happy prime h[2535301200456458802993406410753] Sad non-prime
Looping (slightly changing the print statement)
1 Happy non-prime 2 Sad prime 3 Sad prime 4 Sad non-prime 5 Sad prime 6 Sad non-prime 7 Happy prime 8 Sad non-prime 9 Sad non-prime 10 Happy non-prime 11 Sad prime 12 Sad non-prime 13 Happy prime