diff options
author | mjfernez <mjfernez@gmail.com> | 2020-02-03 23:04:10 -0500 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2020-02-03 23:04:10 -0500 |
commit | 8e431d287c0e89041506da4c4da57e3e3d657d72 (patch) | |
tree | b6cd296fbdd75d1421fe84a82cf3eec859407dcb | |
parent | e6a7399134b3dc08f9e6e9c9c74e39ac449b8538 (diff) | |
download | Project_Euler_Solutions-8e431d287c0e89041506da4c4da57e3e3d657d72.tar.gz |
cleanup, added c translations for some
-rwxr-xr-x | 10001prime | bin | 0 -> 16880 bytes | |||
-rw-r--r-- | 10001prime.c | 42 | ||||
-rw-r--r-- | 10001prime.py | 52 | ||||
-rw-r--r-- | 10001prime.py.orig | 30 | ||||
-rwxr-xr-x | bigfactors2 | bin | 0 -> 16752 bytes | |||
-rw-r--r-- | bigfactors2.c | 31 | ||||
-rw-r--r-- | bigfactors2.py | 46 | ||||
-rw-r--r-- | bigfactors2.py.orig | 32 | ||||
-rwxr-xr-x | collatz | bin | 0 -> 16688 bytes | |||
-rw-r--r-- | collatz.c | 39 | ||||
-rw-r--r-- | collatz.py | 38 | ||||
-rw-r--r-- | collatz.py.orig | 24 | ||||
-rw-r--r-- | euler1.o | bin | 1640 -> 0 bytes | |||
-rw-r--r-- | fib.py | 39 | ||||
-rw-r--r-- | fib.py.orig | 33 | ||||
-rw-r--r-- | largestprime.py | 80 | ||||
-rw-r--r-- | largestprime.py.orig | 55 | ||||
-rw-r--r-- | palindrome.py | 59 | ||||
-rw-r--r-- | palindrome.py.orig | 40 | ||||
-rw-r--r-- | prodgrid.c~ | 78 | ||||
-rw-r--r-- | prodgrid.o | bin | 3864 -> 0 bytes | |||
-rw-r--r-- | productofdigits.py | 29 | ||||
-rw-r--r-- | productofdigits.py.orig | 18 | ||||
-rw-r--r-- | smallmult.py | 35 | ||||
-rw-r--r-- | smallmult.py.orig | 26 | ||||
-rw-r--r-- | sumexp.py | 9 | ||||
-rw-r--r-- | sumexp.py.orig | 10 | ||||
-rw-r--r-- | sumofprimes.py | 59 | ||||
-rw-r--r-- | sumofprimes.py.orig | 44 | ||||
-rw-r--r-- | sumsq.py | 23 | ||||
-rw-r--r-- | sumsq.py.orig | 27 |
31 files changed, 698 insertions, 300 deletions
diff --git a/10001prime b/10001prime Binary files differnew file mode 100755 index 0000000..a9cc351 --- /dev/null +++ b/10001prime diff --git a/10001prime.c b/10001prime.c new file mode 100644 index 0000000..2ef3bd8 --- /dev/null +++ b/10001prime.c @@ -0,0 +1,42 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +int n = 1000000; + +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 + int s = 0; + 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); + int prime10001 = p[10000]; + printf("%d\n", prime10001); + return 0; +} diff --git a/10001prime.py b/10001prime.py index ecaa8cd..bc51d49 100644 --- a/10001prime.py +++ b/10001prime.py @@ -1,30 +1,34 @@ -import PIL, math +import PIL +import math + +# Problem 7 - 10,001st prime + +# returns a list of every prime up to a certain number -#Problem 7 - 10,001st prime -#returns a list of every prime up to a certain number def listprimes(number): - primes = [] - if (number <=1): - return [1] - a = [1] * number - a[0]=0 - a[1]=0#1 and 0 are not prime - ##Sieve of eratosthenes - for i in range(2,int(math.ceil(math.sqrt(number)))): - if(a[i]==1): - j = i**2 #cross out all the multiples of i, starting with its square - while(j < number): - a[j] = 0 - j+=i - - for k in range(2, number): - #is prime - if(a[k]==1): - primes.append(k) - return primes + primes = [] + if (number <= 1): + return [1] + a = [1] * number + a[0] = 0 + a[1] = 0 # 1 and 0 are not prime + # Sieve of eratosthenes + for i in range(2, int(math.ceil(math.sqrt(number)))): + if(a[i] == 1): + j = i**2 # cross out all the multiples of i, starting with its square + while(j < number): + a[j] = 0 + j += i + + for k in range(2, number): + # is prime + if(a[k] == 1): + primes.append(k) + return primes + out_list = listprimes(1000000) -#print(out_list) -#return 10,001th prime factor +# print(out_list) +# return 10,001th prime factor print(out_list[10000]) diff --git a/10001prime.py.orig b/10001prime.py.orig new file mode 100644 index 0000000..ecaa8cd --- /dev/null +++ b/10001prime.py.orig @@ -0,0 +1,30 @@ +import PIL, math + +#Problem 7 - 10,001st prime + +#returns a list of every prime up to a certain number +def listprimes(number): + primes = [] + if (number <=1): + return [1] + a = [1] * number + a[0]=0 + a[1]=0#1 and 0 are not prime + ##Sieve of eratosthenes + for i in range(2,int(math.ceil(math.sqrt(number)))): + if(a[i]==1): + j = i**2 #cross out all the multiples of i, starting with its square + while(j < number): + a[j] = 0 + j+=i + + for k in range(2, number): + #is prime + if(a[k]==1): + primes.append(k) + return primes + +out_list = listprimes(1000000) +#print(out_list) +#return 10,001th prime factor +print(out_list[10000]) diff --git a/bigfactors2 b/bigfactors2 Binary files differnew file mode 100755 index 0000000..3e409b1 --- /dev/null +++ b/bigfactors2 diff --git a/bigfactors2.c b/bigfactors2.c new file mode 100644 index 0000000..97ba17d --- /dev/null +++ b/bigfactors2.c @@ -0,0 +1,31 @@ +#include <stdlib.h> +#include <stdio.h> +#include <math.h> + +int countFactors(int num){ + int factors = 0; + // Check only up until the square root of the number + int root = (int) ceil(sqrt(num)); + //printf("%d\n", root); + for(int i = 2; i < root; i++){ + if(num % i == 0) + factors+=2; + } + // Correction for perfect square + if(root * root == num) + factors -= 1; + return factors; +} + +int main(){ + int i = 1; + int k = 1; + int j = 0; + while(k < 500){ + j += i; + k = countFactors(j); + i += 1; + } + printf("%d has over 500 factors. Neat!\n", j); + return 0; +} diff --git a/bigfactors2.py b/bigfactors2.py index 41bc13d..9e51502 100644 --- a/bigfactors2.py +++ b/bigfactors2.py @@ -1,32 +1,34 @@ -import PIL, math +import PIL +import math + +# Problem 12 Highly divisible triangular number +# finds the first number with over 500 factors -#Problem 12 Highly divisible triangular number -#finds the first number with over 500 factors def countfactors(num): - factors = 0 - ### we only need to know about the FIRST HALF of factors, - ##one factor implies a second - root = int(math.ceil(math.sqrt(num))) - divs = range(1, root) - for d in divs: - if(num%d == 0): - factors += 2 - - #Correction if the number is a perfect square - if (root * root == num): - factors-=1 - return factors + factors = 0 + # we only need to know about the FIRST HALF of factors, + # one factor implies a second + root = int(math.ceil(math.sqrt(num))) + divs = range(1, root) + for d in divs: + if(num % d == 0): + factors += 2 + + # Correction if the number is a perfect square + if (root * root == num): + factors -= 1 + return factors #### MAIN ##### -i=1 -k=1 +i = 1 +k = 1 j = 0 while(k < 500): - j += i - k=countfactors(j) - print(str(j) + " has " + str(k) + " factors") - i += 1 + j += i + k = countfactors(j) + print(str(j) + " has " + str(k) + " factors") + i += 1 print("Ding! Ding! {} has over 500 factors, wow!".format(j)) diff --git a/bigfactors2.py.orig b/bigfactors2.py.orig new file mode 100644 index 0000000..41bc13d --- /dev/null +++ b/bigfactors2.py.orig @@ -0,0 +1,32 @@ +import PIL, math + +#Problem 12 Highly divisible triangular number +#finds the first number with over 500 factors + +def countfactors(num): + factors = 0 + ### we only need to know about the FIRST HALF of factors, + ##one factor implies a second + root = int(math.ceil(math.sqrt(num))) + divs = range(1, root) + for d in divs: + if(num%d == 0): + factors += 2 + + #Correction if the number is a perfect square + if (root * root == num): + factors-=1 + return factors + + +#### MAIN ##### +i=1 +k=1 +j = 0 +while(k < 500): + j += i + k=countfactors(j) + print(str(j) + " has " + str(k) + " factors") + i += 1 + +print("Ding! Ding! {} has over 500 factors, wow!".format(j)) Binary files differdiff --git a/collatz.c b/collatz.c new file mode 100644 index 0000000..5bbb783 --- /dev/null +++ b/collatz.c @@ -0,0 +1,39 @@ +#include <stdlib.h> +#include <stdio.h> +#include <math.h> +#include <string.h> + +// Temporary storage chain +//long int *chain; +// The longest sequence found so far +//long int *seq; + +// Size of the above arrays +long int c = 0; +long int s = 0; +long int collatz(long int seed){ + //chain = (long int *) realloc (chain, c * sizeof(long int)); + //chain[c - 1] == seed; + c++; + if(seed == 1) + return 0; + if(seed % 2 == 0) + seed = seed / 2; + else + seed = 3 * seed + 1; + //collatz(seed); +} + +int main(){ + for(long int i = 1; i < pow(10, 6); i++){ + c = 0; + //chain = (long int *) malloc(c * sizeof(long int)); + collatz(i); + printf("%d \n", i); + if(c > s){ + s = c; + } + } + printf("Has %d numbers \n", s); + return 0; +} @@ -1,24 +1,28 @@ -import PIL, math +import PIL +import math -#Problem 14 Longest Collatz sequence -#Note, a little slow, takes about 15 seconds. -#There is probably a more efficient solution out there +# Problem 14 Longest Collatz sequence +# Note, a little slow, takes about 15 seconds. +# There is probably a more efficient solution out there chain = [] biggo = [] + def collatz(seed): - chain.append(seed) - if(seed == 1): return 0 - if(seed%2 == 0): - seed = seed/2 - else: - seed = 3*seed +1 - collatz(seed) + chain.append(seed) + if(seed == 1): + return 0 + if(seed % 2 == 0): + seed = seed/2 + else: + seed = 3*seed + 1 + collatz(seed) + -for n in range(1,pow(10,6)): - collatz(n) - if(len(chain)>len(biggo)): - biggo = chain - chain = [] +for n in range(1, pow(10, 6)): + collatz(n) + if(len(chain) > len(biggo)): + biggo = chain + chain = [] print(biggo) -print("Has " + str(len(biggo)) +" numbers.") +print("Has " + str(len(biggo)) + " numbers.") diff --git a/collatz.py.orig b/collatz.py.orig new file mode 100644 index 0000000..404a2ed --- /dev/null +++ b/collatz.py.orig @@ -0,0 +1,24 @@ +import PIL, math + +#Problem 14 Longest Collatz sequence +#Note, a little slow, takes about 15 seconds. +#There is probably a more efficient solution out there +chain = [] +biggo = [] + +def collatz(seed): + chain.append(seed) + if(seed == 1): return 0 + if(seed%2 == 0): + seed = seed/2 + else: + seed = 3*seed +1 + collatz(seed) + +for n in range(1,pow(10,6)): + collatz(n) + if(len(chain)>len(biggo)): + biggo = chain + chain = [] +print(biggo) +print("Has " + str(len(biggo)) +" numbers.") diff --git a/euler1.o b/euler1.o Binary files differdeleted file mode 100644 index 2abffe6..0000000 --- a/euler1.o +++ /dev/null @@ -1,33 +1,34 @@ -import PIL, math +import PIL +import math MAX = 4*10**6 -#Problem 2 Even Fibonnacci numbers +# Problem 2 Even Fibonnacci numbers fib = [1, 1] -k=1; +k = 1 while (True): - n = fib[k] + fib[k-1] - if(n>MAX): - break - else: - fib.append(n); - k+=1 + n = fib[k] + fib[k-1] + if(n > MAX): + break + else: + fib.append(n) + k += 1 -c=0 +c = 0 for i in fib: - num = str(i) - print(num + " ", end='') - c+=1 - if(c%10==0): - print() - + num = str(i) + print(num + " ", end='') + c += 1 + if(c % 10 == 0): + print() + print() print("The sum is: " + str(sum(fib))) -s=0 +s = 0 for i in fib: - if (i%2==0): - s+=i + if (i % 2 == 0): + s += i print("The sum of the even terms is: " + str(s)) diff --git a/fib.py.orig b/fib.py.orig new file mode 100644 index 0000000..77b32fa --- /dev/null +++ b/fib.py.orig @@ -0,0 +1,33 @@ +import PIL, math +MAX = 4*10**6 + +#Problem 2 Even Fibonnacci numbers +fib = [1, 1] + +k=1; +while (True): + n = fib[k] + fib[k-1] + if(n>MAX): + break + else: + fib.append(n); + k+=1 + +c=0 +for i in fib: + num = str(i) + print(num + " ", end='') + c+=1 + if(c%10==0): + print() + +print() + +print("The sum is: " + str(sum(fib))) + +s=0 +for i in fib: + if (i%2==0): + s+=i + +print("The sum of the even terms is: " + str(s)) diff --git a/largestprime.py b/largestprime.py index fa39904..fa53dc5 100644 --- a/largestprime.py +++ b/largestprime.py @@ -1,9 +1,10 @@ -import PIL, math +import PIL +import math import numpy as np -#Problem 3 Largest Prime Factor +# Problem 3 Largest Prime Factor ###INEFFICIENT METHODS### -#def isPrime(number): +# def isPrime(number): # if (number <=1): # return False # for i in range(2,number): @@ -11,45 +12,46 @@ import numpy as np # 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 = [] +# for i in range(number, 2, -1): +# if(number%i==0): +# if (isPrime(i)): +# return i +# return primes def prime_factors(number): - primes = [] - if (number <=1): - return [1] - a = np.ones(number) - a[0]=0 - a[1]=0#not prime - for i in range(2,int(math.ceil(math.sqrt(number)))): - if(a[i]==1): - j = i**2 #cross out all the multiples - while(j < number): - a[j] = False - j+=i - - for k in range(2, number): - #is prime is a factor - if(a[k]==1) and (number%k==0): - primes.append(k) - return primes - + primes = [] + if (number <= 1): + return [1] + a = np.ones(number) + a[0] = 0 + a[1] = 0 # not prime + for i in range(2, int(math.ceil(math.sqrt(number)))): + if(a[i] == 1): + j = i**2 # cross out all the multiples + while(j < number): + a[j] = False + j += i + + for k in range(2, number): + # is prime is a factor + if(a[k] == 1) and (number % k == 0): + primes.append(k) + return primes + + def lpf(number): - factor = 2 - while (number > factor): - if(number % factor == 0): - number = number/factor - factor = 2 - else: - factor +=1 - return factor - + factor = 2 + while (number > factor): + if(number % factor == 0): + number = number/factor + factor = 2 + else: + factor += 1 + return factor + + out = lpf(600851475143) print out diff --git a/largestprime.py.orig b/largestprime.py.orig new file mode 100644 index 0000000..fa39904 --- /dev/null +++ b/largestprime.py.orig @@ -0,0 +1,55 @@ +import PIL, math +import numpy as np +#Problem 3 Largest Prime Factor + +###INEFFICIENT METHODS### +#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 = [] + if (number <=1): + return [1] + a = np.ones(number) + a[0]=0 + a[1]=0#not prime + for i in range(2,int(math.ceil(math.sqrt(number)))): + if(a[i]==1): + j = i**2 #cross out all the multiples + while(j < number): + a[j] = False + j+=i + + for k in range(2, number): + #is prime is a factor + if(a[k]==1) and (number%k==0): + primes.append(k) + return primes + +def lpf(number): + factor = 2 + while (number > factor): + if(number % factor == 0): + number = number/factor + factor = 2 + else: + factor +=1 + return factor + +out = lpf(600851475143) +print out diff --git a/palindrome.py b/palindrome.py index 9690553..be3fe79 100644 --- a/palindrome.py +++ b/palindrome.py @@ -1,40 +1,43 @@ -import PIL, math +import PIL +import math + +# Problem 4 - Palindrome Products -#Problem 4 - Palindrome Products def isPalindrome(number): - numchar = str(number) - middle = len(numchar)/2 - face = numchar[:middle] - ref = numchar[len(numchar):middle-1:-1] - if(face == ref): - return True - else: - return False + numchar = str(number) + middle = len(numchar)/2 + face = numchar[:middle] + ref = numchar[len(numchar):middle-1:-1] + if(face == ref): + return True + else: + return False + def findProduct(number): - for i in range(999,100,-1): - for j in range(999,100,-1): - if(i*j==number): - return [i,j] - + for i in range(999, 100, -1): + for j in range(999, 100, -1): + if(i*j == number): + return [i, j] + + def findMaxPalindrome(): - large = 0 - for i in range(999,100,-1): - for j in range(999,100,-1): - test = i*j - if(isPalindrome(test) and test > large): - large = test - return large + large = 0 + for i in range(999, 100, -1): + for j in range(999, 100, -1): + test = i*j + if(isPalindrome(test) and test > large): + large = test + return large + answer = findMaxPalindrome() print(answer) print("The factors are: " + str(findProduct(answer))) #x = input("Type a palindromic number: ") -#if(isPalindrome(x)): - #print "The factors are: " + str(findProduct(x)) -#else: - #print "not a palindrome" - - +# if(isPalindrome(x)): +# print "The factors are: " + str(findProduct(x)) +# else: +# print "not a palindrome" diff --git a/palindrome.py.orig b/palindrome.py.orig new file mode 100644 index 0000000..9690553 --- /dev/null +++ b/palindrome.py.orig @@ -0,0 +1,40 @@ +import PIL, math + +#Problem 4 - Palindrome Products + +def isPalindrome(number): + numchar = str(number) + middle = len(numchar)/2 + face = numchar[:middle] + ref = numchar[len(numchar):middle-1:-1] + if(face == ref): + return True + else: + return False + +def findProduct(number): + for i in range(999,100,-1): + for j in range(999,100,-1): + if(i*j==number): + return [i,j] + +def findMaxPalindrome(): + large = 0 + for i in range(999,100,-1): + for j in range(999,100,-1): + test = i*j + if(isPalindrome(test) and test > large): + large = test + return large + +answer = findMaxPalindrome() +print(answer) +print("The factors are: " + str(findProduct(answer))) +#x = input("Type a palindromic number: ") + +#if(isPalindrome(x)): + #print "The factors are: " + str(findProduct(x)) +#else: + #print "not a palindrome" + + diff --git a/prodgrid.c~ b/prodgrid.c~ deleted file mode 100644 index 611e036..0000000 --- a/prodgrid.c~ +++ /dev/null @@ -1,78 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -int matrix[20][20]= {{8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8}, - {49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0}, - {81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65}, - {52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91}, - {22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80}, - {24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50}, - {32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70}, - {67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21}, - {24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72}, - {21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95}, - {78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92}, - {16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57}, - {86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58}, - {19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40}, - {04, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66}, - {88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69}, - {4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36}, - {20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16}, - {20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54}, - {1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}}; - -int parseMatrix(int mat[20][20]){ - int maxi, i, j, k, c = 0; - int prod = 1; - for(i; i < 20; i++){ - for(j; j < 16; j++){ - k = j; - prod = 1; - for(k; k<j+4; k++) - printf("(%d,%d)\n", i, k); - prod*=mat[i][k]; - if(prod > maxi) - maxi=prod; - } - } - /* - for(j=0; j < 20; j++){ - for(i=0; i < 16; i++){ - k = i; - prod = 1; - for(k; k<k+4; k++) - prod*=mat[k][j]; - if(prod > maxi) - maxi=prod; - } - } - for(i=0; i < 16; i++){ - for(j=0; j < 16; j++){ - prod = 1; - for(c = 0; c < 4; c++) - prod*=mat[i+c][j+c]; - if(prod > maxi) - maxi=prod; - } - } - - for(i=0; i < 16; i++){ - for(j=19; j > 0; j--){ - prod = 1; - for(c = 0; c < 4; c++) - prod*=mat[i-c][j-c]; - if(prod > maxi) - maxi=prod; - } - } - */ - return maxi; -} - - -int main(){ - int ans = parseMatrix(matrix); - printf("%d\n", ans); - return 0; -}
\ No newline at end of file diff --git a/prodgrid.o b/prodgrid.o Binary files differdeleted file mode 100644 index bd2e679..0000000 --- a/prodgrid.o +++ /dev/null diff --git a/productofdigits.py b/productofdigits.py index ddd5edc..3e7756e 100644 --- a/productofdigits.py +++ b/productofdigits.py @@ -1,18 +1,21 @@ -import PIL, math +import PIL +import math import numpy as np -#Problem 8 - largest Product in a series -bigBoy=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 +# Problem 8 - largest Product in a series +bigBoy = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 + def parseNum(x, r): - search = str(x) - maxi = 0 - for ch in range(0,len(search)-r): - prod = int(search[ch]) - for dig in range(ch+1, ch+r): - prod*=int(search[dig]) - if (prod > maxi): - maxi = prod - return maxi - + search = str(x) + maxi = 0 + for ch in range(0, len(search)-r): + prod = int(search[ch]) + for dig in range(ch+1, ch+r): + prod *= int(search[dig]) + if (prod > maxi): + maxi = prod + return maxi + + print(parseNum(bigBoy, 13)) diff --git a/productofdigits.py.orig b/productofdigits.py.orig new file mode 100644 index 0000000..ddd5edc --- /dev/null +++ b/productofdigits.py.orig @@ -0,0 +1,18 @@ +import PIL, math +import numpy as np + +#Problem 8 - largest Product in a series +bigBoy=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 + +def parseNum(x, r): + search = str(x) + maxi = 0 + for ch in range(0,len(search)-r): + prod = int(search[ch]) + for dig in range(ch+1, ch+r): + prod*=int(search[dig]) + if (prod > maxi): + maxi = prod + return maxi + +print(parseNum(bigBoy, 13)) diff --git a/smallmult.py b/smallmult.py index d672ef0..a8a6344 100644 --- a/smallmult.py +++ b/smallmult.py @@ -1,26 +1,29 @@ -#Problem 5 smallest multiple -#2520 is the smallest number that can be divided by -#each of the numbers from 1 to 10 without any remainder. -#What is the smallest positive number that is evenly divisible -#by all of the numbers from 1 to 20? +# Problem 5 smallest multiple +# 2520 is the smallest number that can be divided by +# each of the numbers from 1 to 10 without any remainder. +# What is the smallest positive number that is evenly divisible +# by all of the numbers from 1 to 20? + +import PIL +import math -import PIL, math def isDivisible(num, divisors): - divisible = True - for j in divisors: - if(num%j !=0): - divisible = False - break - return divisible + divisible = True + for j in divisors: + if(num % j != 0): + divisible = False + break + return divisible + -divs = range(1,21) +divs = range(1, 21) found = False start = 2520 while (not found): - start+=20 - found = isDivisible(start,divs) - + start += 20 + found = isDivisible(start, divs) + print(start) print(str(isDivisible(start, divs))) diff --git a/smallmult.py.orig b/smallmult.py.orig new file mode 100644 index 0000000..d672ef0 --- /dev/null +++ b/smallmult.py.orig @@ -0,0 +1,26 @@ +#Problem 5 smallest multiple +#2520 is the smallest number that can be divided by +#each of the numbers from 1 to 10 without any remainder. +#What is the smallest positive number that is evenly divisible +#by all of the numbers from 1 to 20? + +import PIL, math + +def isDivisible(num, divisors): + divisible = True + for j in divisors: + if(num%j !=0): + divisible = False + break + return divisible + +divs = range(1,21) + +found = False +start = 2520 +while (not found): + start+=20 + found = isDivisible(start,divs) + +print(start) +print(str(isDivisible(start, divs))) @@ -1,10 +1,11 @@ -import PIL, math -#Problem 16 Power digit sum +import PIL +import math +# Problem 16 Power digit sum n = 2**1000 strn = str(n) s = 0 for i in strn: - s += int(i) - + s += int(i) + print(s) diff --git a/sumexp.py.orig b/sumexp.py.orig new file mode 100644 index 0000000..90dd2da --- /dev/null +++ b/sumexp.py.orig @@ -0,0 +1,10 @@ +import PIL, math +#Problem 16 Power digit sum +n = 2**1000 +strn = str(n) +s = 0 + +for i in strn: + s += int(i) + +print(s) diff --git a/sumofprimes.py b/sumofprimes.py index 3799e94..e85e394 100644 --- a/sumofprimes.py +++ b/sumofprimes.py @@ -1,8 +1,9 @@ -import PIL, math +import PIL +import math -#Problem 10 sum of primes +# Problem 10 sum of primes ###INEFFICIENT### -#def isPrime(number): +# def isPrime(number): # if (number <=1): # return False # for i in range(2,number): @@ -10,35 +11,35 @@ import PIL, math # 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 = [] +# for i in range(number, 2, -1): +# if(number%i==0): +# if (isPrime(i)): +# return i +# return primes def prime_factors(number): - primes = [] - if (number <=1): - return [1] - a = [1] * number - a[0]=0 - a[1]=0#not prime - for i in range(2,int(math.ceil(math.sqrt(number)))): - if(a[i]==1): - j = i**2 #cross out all the multiples - while(j < number): - a[j] = False - j+=i - - for k in range(2, number): - #is prime - if(a[k]==1): - primes.append(k) - return primes + primes = [] + if (number <= 1): + return [1] + a = [1] * number + a[0] = 0 + a[1] = 0 # not prime + for i in range(2, int(math.ceil(math.sqrt(number)))): + if(a[i] == 1): + j = i**2 # cross out all the multiples + while(j < number): + a[j] = False + j += i + + for k in range(2, number): + # is prime + if(a[k] == 1): + primes.append(k) + return primes + out = prime_factors(2000000) print(str(sum(out))) diff --git a/sumofprimes.py.orig b/sumofprimes.py.orig new file mode 100644 index 0000000..3799e94 --- /dev/null +++ b/sumofprimes.py.orig @@ -0,0 +1,44 @@ +import PIL, 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 = [] + if (number <=1): + return [1] + a = [1] * number + a[0]=0 + a[1]=0#not prime + for i in range(2,int(math.ceil(math.sqrt(number)))): + if(a[i]==1): + j = i**2 #cross out all the multiples + while(j < number): + a[j] = False + j+=i + + for k in range(2, number): + #is prime + if(a[k]==1): + primes.append(k) + return primes + +out = prime_factors(2000000) +print(str(sum(out))) @@ -1,16 +1,17 @@ -#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. +# 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, math +import PIL +import math -nums = range(1,11) -numsq = range(1,11) +nums = range(1, 11) +numsq = range(1, 11) for i in nums: i = i*i diff --git a/sumsq.py.orig b/sumsq.py.orig new file mode 100644 index 0000000..e5e8988 --- /dev/null +++ b/sumsq.py.orig @@ -0,0 +1,27 @@ +#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, 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))) |