aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormjfernez <mjfernez@gmail.com>2020-02-16 16:42:23 -0500
committermjfernez <mjfernez@gmail.com>2020-02-16 16:42:23 -0500
commit64c113b89b96b1cbdec8ba6b3323aeff77ca0c85 (patch)
tree3f877d934a27ee12fc40e01b6f7b17b2f22e659c
parentd21e516c4ec3ab5b85f52df9aa7daa7f020b5f7b (diff)
downloadProject_Euler_Solutions-64c113b89b96b1cbdec8ba6b3323aeff77ca0c85.tar.gz
Added sumsq.c. Fixed pyhton solution for the same
-rwxr-xr-x06-Sum-Square-Difference/sumsqbin0 -> 16616 bytes
-rw-r--r--06-Sum-Square-Difference/sumsq.c20
-rw-r--r--06-Sum-Square-Difference/sumsq.py9
3 files changed, 23 insertions, 6 deletions
diff --git a/06-Sum-Square-Difference/sumsq b/06-Sum-Square-Difference/sumsq
new file mode 100755
index 0000000..8f8aa03
--- /dev/null
+++ b/06-Sum-Square-Difference/sumsq
Binary files differ
diff --git a/06-Sum-Square-Difference/sumsq.c b/06-Sum-Square-Difference/sumsq.c
new file mode 100644
index 0000000..1de2ebe
--- /dev/null
+++ b/06-Sum-Square-Difference/sumsq.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+
+// Find the difference between (1+2+3+...+100)^2 and (1^2+2^2+3^2+...+100^2)
+// I used another math trick here:
+// The sum up to a number can be found using Gauss' Trick:
+// 1 + 2 + 3 + ... + 10 -> (1+9) + (2+8) + (3+7) + (4+6) + 10 + 5 -> 5*10 +5
+// In general : (n/2)(n+1)
+
+int main() {
+ long sqsum = 50 * 101;
+ sqsum *=sqsum;
+ printf("The square of the sum is: %lu\n", sqsum);
+ long sumsq = 0;
+ for(long i = 1; i <= 100; i++) {
+ sumsq += i * i;
+ }
+ printf("The sum of the squares is: %lu\n", sumsq);
+ printf("Difference: %lu\n", sqsum - sumsq);
+ return 0;
+}
diff --git a/06-Sum-Square-Difference/sumsq.py b/06-Sum-Square-Difference/sumsq.py
index fdd43e4..6882eb6 100644
--- a/06-Sum-Square-Difference/sumsq.py
+++ b/06-Sum-Square-Difference/sumsq.py
@@ -10,14 +10,11 @@
import PIL
import math
-nums = range(1, 11)
-numsq = range(1, 11)
+nums = range(1, 101)
+numsq = []
for i in nums:
- i = i*i
-
-print(nums)
-print(numsq)
+ numsq.append(i*i)
sum1 = (sum(nums)**2)
sum2 = (sum(numsq))