55import java .util .Scanner ;
66import java .util .Set ;
77
8- /**
9- * A utility class for working with Happy Numbers.
10- *
11- * <p>
12- * A Happy Number is defined by the following process:
13- * Starting with any positive integer, replace the number by the sum of the
14- * squares of its digits.
15- * Repeat the process until the number equals 1 (where it will stay), or it
16- * loops endlessly in a
17- * cycle which does not include 1.
18- * Those numbers for which this process ends in 1 are happy numbers, while those
19- * that do not end
20- * in 1 are unhappy (or sad) numbers.
21- *
22- * <p>
23- * For example:
24- * <ul>
25- * <li>7 is a happy number: 7 → 49 → 97 → 130 → 10 → 1</li>
26- * <li>2 is not a happy number (sad number): 2 → 4 → 16 → 37 → 58 → 89 → 145 →
27- * 42 → 20 → 4 (cycle)</li>
28- * </ul>
29- *
30- * @see <a href="https://en.wikipedia.org/wiki/Happy_number">Happy Number -
31- * Wikipedia</a>
32- * @see <a href="https://mathworld.wolfram.com/HappyNumber.html">Happy Number -
33- * Wolfram MathWorld</a>
34- */
358public final class HappyNumbersSeq {
369 private HappyNumbersSeq () {
3710 }
3811
39- /**
40- * Known cycle numbers that indicate a sad number.
41- * If the sequence reaches any of these numbers, it will cycle indefinitely
42- * without reaching 1.
43- */
4412 private static final Set <Integer > CYCLE_NUMS = new HashSet <>(Arrays .asList (4 , 16 , 20 , 37 , 58 , 145 ));
4513
46- /**
47- * Main method to demonstrate happy number detection.
48- * Reads a number from user input and displays the sequence until it reaches 1
49- * (happy)
50- * or enters a cycle (sad).
51- *
52- * @param args command-line arguments (not used)
53- */
5414 public static void main (String [] args ) {
5515 Scanner in = new Scanner (System .in );
5616 System .out .print ("Enter number: " );
@@ -64,47 +24,16 @@ public static void main(String[] args) {
6424 in .close ();
6525 }
6626
67- /**
68- * Determines if a number is a happy number.
69- *
70- * @param n the number to check (must be positive)
71- * @return {@code true} if the number is happy, {@code false} otherwise
72- * @throws IllegalArgumentException if n is not positive
73- */
74- public static boolean isHappy (int n ) {
75- if (n <= 0 ) {
76- throw new IllegalArgumentException ("Number must be positive" );
27+ private static int sumSquares (int n ) {
28+ int s = 0 ;
29+ for (; n > 0 ; n /= 10 ) {
30+ int r = n % 10 ;
31+ s += r * r ;
7732 }
78- while (n != 1 && !isSad (n )) {
79- n = sumSquares (n );
80- }
81- return n == 1 ;
82- }
83-
84- /**
85- * Computes the sum of the squares of the digits of a number.
86- *
87- * @param n the number whose digits will be squared and summed
88- * @return the sum of the squares of the digits
89- */
90- static int sumSquares (int n ) {
91- int sum = 0 ;
92- while (n > 0 ) {
93- int digit = n % 10 ;
94- sum += digit * digit ;
95- n /= 10 ;
96- }
97- return sum ;
33+ return s ;
9834 }
9935
100- /**
101- * Checks if a number is part of the known cycle that indicates a sad number.
102- *
103- * @param n the number to check
104- * @return {@code true} if the number is in the sad cycle, {@code false}
105- * otherwise
106- */
107- static boolean isSad (int n ) {
36+ private static boolean isSad (int n ) {
10837 return CYCLE_NUMS .contains (n );
10938 }
11039}
0 commit comments