当前位置:网站首页>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;
}边栏推荐
- Toolbutton property settings
- Replace the files under the folder with sed
- Dynamic layout management
- [combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
- STM32 running lantern experiment - library function version
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- 51 MCU tmod and timer configuration
- LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
- Opencv histogram equalization
- 20220601数学:阶乘后的零
猜你喜欢

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

ADS simulation design of class AB RF power amplifier

Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa

Deep Reinforcement learning with PyTorch

Pycharm cannot import custom package

CV learning notes alexnet

LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*

Pymssql controls SQL for Chinese queries

CV learning notes - reasoning and training

Leetcode-112:路径总和
随机推荐
20220609其他:多数元素
20220605数学:两数相除
20220604数学:x的平方根
20220603数学:Pow(x,n)
2.1 Dynamic programming and case study: Jack‘s car rental
LeetCode - 900. RLE 迭代器
Opencv Harris corner detection
After clicking the Save button, you can only click it once
Leetcode-100: same tree
4G module designed by charging pile obtains signal strength and quality
. DLL and Differences between lib files
20220602数学:Excel表列序号
Modelcheckpoint auto save model
QT self drawing button with bubbles
Qcombox style settings
03 fastjason solves circular references
Installation and removal of MySQL under Windows
2.2 DP: Value Iteration & Gambler‘s Problem
LeetCode - 919. Full binary tree inserter (array)
LeetCode - 895 最大频率栈(设计- 哈希表+优先队列 哈希表 + 栈) *