log10 with no pow

Armstrong Numbers
Armstrong Numbers in C

armstrong_numbers.h

#ifndef ARMSTRONG_NUMBERS
#define ARMSTRONG_NUMBERS
#include <stdbool.h>

bool is_armstrong_number(int candidate);

#endif

armstrong_numbers.c

#include "armstrong_numbers.h"
#include <math.h>

bool is_armstrong_number(int candidate)
{
    if (candidate < 10)
        return true;

    int digit_count = log10(candidate) + 1;

    int num = candidate;
    int pow_total = 0;
    while (num > 0)
    {
        int pow_temp = num % 10;
        int pow_temp_total = 1;
        for (int i = 0; i < digit_count; i++)
            pow_temp_total *= pow_temp;
        pow_total += pow_temp_total;
        num /= 10;
    }
    return candidate == pow_total;
}

This approach starts by checking if the input number is less than 10 (negative numbers are not used by the tests.) If the number is 0 through 9, it is an Armstrong Number, and the function will return true. Otherwise, the number of digits is calculated by passing the number to log10 and adding 1 to the result.

If the digit count is 2, the function could return false right away, since there are no two-digit Armstrong Numbers. In this approach it is skipped, rather than performing that check every time.

A number is defined for calculating down the input number, as well as another number for holding the results of calculating up.

The while loop iterates while the number for calculating down is greater than 0.

The modulo operator is used to get the remainder of dividing the calculate-down number by 10. So, if the number is 153, the remainder of dividing by 10 is 3.

The running total of multiplying 3 is initialized to 1.

The inner for loop iterates for the number of digits. So 3 will be multiplied three times, like so:

  • 1 x 3 = 3
  • 3 x 3 = 9
  • 9 x 3 = 27

which is the same as 3 (the number) to the power of 3 (the digit count).

When the inner for loop is done, the running total of the multiplication is added to the calculating-up variable. Then the number is divided by 10 for the next iteration, making it 15.

5 will be multiplied three times to have the result of 125 added to 27, for a calculating-up value of 152. 15 will be divided by 10 for the next iteration, making it 1.

1 times itself by any amount results in 1, so 1 is added to 152 for a calculating-up value of 153. 1 is divided by 10, making it 0, so the while loop will no longer iterate.

After the while loop is done, the function returns if the calculating-up variable is equal to the original input number.

In the case of the 153 example, they are equal, so the function returns true.

6th Dec 2024 · Found it useful?