Armstrong Number
Given a non-negative integer num, check whether the given number is an Armstrong number or not.
An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits in the number.
Examples :
Input: 1634
Output: true
Explanation: Yes, 1^4 + 6^4 + 3^4 + 4^4 = 1634.
Input: 1253
Output: false
Explanation: As 1^4 + 2^4 + 5^4 + 3^4 is not equal to 1253, it is not an Armstrong number.
From the definition of the Armstrong number, we know that the Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits(let's say k) in the number.
Let a number 1634 have k=4 digits in it, so to check whether that number is an Armstrong number or not we have to add all those digits raised to the power k and sum up them. But before doing this operation, we must count the number of digits(k) in the given number. Then proceed with the operations discussed above.
Code Implementation
C++ Code Try on Compiler!
// Function to check if a number is an Armstrong number
bool isArmstrong(int num) {
// Store the original number for comparison later
int originalNum = num;
// Initialize sum to store the sum of digits raised to the power n
int sum = 0;
// Initialize n to count the number of digits in the number
int n = 0;
// Calculate the number of digits
while (originalNum != 0) {
originalNum /= 10; // Remove the last digit from the number
n++; // Increment digit count
}
// Reset originalNum to the input number for further calculation
originalNum = num;
// Calculate the sum of the digits each raised to the power of n
while (originalNum != 0) {
// Extract the last digit of the number
int digit = originalNum % 10;
// Add the digit raised to the power of n to sum
sum += pow(digit, n);
// Remove the last digit from the number
originalNum /= 10;
}
// Return true if the sum equals the original number, false otherwise
return sum == num;
}
Java Code Try on Compiler!
public class ArmstrongCheck {
public static boolean isArmstrong(int num) {
int originalNum = num;
int n = String.valueOf(num).length(); // Calculate the number of digits
int sum = 0;
while (originalNum != 0) {
int digit = originalNum % 10;
sum += Math.pow(digit, n); // Add the digit raised to the power n
originalNum /= 10;
}
return sum == num;
}
}
Python Code Try on Compiler!
def is_armstrong(num):
# Convert number to a string to calculate the number of digits
n = len(str(num))
# Calculate the sum of each digit raised to the power of n
sum_of_powers = sum(int(digit) ** n for digit in str(num))
# Check if the sum equals the original number
return sum_of_powers == num
Javascript Code Try on Compiler!
function isArmstrong(num) {
const digits = num.toString().split(""); // Get digits as an array
const n = digits.length; // Number of digits
// Calculate the sum of each digit raised to the power of n
const sum = digits.reduce((acc, digit) => acc + Math.pow(parseInt(digit), n), 0);
return sum === num;
}
Time Complexity : O(logn)
The time complexity arises from the number of digits in the number. The number of times the while loop runs depends on the number of digits in the given number num.
For a given number num in a decimal representation, the number of digits is approximately equal to log10(num). This is because each digit represents a power of 10.
The loop executes the first time to find the number of digits O(logn), and then another time to check for the Armstrong number O(logn).
Therefore, the loop runs 2*log10(num) times, which simplifies to O(logn).
Space Complexity : O(1)
We are using only variables like n, sum, and originalnumber which use constant space.
Since the space required by these variables does not depend on the size of the input number num, the auxiliary space is O(1).