diff --git a/Project-Euler/001/ArithmeticProgression.c b/Project-Euler/001/ArithmeticProgression.c new file mode 100644 index 0000000..5600bf3 --- /dev/null +++ b/Project-Euler/001/ArithmeticProgression.c @@ -0,0 +1,36 @@ +#include +#include +#include +#include + +// Use: +// bruteforce +// n numbers to calculate (PE 1) +// number #1 to calculate (PE 1000) +// number #2 to calculate +// ... +// number n to calculate + +// Prototype +long sum_of_divisible(long divisible, long last); + +int main() { + int n; + scanf("%d",&n); + long arr[n]; + for(int i = 0; i < n; i++){ + scanf("%li",&arr[i]); + } + + for (int i = 0; i < n; i++) + { + printf("%li\n", sum_of_divisible(3, arr[i] - 1) + sum_of_divisible(5, arr[i] - 1) - sum_of_divisible(15, arr[i] - 1)); + } +} + + +// finds the sum of all the numbers divisible by "divisible" up to and including "last" using Arithmetic Progression +long sum_of_divisible(long divisible, long last) +{ + return (long) divisible*(last/divisible)*((last/divisible)+1)/2; +}