diff options
-rwxr-xr-x | smallmult | bin | 0 -> 16648 bytes | |||
-rw-r--r-- | smallmult.c | 23 | ||||
-rw-r--r-- | sumexp.py | 2 | ||||
-rwxr-xr-x | sumofprimes | bin | 0 -> 16904 bytes | |||
-rw-r--r-- | sumofprimes.c | 50 | ||||
-rw-r--r-- | sumofprimes.py | 17 | ||||
-rw-r--r-- | sumsq.py | 28 |
7 files changed, 73 insertions, 47 deletions
diff --git a/smallmult b/smallmult Binary files differnew file mode 100755 index 0000000..5f4e692 --- /dev/null +++ b/smallmult diff --git a/smallmult.c b/smallmult.c new file mode 100644 index 0000000..302f19b --- /dev/null +++ b/smallmult.c @@ -0,0 +1,23 @@ +#include <stdlib.h> +#include <stdio.h> + +// num: the number to divide +// gd: the greatest divisor to count up to +int isDivisible(int num, int gd){ + int divisible = 1; + for(int i = 1; i < gd; i++){ + if(num % i != 0) + divisible = 0; + } + return divisible; +} + +int main(){ + int found = 0; + int start = 2520; + while(!found){ + start += 20; + found = isDivisible(start, 20); + } + printf("%d\n", start); +} @@ -1,5 +1,3 @@ -import PIL -import math # Problem 16 Power digit sum n = 2**1000 strn = str(n) diff --git a/sumofprimes b/sumofprimes Binary files differnew file mode 100755 index 0000000..a175f6a --- /dev/null +++ b/sumofprimes diff --git a/sumofprimes.c b/sumofprimes.c new file mode 100644 index 0000000..90e0bbb --- /dev/null +++ b/sumofprimes.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +// The number to find primes up to +int n = 2000000; + +// The number of primes in the primes array +int s = 0; + +int *listPrimes(int num){ + int i; + int *primes; + int *sieve = (int *) malloc(num * sizeof(int)); + //initialize to all 1s (except 0 and 1 which are not prime) + for(i = 2; i < num; i++) + sieve[i] = 1; + for(i = 2; i < ceil(sqrt(num)); i++){ + if(sieve[i] == 1){ + int j = i * i; + while(j < num){ + sieve[j] = 0; + j += i; + } + } + } + + //now check which were prime + primes = (int *) malloc(sizeof(int)); + for(i = 2; i < num; i++){ + if(sieve[i]){ + primes[s] = i; + s++; + primes = (int *) realloc (primes, (s + 1) * sizeof(int)); + } + } + + return primes; +} + +int main(){ + int *p = listPrimes(n); + long long sum = 0; + int len = s; + for(int i = 0; i < s; i++){ + sum += p[i]; + } + printf("%ld\n", sum); + return 0; +} diff --git a/sumofprimes.py b/sumofprimes.py index e85e394..d0db224 100644 --- a/sumofprimes.py +++ b/sumofprimes.py @@ -2,23 +2,6 @@ import PIL import math # Problem 10 sum of primes -###INEFFICIENT### -# def isPrime(number): -# if (number <=1): -# return False -# for i in range(2,number): -# if(number%i==0): -# return False -# return True - -# def prime_factors(number): -#primes = [] -# for i in range(number, 2, -1): -# if(number%i==0): -# if (isPrime(i)): -# return i -# return primes - def prime_factors(number): primes = [] diff --git a/sumsq.py b/sumsq.py deleted file mode 100644 index fdd43e4..0000000 --- a/sumsq.py +++ /dev/null @@ -1,28 +0,0 @@ -# The sum of the squares of the first ten natural numbers is, -# 12 + 22 + ... + 102 = 385 -# The square of the sum of the first ten natural numbers is, -# (1 + 2 + ... + 10)2 = 552 = 3025 -# Hence the difference between the sum of the squares -# of the first ten natural numbers and the square of the sum is -# Find the difference between the sum of the squares of the first -# one hundred natural numbers and the square of the sum. - -import PIL -import math - -nums = range(1, 11) -numsq = range(1, 11) - -for i in nums: - i = i*i - -print(nums) -print(numsq) - -sum1 = (sum(nums)**2) -sum2 = (sum(numsq)) - -print("The sum squared is: " + str(sum1)) -print("The sum of the squares is: " + str(sum2)) - -print("The difference is: " + str(abs(sum2-sum1))) |