Skip to content
Browse files

Add C solution to p1

  • Loading branch information...
1 parent 7884b2f commit 78ce69c1c72b14e723dc1822af70bd11e6283a27 @mason-larobina committed Oct 22, 2011
Showing with 26 additions and 0 deletions.
  1. +26 −0 001/1.c
View
26 001/1.c
@@ -0,0 +1,26 @@
+// (C) Mason Larobina <mason.larobina@gmail.com>
+// clang -std=c99 -Wall -o a 1.c && time ./a
+
+// === Problem 1 ===
+// If we list all the natural numbers below 10 that are multiples of 3 or 5,
+// we get 3, 5, 6 and 9. The sum of these multiples is 23.
+//
+// Find the sum of all the multiples of 3 or 5 below 1000.
+
+#include <stdio.h>
+
+int
+rsum(int min, int max, int step)
+{
+ int sum = 0;
+ for (int i = min; i < max; i += step)
+ sum += i;
+ return sum;
+}
+
+int
+main(void)
+{
+ printf("%d", rsum(3, 1000, 3) + rsum(5, 1000, 5) - rsum(15, 1000, 15));
+ return 0;
+}

0 comments on commit 78ce69c

Please sign in to comment.
Something went wrong with that request. Please try again.