202 Happy Number

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

bool isHappy(int n) 
{
    int rem, m;
    
    rem = m = 0; // 分别为余数,新数
    
    if ( n <= 0 )
    {
        return false;
    }
        
    while (n)
    {
        rem = n%10;
        n   = n/10;
        m  += rem*rem;
        
        if ( n == 0 ) // 一次循环结束
        {
            if ( m == 1 ) // 得到快乐数
                return true;
            else if ( m == 4 ) 
                return false;
                
            n = m;
            rem = m = 0; // 继续下一轮
        }
    }
    
    return false;
}

400 / 400 test cases passed.
Status: 
Accepted
Runtime:  3 ms

陷阱:

将以下两行语句颠倒引发错误
  rem = n%10;
  n   = n/10;


快乐数解释:(以下引自 关于“快乐数”的一些事

首先,进行一下“快乐数”的科普是很有必要的。它指的不是什么幸运数字,而是数学中的一个有趣的现象:

快乐数(happy number)有以下的特性:在给定的进位制下,该数字所有数位(digits)的平方和,得到的新数再次求所有数位的平方和,如此重复进行,最终结果必为1。
例如,以十进位为例:

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……

因此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。

那么快乐数到底是什么呢?首先从根本上来说,快乐数是数学家们享受数学,在数学中发现乐趣的证据。他们和数字打交道,玩耍的方式就像我们普通人(...)玩玩具一样。然后,他们就决定,有些数字是快乐的,因为它们都能以“1”结束,而那些不快乐的数字(他们真的叫不快乐数...)只能迷失在无尽的不循环当中。除此之外,还有无限的,令人恐惧的“4”.

有些时候我看到关于数学的论文时,我会想到这种景象:数学能够描绘出的那些非凡的,精确的,美丽的图景,这些景象是照片,甚至是人的本能都无法做到的。这样的数学,可以预测光子的运动,宇宙的雄壮,维度之间的交汇,银河的漂移。

剩下的时候,数学给我的印象就是,数学家拿着一张绘图纸,上面画了三个椭圆还有一个笑脸,然后数学家向人们兴奋地大喊:“我成功了!”,“我发现了!”或是“我证明出来了!”之类的。快乐数,对我来说,就是后者——快乐的数字,神秘的宿敌,4之螺旋,这些都是有趣的现象,不过我并不认为这是什么重大的发现。

发现一个快乐数是很简单的事。随便挑一个数字,把每一位都分离出来,每个数字平方,再将平方后的数字加起来,如簇往复,如果最终得到了1,这个数字就被定义为是快乐的。

但是,这个快乐数又能干什么呢?当然了,有些数字确实能做这些神奇的事情,有些数字不能。但是这有什么意义呢?就连数学家们自己都不知道,只是他们确定的是,那些最终结果不是1的数字,最终都会以4结束。一旦一个数字沾上了“4”,等待它的就是无尽的循环。我当然不会为了这一现象就去研究整个数学史,但是那些数字出现的规律是:4,16,37,58,89,145,42,20,4,然后就是循环,再循环,再循环 ...这就是一个不快乐的数字。

在大家问起这个问题之前,我要说,目前这些快乐数还没有任何“用处”,它只是多年以来数学家们一直在试图挖掘的一个事实。

#这样说吧,这个奇怪的现象,就连那些共济会的阴谋论者都不屑于使用。我唯一能想到的就是当年 NASA 用过的,向太空发射质数的那一招——用于引起其他文明的注意。不过这个事实实在是太蛋疼了,我想就连外星人都不会认为这有什么大不了的。

不过,一个简单的平方过程就能将所有的数字归于两个种类之中,这难道是上帝的旨意?或者只是数学本身的魅力。


至于为什么不快乐数最后一定会回归到4,暂时还不清楚。
Submitted:  1 minute ago

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(Telephone Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table: Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indi- cated in the above table to develop the seven-letter word “NUMBERS.” Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtaposi- tions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.” Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值