summaryrefslogtreecommitdiff
path: root/euler120.c
diff options
context:
space:
mode:
authorIru Cai <mytbk920423@gmail.com>2018-05-27 14:05:48 +0800
committerIru Cai <mytbk920423@gmail.com>2018-05-27 14:05:48 +0800
commitd1be3105ba259970ebfcf71370fb4baca89abd86 (patch)
tree5c1548ec9ed65e0d1c732b8553ac566995aaf341 /euler120.c
parent12b78407f3ce4d6ee5f67fb0c6ce5c754cf78a58 (diff)
downloadproject_euler-d1be3105ba259970ebfcf71370fb4baca89abd86.tar.xz
72,120
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);
+}
+