由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - LC: happy number怎么保证一定停机或循环?
相关主题
coding test 题问有人做projecteuler吗?
lintcode 上的 Count of Smaller Number before itself做题
Count of Smaller Numbers After SelfAn interview question from a well-known small company
问一题急问 OPT I-94 number的位数问题 包子伺候
求助:求整数的位数,咋就不对了呢?I-94 Number
C++ 一小题finished google two phone interviews 求bless
[合集] Amazon onsite 面经Jane Street 二面
请问如何安全地reverse 一个integer今天电面考了Happy Number, 挂了, 求指导
相关话题的讨论汇总
话题: digits话题: number话题: lc话题: 停机话题: 循环
进入JobHunting版参与讨论
1 (共1页)
A*******e
发帖数: 2419
1
不可能出现无限不循环的情况吗?如何证明?停机条件怎么写呢?
h*c
发帖数: 23
2
During the processing, it will get smaller (see Wiki), so detecting looping
or 1 works:
To see this fact, first note that if n has m digits, then the sum of the
squares of its digits is at most 9^2 m, or 81m.
For m=4 and above, n > 10^{m-1} > 81m
so any number over 1000 gets smaller under this process and in particular
becomes a number with strictly fewer digits. Once we are under 1000, the
number for which the sum of squares of digits is largest is 999, and the
result is 3 times 81, that is, 243.
i******o
发帖数: 171
3
solution0: while(n != 1)
solution1: while(n != 1 && n != 4);
solution2: for(int i = 0; i < 8; i++);
all above three solution works and pass the leetcode OJ.

【在 A*******e 的大作中提到】
: 不可能出现无限不循环的情况吗?如何证明?停机条件怎么写呢?
A*******e
发帖数: 2419
4
why they work? any proof like the 2nd floor?
passing OJ doesn't mean it's right. the correctness of an algorithm cannot
be assured by a set of test cases.

【在 i******o 的大作中提到】
: solution0: while(n != 1)
: solution1: while(n != 1 && n != 4);
: solution2: for(int i = 0; i < 8; i++);
: all above three solution works and pass the leetcode OJ.

l******s
发帖数: 3045
5
不会。个位数平方最大81,不管起始多么长的数字,到最后都会缩短到不超过三位数内
的循环,循环一千次总有重复的。

【在 A*******e 的大作中提到】
: 不可能出现无限不循环的情况吗?如何证明?停机条件怎么写呢?
1 (共1页)
进入JobHunting版参与讨论
相关主题
今天电面考了Happy Number, 挂了, 求指导求助:求整数的位数,咋就不对了呢?
leetcode一道新题我不懂C++ 一小题
请教一下leetcode #321. Create Maximum Number[合集] Amazon onsite 面经
微软一道 智力题请问如何安全地reverse 一个integer
coding test 题问有人做projecteuler吗?
lintcode 上的 Count of Smaller Number before itself做题
Count of Smaller Numbers After SelfAn interview question from a well-known small company
问一题急问 OPT I-94 number的位数问题 包子伺候
相关话题的讨论汇总
话题: digits话题: number话题: lc话题: 停机话题: 循环