diff options
author | mjfernez <mjfernez@gmail.com> | 2018-12-02 19:12:42 -0500 |
---|---|---|
committer | mjfernez <mjfernez@gmail.com> | 2018-12-02 19:12:42 -0500 |
commit | d62ba829dc3a54edc46b7deb581dd244713393f5 (patch) | |
tree | 637d985d70fff70c4dedc14a45c559747ea18b84 | |
parent | c15221f10ed2ea4a55b474a57b6ee39f6b24ac85 (diff) | |
download | Project_Euler_Solutions-d62ba829dc3a54edc46b7deb581dd244713393f5.tar.gz |
add code
-rw-r--r-- | 10001prime.py | 31 | ||||
-rw-r--r-- | bigfactors2.py | 31 | ||||
-rw-r--r-- | collatz.py | 25 | ||||
-rwxr-xr-x | euler1 | bin | 0 -> 8608 bytes | |||
-rw-r--r-- | euler1.c | 13 | ||||
-rw-r--r-- | euler1.o | bin | 0 -> 1640 bytes | |||
-rw-r--r-- | fib.py | 33 | ||||
-rw-r--r-- | largestprime.py | 55 | ||||
-rw-r--r-- | palindrome.py | 41 | ||||
-rwxr-xr-x | prodgrid | bin | 0 -> 10312 bytes | |||
-rw-r--r-- | prodgrid.c | 87 | ||||
-rw-r--r-- | prodgrid.c~ | 78 | ||||
-rw-r--r-- | prodgrid.o | bin | 0 -> 3864 bytes | |||
-rw-r--r-- | productofdigits.py | 18 | ||||
-rw-r--r-- | productofgrid.py | 21 | ||||
-rw-r--r-- | smallmult.py | 27 | ||||
-rw-r--r-- | sumexp.py | 10 | ||||
-rw-r--r-- | sumofprimes.py | 45 | ||||
-rw-r--r-- | sumsq.py | 28 |
19 files changed, 543 insertions, 0 deletions
diff --git a/10001prime.py b/10001prime.py new file mode 100644 index 0000000..ecf8139 --- /dev/null +++ b/10001prime.py @@ -0,0 +1,31 @@ +import PIL, math +import numpy as np + +#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 = np.ones(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.py b/bigfactors2.py new file mode 100644 index 0000000..5758019 --- /dev/null +++ b/bigfactors2.py @@ -0,0 +1,31 @@ +import PIL, math +import numpy as np + +#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 diff --git a/collatz.py b/collatz.py new file mode 100644 index 0000000..1052610 --- /dev/null +++ b/collatz.py @@ -0,0 +1,25 @@ +import PIL, math +import numpy as np + +#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." Binary files differdiff --git a/euler1.c b/euler1.c new file mode 100644 index 0000000..d89956e --- /dev/null +++ b/euler1.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdlib.h> +//Problem 1: multiples of 3 and 5 +int main(){ + int max = 1000; + int i; double sum; + + for (i=0;i<max; i++) + if(i%3 == 0 || i%5==0) + sum+= (double) i; + + printf("%f\n", sum); +} diff --git a/euler1.o b/euler1.o Binary files differnew file mode 100644 index 0000000..2abffe6 --- /dev/null +++ b/euler1.o @@ -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 new file mode 100644 index 0000000..fa39904 --- /dev/null +++ b/largestprime.py @@ -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 new file mode 100644 index 0000000..c581a72 --- /dev/null +++ b/palindrome.py @@ -0,0 +1,41 @@ +import PIL, math +import numpy as np + +#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 b/prodgrid Binary files differnew file mode 100755 index 0000000..5054666 --- /dev/null +++ b/prodgrid diff --git a/prodgrid.c b/prodgrid.c new file mode 100644 index 0000000..a4f27c3 --- /dev/null +++ b/prodgrid.c @@ -0,0 +1,87 @@ +#include <stdio.h> +#include <stdlib.h> + +//Problem 11 - Largest Product in a grid + +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, 1, 32, 56, 71, 37, 2, 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}, + {4, 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, 1, 89, 19, 67, 48}}; + +int parseMatrix(int mat[20][20]){ + int maxi, i, j, k, c = 0; + int prod = 1; + //parse left to right + for(i; i < 20; i++){ + for(j; j < 16; j++){ + k = j; + prod = 1; + for(k; k<j+4; k++){ + prod*=mat[i][k]; + if(prod > maxi) + maxi=prod; + } + } + } + //parse up and down + for(j=0; j < 20; j++){ + for(i=0; i < 16; i++){ + k = i; + prod = 1; + for(k; k < i+4; k++){ + prod*=mat[k][j]; + if(prod > maxi) + maxi=prod; + } + } + } + + //parse diagonal right + 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; + } + } + } + + //parse diagonal left + for(i=0; i < 16; i++){ + for(j=19; j > 3; 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; +} diff --git a/prodgrid.c~ b/prodgrid.c~ new file mode 100644 index 0000000..611e036 --- /dev/null +++ b/prodgrid.c~ @@ -0,0 +1,78 @@ +#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 differnew file mode 100644 index 0000000..bd2e679 --- /dev/null +++ b/prodgrid.o diff --git a/productofdigits.py b/productofdigits.py new file mode 100644 index 0000000..d05b757 --- /dev/null +++ b/productofdigits.py @@ -0,0 +1,18 @@ +import PIL, math +import numpy as np + +#Problem 8 - largest Product in a series +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 + +bigBoy=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 + +print parseNum(bigBoy, 13) diff --git a/productofgrid.py b/productofgrid.py new file mode 100644 index 0000000..ab54c40 --- /dev/null +++ b/productofgrid.py @@ -0,0 +1,21 @@ +import PIL, math +import numpy +from numpy import matrix + +##Problem 11 - Largest Product in a grid +def parseMatrix(x, r): + + 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 + +#bigBoy=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450 + +mat = matrix([[08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08],[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00],[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65],[52, 70, 95, 23, 04, 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, 03, 45, 02, 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, 02, 62, 12, 20, 95, 63, 94, 39, 63, 08, 40, 91, 66, 49, 94, 21],[24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],[21, 36, 23, 09, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95],[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 09, 53, 56, 92],[16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57],[86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],[19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40],[04, 52, 08, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],[88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],[04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 08, 46, 29, 32, 40, 62, 76, 36],[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16],[20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54],[01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48]]) +print mat[6,8] +#print parseMatrix(mat, 4 ) diff --git a/smallmult.py b/smallmult.py new file mode 100644 index 0000000..88c3dcc --- /dev/null +++ b/smallmult.py @@ -0,0 +1,27 @@ +#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 +import numpy as np + +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)) diff --git a/sumexp.py b/sumexp.py new file mode 100644 index 0000000..7e4a1f1 --- /dev/null +++ b/sumexp.py @@ -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 new file mode 100644 index 0000000..7c1646b --- /dev/null +++ b/sumofprimes.py @@ -0,0 +1,45 @@ +import PIL, math +import numpy as np + +#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 = 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 + if(a[k]==1): + primes.append(k) + return primes + +out = prime_factors(2000000) +print str(sum(out)) diff --git a/sumsq.py b/sumsq.py new file mode 100644 index 0000000..dd4e885 --- /dev/null +++ b/sumsq.py @@ -0,0 +1,28 @@ +#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 numpy as np + +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))
\ No newline at end of file |