当前位置:网站首页>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;
}
边栏推荐
- . DLL and Differences between lib files
- (1) What is a lambda expression
- Tensorflow2.0 save model
- Application of 51 single chip microcomputer timer
- 使用sed替换文件夹下文件
- Design of charging pile mqtt transplantation based on 4G EC20 module
- Leetcode bit operation
- Circular queue related design and implementation reference 1
- 20220606数学:分数到小数
- Opencv feature extraction sift
猜你喜欢
Yocto technology sharing phase IV: customize and add software package support
LeetCode - 715. Range 模块(TreeSet) *****
LeetCode - 5 最长回文子串
CV learning notes - scale invariant feature transformation (SIFT)
CV learning notes - reasoning and training
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
03 fastjason solves circular references
Retinaface: single stage dense face localization in the wild
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
LeetCode - 508. 出现次数最多的子树元素和 (二叉树的遍历)
随机推荐
My notes on intelligent charging pile development (II): overview of system hardware circuit design
3.2 Off-Policy Monte Carlo Methods & case study: Blackjack of off-Policy Evaluation
QT detection card reader analog keyboard input
【C 题集】of Ⅵ
The underlying principle of vector
Serial port programming
51 MCU tmod and timer configuration
4G module board level control interface designed by charging pile
Development of intelligent charging pile (I): overview of the overall design of the system
3.1 Monte Carlo Methods & case study: Blackjack of on-Policy Evaluation
Leetcode-513:找树的左下角值
Opencv interview guide
LeetCode 面试题 17.20. 连续中值(大顶堆+小顶堆)
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
使用sed替换文件夹下文件
03 fastjason solves circular references
QT self drawing button with bubbles
Discrete-event system
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
openEuler kernel 技术分享 - 第1期 - kdump 基本原理、使用及案例介绍