initial commit

This commit is contained in:
2021-08-08 21:11:22 +02:00
commit fe7136d801
130 changed files with 6858 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include "armstrong_numbers.h"
static inline int power(int n, int p) {
int res=n;
/* useless here
* if (p==0)
* return 1;
*/
while (--p)
res*=n;
return res;
}
bool is_armstrong_number(int candidate)
{
int p=1, r=0, tmp=candidate;
while (tmp/=10)
p++;
for (tmp=candidate; tmp; tmp /=10)
r+=power(tmp%10, p);
return r==candidate;
}
#ifdef UNIT_TEST
int main(int ac, char **av)
{
int arg=1, n;
for (; arg<ac; ++arg) {
n=atoi(av[arg]);
printf("armstrong(%d)=%d\n", n, is_armstrong_number(n));
}
}
#endif

View File

@@ -0,0 +1,8 @@
#ifndef ARMSTRONG_NUMBERS
#define ARMSTRONG_NUMBERS
#include <stdbool.h>
bool is_armstrong_number(int candidate);
#endif