aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-18 20:49:50 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-18 20:49:50 +0100
commit77cf528cf5b14b3beb067f560753b1e5a3960bbd (patch)
tree28260acc930868c822ec94288401fe6ba2a2a8d5
parent4c69de67681a1bb6e78c9adf1072a5f3b459e4c9 (diff)
downloadproject_euler-77cf528cf5b14b3beb067f560753b1e5a3960bbd.tar.gz
project_euler-77cf528cf5b14b3beb067f560753b1e5a3960bbd.tar.bz2
project_euler-77cf528cf5b14b3beb067f560753b1e5a3960bbd.zip
problem 23 c
-rw-r--r--c/023-nonabundant_sums.c60
-rw-r--r--haskell/wip/023-non_abundant_sum.hs10
2 files changed, 65 insertions, 5 deletions
diff --git a/c/023-nonabundant_sums.c b/c/023-nonabundant_sums.c
new file mode 100644
index 0000000..badc979
--- /dev/null
+++ b/c/023-nonabundant_sums.c
@@ -0,0 +1,60 @@
+/*
+ * Non-abundant sums
+ * Problem 23
+ *
+ * A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
+ * A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
+ * As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
+ * Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
+ */
+
+#include <stdio.h>
+#include <math.h>
+
+int div_sum(int num)
+{
+ int result = 0;
+
+ for (int i = 2; i <= sqrt(num); i++)
+ {
+ if (num % i == 0)
+ {
+ if (i == num / i)
+ result += i;
+ else
+ result += (i + num / i);
+ }
+ }
+ return (result + 1);
+}
+
+int has_two_sum(int *nums, int nums_size, int target)
+{
+ for (int i = 0; i < nums_size; i++)
+ {
+ if (nums[i] >= target)
+ return 0;
+ for (int j = i; j < nums_size; j++)
+ if (nums[i] + nums[j] == target)
+ return 1;
+ }
+ return 0;
+}
+
+int main(void)
+{
+ int nums[28123 + 10];
+ int start = 0;
+ for (int n = 1; n < 28123; n++)
+ if (div_sum(n) > n)
+ nums[start++] = n;
+
+ int sum = 0;
+ for (int n = 1; n < 28123; n++)
+ if (!has_two_sum(nums, start, n))
+ sum += n;
+
+ printf("%d\n", sum);
+
+ return 0;
+}
diff --git a/haskell/wip/023-non_abundant_sum.hs b/haskell/wip/023-non_abundant_sum.hs
index d519eb6..9174aef 100644
--- a/haskell/wip/023-non_abundant_sum.hs
+++ b/haskell/wip/023-non_abundant_sum.hs
@@ -46,12 +46,12 @@ notAbundantSum x
abundants = [n | n <- [1..28123], divSum n > n]
divSum :: Int -> Int
-divSum n = factorise 2
- where factorise d
+divSum n = factorize 2
+ where factorize d
| d > nSqrt = 1
- | rest == 0 && d /= quotient = d + quotient + factorise (d + 1)
- | rest == 0 && d == quotient = quotient + factorise (d + 1)
- | otherwise = factorise (d + 1)
+ | rest == 0 && d /= quotient = d + quotient + factorize (d + 1)
+ | rest == 0 && d == quotient = quotient + factorize (d + 1)
+ | otherwise = factorize (d + 1)
where quotient = n `div` d
rest = n `mod` d
nSqrt = floor $ sqrt $ fromIntegral n