summary refs log blame commit diff stats
path: root/c/sum-of-multiples/src/sum_of_multiples.c
blob: 9ad1eb111485d908497dfc8394001966accc4f4d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}