-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArmstrongNumber.java
More file actions
56 lines (51 loc) · 1.87 KB
/
Copy pathArmstrongNumber.java
File metadata and controls
56 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Problem: Armstrong Number (Narcissistic Number) Check
* Problem Statement: Determine whether a given integer equals the sum of its
* digits each raised to the power of the number of digits.
* Intuition: Split the number into digits, compute digit^count, and compare to
* the original value.
* Approach:
* 1) Count the number of digits.
* 2) Re-traverse the number, summing digit^count.
* 3) Return true if the sum equals the original number.
* Time Complexity: O(d) where d is the number of digits.
* Space Complexity: O(1).
* Edge Cases: Single-digit numbers (always Armstrong), large values.
* Dry Run: n=153 -> digits=3, sum=1^3+5^3+3^3=1+125+27=153 => true.
* Correctness Check: The algorithm matches the mathematical definition exactly.
* Assumption: n is non-negative (standard definition for Armstrong numbers).
*/
class armstrong_number {
/**
* Time Complexity: O(log N) where N is the number, corresponding to the number
* of digits in N.
* Space Complexity: O(1)
*/
static boolean armstrongNumber(int n) {
int og_no = n; // Keep original number to compare later
int count = 0;
int temp = n;
// Count the number of digits
while (temp != 0) {
count++;
temp = temp / 10;
}
int sum = 0;
// Calculate the sum of the digits raised to the power of `count`
while (n != 0) {
int digit = n % 10;
sum += Math.pow(digit, count);
n /= 10;
}
// Return true if the sum equals the original number
return (sum == og_no);
}
public static void main(String args[]) {
int n1 = 152;
if (armstrongNumber(n1)) {
System.out.println("Yes, it is an Armstrong Number\n");
} else {
System.out.println("No, it is not an Armstrong Number\n");
}
}
}