From abcb2bc3888ffc91dbab3a12c5a76f019439191d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B8vbr=C3=B8tte=20Olsen?= Date: Wed, 30 Dec 2015 15:28:28 +0100 Subject: [PATCH] Added mathermatical solution --- Project-Euler/001/ArithmeticProgression.c | 36 +++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Project-Euler/001/ArithmeticProgression.c 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; +}