•Better than 100%
•Runtime is 6ms
•Time Complexity is O(log n)
bool checkArmstrong(int n) {
// Store the original value of n in p for later comparison
int p = n;
// Initialize sum to 0, which will hold the sum of digits raised to the power of the number of digits
int sum = 0;
/*Calculate the number of digits in the original number p. log10(p) gives the logarithm base 10 of p, adding 1 gives the total number of digits.*/
int count = int(log10(p) + 1);
// Loop while n is greater than 0 (i.e., while there are still digits to process)
while (n > 0) {
// Get the last digit of n using the modulus operator
int ld = n % 10;
// Raise the last digit (ld) to the power of the number of digits (count)
// and add this value to the sum.
sum = sum + pow(ld, count);
// Remove the last digit from n by performing integer division by 10.
n = n / 10;
}
// After processing all digits, check if the calculated sum equals the original number
if (sum == p) {
// If the sum equals the original number, it is an Armstrong number
return true;
} else {
// Otherwise, it is not an Armstrong number
return false;
}
}