当前位置:网站首页>20220531 Mathematics: Happy numbers
20220531 Mathematics: Happy numbers
2022-07-03 10:11:00 【Seeyouagain】
Title Description : Write an algorithm to judge a number n Is it a happy number .
Happiness number is defined as : For a positive integer , Replace the number with the sum of the squares of the numbers in each of its positions . Then repeat the process until the number becomes 1, It could be Infinite loop But it doesn't change 1. If this process The result is 1, So this number is the happy number .
If n yes Happy number Just go back to true ; No , Then return to false .
coded :
public static boolean isHappy(int n) {
if (n == 1) {
return true;
} else {
Set<Integer> set = new HashSet<>();
while (n != 1) {
int temp = n, sum = 0;
while (temp >= 10) {
int mod = temp % 10;
temp /= 10;
sum += mod * mod;
}
sum += temp * temp;
if (!set.add(sum)) {
return false;
}
n = sum;
}
}
return true;
}边栏推荐
- Pycharm cannot import custom package
- 20220531数学:快乐数
- Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
- QT detection card reader analog keyboard input
- openCV+dlib实现给蒙娜丽莎换脸
- Markdown latex full quantifier and existential quantifier (for all, existential)
- 2. Elment UI date selector formatting problem
- QT self drawing button with bubbles
- CV learning notes - feature extraction
- LeetCode - 933 最近的请求次数
猜你喜欢

03 fastjason solves circular references

LeetCode - 706 设计哈希映射(设计) *

Basic knowledge of communication interface

Swing transformer details-2

Opencv feature extraction - hog

Leetcode-513:找树的左下角值

MySQL root user needs sudo login

Opencv notes 17 template matching

Deep learning by Pytorch

El table X-axis direction (horizontal) scroll bar slides to the right by default
随机推荐
yocto 技術分享第四期:自定義增加軟件包支持
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
20220610其他:任务调度器
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
20220609其他:多数元素
getopt_ Typical use of long function
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
Flutter 退出当前操作二次确认怎么做才更优雅?
4G module designed by charging pile obtains signal strength and quality
Opencv notes 17 template matching
Opencv feature extraction - hog
RESNET code details
Serial communication based on 51 single chip microcomputer
On the problem of reference assignment to reference
Opencv+dlib to change the face of Mona Lisa
Screen display of charging pile design -- led driver ta6932
LeetCode - 933 最近的请求次数
Leetcode-112:路径总和
yocto 技术分享第四期:自定义增加软件包支持