mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 08:17:33 +08:00
40 lines
993 B
Java
40 lines
993 B
Java
package com.thealgorithms.others;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.HashSet;
|
|
import java.util.Scanner;
|
|
import java.util.Set;
|
|
|
|
public final class HappyNumbersSeq {
|
|
private HappyNumbersSeq() {
|
|
}
|
|
|
|
private static final Set<Integer> CYCLE_NUMS = new HashSet<>(Arrays.asList(4, 16, 20, 37, 58, 145));
|
|
|
|
public static void main(String[] args) {
|
|
Scanner in = new Scanner(System.in);
|
|
System.out.print("Enter number: ");
|
|
int n = in.nextInt();
|
|
while (n != 1 && !isSad(n)) {
|
|
System.out.print(n + " ");
|
|
n = sumSquares(n);
|
|
}
|
|
String res = n == 1 ? "1 Happy number" : "Sad number";
|
|
System.out.println(res);
|
|
in.close();
|
|
}
|
|
|
|
private static int sumSquares(int n) {
|
|
int s = 0;
|
|
for (; n > 0; n /= 10) {
|
|
int r = n % 10;
|
|
s += r * r;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
private static boolean isSad(int n) {
|
|
return CYCLE_NUMS.contains(n);
|
|
}
|
|
}
|