summary refs log blame commit diff stats
path: root/c/armstrong-numbers/src/armstrong_numbers.c
blob: 8c0edfd31b65c9d2608032ff2e4522092d6d1c10 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
















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

bool is_armstrong_number(unsigned int num) {
    // log10(0) returns `HUGE_VAL' or `-∞'.
    if (!num) return true;

    unsigned short len = log10(num) + 1;
    unsigned int arms = 0, num_cp = num;

    for (; num != 0; num /= 10) {
        unsigned short rem = num % 10;
        arms += pow(rem, len);
    }

    return num_cp == arms;
}