diff options
author | Andinus <andinus@nand.sh> | 2021-08-25 20:09:37 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2021-08-25 20:09:55 +0530 |
commit | 094df4feb1fc02197029764e4f748c9680150574 (patch) | |
tree | f4e31ec4f794f09e8aa84aeb06e693e33b9eef24 /c/sum-of-multiples/src | |
parent | 7ec482f4126a6ac5bff7fffaa16c84210df13fac (diff) | |
download | exercism-094df4feb1fc02197029764e4f748c9680150574.tar.gz |
C: Solution for sum-of-multiples
Diffstat (limited to 'c/sum-of-multiples/src')
-rw-r--r-- | c/sum-of-multiples/src/sum_of_multiples.c | 17 | ||||
-rw-r--r-- | c/sum-of-multiples/src/sum_of_multiples.h | 9 |
2 files changed, 26 insertions, 0 deletions
diff --git a/c/sum-of-multiples/src/sum_of_multiples.c b/c/sum-of-multiples/src/sum_of_multiples.c new file mode 100644 index 0000000..9ad1eb1 --- /dev/null +++ b/c/sum-of-multiples/src/sum_of_multiples.c @@ -0,0 +1,17 @@ +#include "sum_of_multiples.h" +#include <stdlib.h> +#include <stdbool.h> + +unsigned int sum(const unsigned int *factors, + const size_t number_of_factors, const unsigned int limit) { + unsigned int total = 0; + bool *seen = calloc(limit, sizeof(*seen)); + for (size_t idx = 0; idx < number_of_factors; idx++) + for (size_t num = 0; num < limit; num++) + // factors[idx] shouldn't be 0. (% fails) + if (!seen[num] && factors[idx] && !(num % factors[idx])) { + total += num; + seen[num] = true; + } + return total; +} diff --git a/c/sum-of-multiples/src/sum_of_multiples.h b/c/sum-of-multiples/src/sum_of_multiples.h new file mode 100644 index 0000000..35dd799 --- /dev/null +++ b/c/sum-of-multiples/src/sum_of_multiples.h @@ -0,0 +1,9 @@ +#ifndef SUM_OF_MULTIPLES_H +#define SUM_OF_MULTIPLES_H + +#include <stddef.h> + +unsigned int sum(const unsigned int *factors, + const size_t number_of_factors, const unsigned int limit); + +#endif |