Created
March 24, 2024 12:09
-
-
Save robert-saramet/c6a21ddefdc8044b6e8d4bc45ad7b7ab to your computer and use it in GitHub Desktop.
My solution to exercism.org/tracks/c/exercises/armstrong-numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <math.h> | |
| int digits(int n) { | |
| if(n < 10) return 1; | |
| if(n < 100) return 2; | |
| if(n < 1000) return 3; | |
| return 3 + digits(n / 1000); | |
| } | |
| bool is_armstrong_number(int candidate) { | |
| int copy = candidate; | |
| int n = digits(copy); | |
| while(copy) { | |
| candidate -= pow(copy % 10, n); | |
| copy /= 10; | |
| } | |
| return candidate == 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment