Skip to content

Instantly share code, notes, and snippets.

@robert-saramet
Created March 24, 2024 12:09
Show Gist options
  • Select an option

  • Save robert-saramet/c6a21ddefdc8044b6e8d4bc45ad7b7ab to your computer and use it in GitHub Desktop.

Select an option

Save robert-saramet/c6a21ddefdc8044b6e8d4bc45ad7b7ab to your computer and use it in GitHub Desktop.
My solution to exercism.org/tracks/c/exercises/armstrong-numbers
#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