summaryrefslogtreecommitdiff
path: root/euler120.c
diff options
context:
space:
mode:
Diffstat (limited to 'euler120.c')
-rw-r--r--euler120.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/euler120.c b/euler120.c
new file mode 100644
index 0000000..48e63e6
--- /dev/null
+++ b/euler120.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+
+/* (a-1)^n + (a+1)^n mod a^2
+ * = n*a*(-1)^(n-1) + (-1)^n + n*a + 1
+ * = na*[(-1)^(n-1)+1] + (-1)^n + 1
+ * = if (n is odd) 2na else 2
+ */
+
+int rmax(int a)
+{
+ int n = a / 2;
+ if (a % 2 == 0)
+ n--;
+ return 2 * n * a;
+}
+
+int main()
+{
+ int sum = 0;
+ for (int a = 3; a <= 1000; a++)
+ sum += rmax(a);
+ printf("%d\n", sum);
+}
+