Evaluate if a number is considered a happy number in the least amount of characters? [closed]
Asked by
5 years ago Edited 4 years ago
For more clarification, a number is happy when the sum of the square of all numbers within a given number is equal to 1. If you enter a loop when calculating the happy number, that means the number is unhappy. :(
Print statements are not part of your character count. Your code should explicitly state if the number is happy or otherwise.
Example:
19
-> (1^2 + 9 ^2)
= 82 -> (8^2 + 2^2)
= 68 -> (6^2 + 8^2)
= 100 ->(1^2 + 0^2 + 0^2)
= 1. This means that 19 is a happy number.
Shortest and most valid answer within a week gets the solution. If you've gotten an answer on the Discord, you are not a valid contester. I'll update the question with my implementation within due time.
And, before you prepare your moderation voter hammer, it's in-line with all rules and regulations. I checked thrice.
Cheers.
My answer:
1 | local t,y,b,x = { } , tonumber , 29 ; function h(n) if (t [ n ] ) then ; return ( false ) end ;t [ n ] = "" ;b = 0 ; for _ in ( tostring (n):gmatch( '%d' )) do ;b = b+(y(_) or ( 0 ))^ 2 ; end ; if (b< = 1 ) then ; return ( "" ) end ; print (b) return (h(b)) end ; print (h(b)~ = false ) |
Locked by JesseSong
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?