202.leetcode Happy Number(easy)[数学 快乐数]
2016-08-05 11:26
363 查看
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, 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.
Example: 19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 =
1
根据题意一个happy数是一个数按照其每位的平方求和得到的新数继续处理最终可以转换成1的数,不快乐的数都会陷入4的循环中,因此每次检测得到的新的数字是否为1或者4,为1则是快乐数,为4则为非快乐数。
快乐数(happy number)有以下的特性:在给定的进位制下,该数字所有数位(digits)的平方和,得到的新数再次求所有数位的平方和,如此重复进行,最终结果必为1。
案例
编辑
以十进位为例:
2 8 → 2²+8²=68 → 6²+8²=100 → 1²+0²+0²=1
3 2 → 3²+2²=13 → 1²+3²=10 → 1²+0²=1
3 7 → 3²+7²=58 → 5²+8²=89 → 8²+9²=145 → 1²+4²+5²=42 → 4²+2²=20 → 2²+0²=4 → 4²=16 → 1²+6²=37……
因此28和32是快乐数,而在37的计算过程中,37重覆出现,继续计算的结果只会是上述数字的循环,不会出现1,因此37不是快乐数。
不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算,最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。
在十进位下,100以内的快乐数有(OEIS中的数列A00770) :1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100。
也许我们能在小于10的进位制之下发现更有趣的东西。这样数字中就不会夹着字母了。167比9的倍数大5,那么在能整除9的进制中,数字的末位是5,看上去比笨拙的7喜庆多了。(当然,这只是对我们习惯了十进制的眼睛来说的,在9进制之下5的含义和我们想象的并不一样。)在9进制中,167写作205,但是我个人更喜欢81进制中的25,它很简洁。
在不同的进位制之下研究167引出了另一个有趣的事实:167是一个严格的非回文数,也就是说它在2和165之间的任何一个进位制之下都不能被写成回文数(正着读和反着读完全一样的数字)。(我们停在165进制的原因是,它是167-2,而任何一个数字n在n-1进制之下都是回文数,看上去都是11的形式。)目前为止,我们还不知道严格非回文数的数目,不过167的下一个非回文数是179,再下一个是223。
上面列出来的这些特征,完全足以证明举办一个庆典的必要性,除此之外,167还是一个安全素数,一个非常cototient质数,一个全循环质数。我特别喜欢最后一个:这意味着存在一个166位的数字,它的每个倍数都是数字的循环排列。也就是说,当你把这个数乘上一个整数之后,得到的积恰好是原来的数的数字,排列顺序相同,但是起点不同,例如142857×2=285714。[1]
A happy number is a number 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.
Example: 19 is a happy number
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 =
1
根据题意一个happy数是一个数按照其每位的平方求和得到的新数继续处理最终可以转换成1的数,不快乐的数都会陷入4的循环中,因此每次检测得到的新的数字是否为1或者4,为1则是快乐数,为4则为非快乐数。
快乐数(happy number)有以下的特性:在给定的进位制下,该数字所有数位(digits)的平方和,得到的新数再次求所有数位的平方和,如此重复进行,最终结果必为1。
案例
编辑
以十进位为例:
2 8 → 2²+8²=68 → 6²+8²=100 → 1²+0²+0²=1
3 2 → 3²+2²=13 → 1²+3²=10 → 1²+0²=1
3 7 → 3²+7²=58 → 5²+8²=89 → 8²+9²=145 → 1²+4²+5²=42 → 4²+2²=20 → 2²+0²=4 → 4²=16 → 1²+6²=37……
因此28和32是快乐数,而在37的计算过程中,37重覆出现,继续计算的结果只会是上述数字的循环,不会出现1,因此37不是快乐数。
不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算,最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。
在十进位下,100以内的快乐数有(OEIS中的数列A00770) :1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100。
也许我们能在小于10的进位制之下发现更有趣的东西。这样数字中就不会夹着字母了。167比9的倍数大5,那么在能整除9的进制中,数字的末位是5,看上去比笨拙的7喜庆多了。(当然,这只是对我们习惯了十进制的眼睛来说的,在9进制之下5的含义和我们想象的并不一样。)在9进制中,167写作205,但是我个人更喜欢81进制中的25,它很简洁。
在不同的进位制之下研究167引出了另一个有趣的事实:167是一个严格的非回文数,也就是说它在2和165之间的任何一个进位制之下都不能被写成回文数(正着读和反着读完全一样的数字)。(我们停在165进制的原因是,它是167-2,而任何一个数字n在n-1进制之下都是回文数,看上去都是11的形式。)目前为止,我们还不知道严格非回文数的数目,不过167的下一个非回文数是179,再下一个是223。
上面列出来的这些特征,完全足以证明举办一个庆典的必要性,除此之外,167还是一个安全素数,一个非常cototient质数,一个全循环质数。我特别喜欢最后一个:这意味着存在一个166位的数字,它的每个倍数都是数字的循环排列。也就是说,当你把这个数乘上一个整数之后,得到的积恰好是原来的数的数字,排列顺序相同,但是起点不同,例如142857×2=285714。[1]
class Solution { public: int getResult(int n) { int result = 0; while(n !=0) { result += pow(n%10,2); n = n/10; } return result; } bool isHappy(int n) { int status = n; for(;;) { status = getResult(status); if(status == 1) break; if(status == 4) break; } if(status == 1) return true; else return false; } };
相关文章推荐
- (easy)LeetCode 202.Happy Number
- leetcode 202(easy)--Happy Number
- [LeetCode]202. Happy Number 快乐数
- leetcode 202 Happy Number(难易度:Easy)
- 【LeetCode】(202)Happy Number(Easy)
- LeetCode 202. Happy Number (Easy)
- 【Leetcode】202-Happy Number【Java实现】【Easy】
- [Leetcode 202, Easy] Happy Number
- Leetcode 202. Happy Number (Easy) (cpp)
- [LeetCode] 202. Happy Number 快乐数
- leetcode 202. Happy Number
- [LeetCode202]Happy Number
- (LeetCode)Happy Number -- 寻找快乐数
- 357.leetcode Count Numbers with Unique Digits(easy)[数学问题 非重复数字]
- 【一天一道LeetCode】#202. Happy Number
- leetcode 202 Happy Number
- leetcode 202. Happy Number
- LeetCode 202. Happy Number
- LeetCode 202. Happy Number
- Leetcode 202. Happy Number