Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process:starting with any positive integer and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endl essly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy numbers.
Example: is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
BOOL Ishappy (int n) { int rem, m; rem = m = 0; The remainder, the new number if (n <= 0) { return false; } while (n) { rem = n%10; n = N/10; M + = Rem*rem; if (n = = 0)//Cycle end { if (m = = 1)//Get Happy number return true; else if (m = = 4) return false; n = m; rem = m = 0; Continue to the next round } } return false;}
400/400Test cases passed. Status:acceptedruntime:3 Ms
Traps:
Reverses the following two lines of statements to raise an error
rem = n%10; n = N/10;
Happy Number Explanation: (the following is quoted from some things about "happy number")
First of all, it is necessary to make a "happy number" of popular science. It's not about lucky numbers, it's an interesting phenomenon in math:
The happy number has the following characteristics: In the given carry system, the sum of the squares of all digits (digits), the new number obtained is again the sum of squares of all the digits, so repeated, the final result must be 1.
For example, take decimal as an example:
1): 28→2^2+8^2=68→6^2+8^2=100→1^2+0^2+0^2=1
2): 32→3^2+2^2=13→1^2+3^2=10→1^2+0^2=1
3): 37→3^2+7^2=58→5^2+8^2=89→8^2+9^2=145→1^2+4^2+5^2=42→4^2+2^2=20→2^2+0^2=4→4^2=16→1^2+6^2=37 ...
So 28 and 32 are happy numbers, and in the 37 calculation, 37 repeats, and the result of the continuation calculation is only the cycle of the above numbers, not 1, so 37 is not a happy number.
Not the number of happy numbers is called the number of unhappy, and the sum of the squares of all the unhappy numbers is counted, and finally goes into the 4→16→37→58→89→145→42→20→4 cycle.
In the decimal, the number of pleasures within 100 (Oeis in the series A00770): 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100.
So what is the number of happiness? First of all, happiness is the evidence that mathematicians enjoy math and find fun in math. They deal with numbers, and play like we're ordinary people. ) Play with toys. Then they decided that some numbers were happy because they all ended up with "1" and those unhappy numbers (they really called unhappiness ...). ) can only be lost in endless cycles. In addition, there is an infinite, frightening "4".
Sometimes when I look at a paper on mathematics, I think of this vision: the extraordinary, precise, beautiful images that mathematics can depict, pictures, even human instincts. Such mathematics can predict the movement of photons, the grandeur of the universe, the intersection of dimensions, the drift of the Milky Way.
The rest of the time, the math gave me the impression that the mathematician took a picture drawing, the above three ellipse and a smiley face, and then the mathematician excitedly shouted to people: "I succeeded!" "I found out!" "or" I prove it! "or something like that. Happiness, for me, is the latter-the happy numbers, the mysterious foes, the 4 spiral, which are all interesting, but I don't think it's a big discovery.
It's easy to find a happy number. Pick a number randomly, separate each bit, each number squared, and then add up the number of squares, such as the cluster reciprocating, if the final 1, the number is defined as happy.
But what can this happy number do? Of course, some numbers do do these amazing things, and some numbers don't. But what is the point? Even mathematicians themselves do not know, but they are certain that the final result is not 1 of the number, the end will end with 4. Once a number has been stained with "4", there is an endless loop waiting for it. I certainly do not study the whole history of mathematics for this phenomenon, but the law of those numbers appears: 4,16,37,58,89,145,42,20,4, then cycle, recycle, recycle ... This is an unhappy number.
Before you ask this question, I would say that there is no "use" of these pleasures at the moment, and it is only a fact that mathematicians have been trying to dig for years.
#这样说吧, this strange phenomenon, even the Masonic conspiracy theorists disdain to use. The only thing I can think of is the one that NASA used to launch prime numbers into space--for the attention of other civilizations. But the fact that it is too painful, I think even the aliens would not think this is a big deal.
But is it God's will that a simple square process can attribute all the numbers to two species? Or just the charm of mathematics itself.
As for why the unhappy number will eventually return to 4, it is unclear. Submitted:1 minute ago
202 Happy Number