when to stop when number is not a happy number

后端 未结 4 680
春和景丽
春和景丽 2020-12-22 07:19

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 un

4条回答
  •  萌比男神i
    2020-12-22 07:58

    You would have to keep a record of all the numbers you have produced so far in the sequence, and if one of them comes up a second time you know you have a loop which will never reach 1. A set is probably a good choice for a place to store the numbers.

提交回复
热议问题