From 93ea7fe5957b62f18e8fbd17a21696bd7de6332d Mon Sep 17 00:00:00 2001 From: mjfernez Date: Sun, 9 Feb 2020 15:16:26 -0500 Subject: Organized everything, update README --- 01-Multiples-of-3-and-5/euler1 | Bin 0 -> 8608 bytes 01-Multiples-of-3-and-5/euler1.c | 13 +++ 02-Even-Fibonacci-numbers/fib | Bin 0 -> 16776 bytes 02-Even-Fibonacci-numbers/fib.c | 33 ++++++++ 02-Even-Fibonacci-numbers/fib.py | 34 ++++++++ 03-Largest-Prime-Factor/largestprime | Bin 0 -> 16648 bytes 03-Largest-Prime-Factor/largestprime.c | 21 +++++ 03-Largest-Prime-Factor/largestprime.py | 16 ++++ 04-Largest-Palindrome-Product/palindrome | Bin 0 -> 16824 bytes 04-Largest-Palindrome-Product/palindrome.c | 59 ++++++++++++++ 04-Largest-Palindrome-Product/palindrome.py | 36 +++++++++ 05-Smallest-Multiple/smallmult | Bin 0 -> 16648 bytes 05-Smallest-Multiple/smallmult.c | 23 ++++++ 05-Smallest-Multiple/smallmult.py | 29 +++++++ 06-Sum-Square-Difference/sumsq.py | 28 +++++++ 07-10001st-Prime/10001prime | Bin 0 -> 16880 bytes 07-10001st-Prime/10001prime.c | 42 ++++++++++ 07-10001st-Prime/10001prime.py | 34 ++++++++ 08-Largest-Product-In-a-Series/productofdigits | Bin 0 -> 16680 bytes 08-Largest-Product-In-a-Series/productofdigits.c | 50 ++++++++++++ 08-Largest-Product-In-a-Series/productofdigits.py | 20 +++++ 09-Special-Pythagorean-Triplet/pyth.py | 29 +++++++ 10-Summation-of-Primes/sumofprimes | Bin 0 -> 16904 bytes 10-Summation-of-Primes/sumofprimes.c | 50 ++++++++++++ 10-Summation-of-Primes/sumofprimes.py | 28 +++++++ 10001prime | Bin 16880 -> 0 bytes 10001prime.c | 42 ---------- 10001prime.py | 34 -------- 11-Largest-Product-In-Grid/prodgrid | Bin 0 -> 10312 bytes 11-Largest-Product-In-Grid/prodgrid.c | 87 +++++++++++++++++++++ 12-Highly-Divisible-Triangular-Number/bigfactors2 | Bin 0 -> 16752 bytes .../bigfactors2.c | 31 ++++++++ .../bigfactors2.py | 34 ++++++++ 14-Longest-Collatz-Sequence/collatz | Bin 0 -> 16696 bytes 14-Longest-Collatz-Sequence/collatz.c | 31 ++++++++ 14-Longest-Collatz-Sequence/collatz.py | 28 +++++++ 16-Power-Digit-Sum/sumexp.py | 9 +++ 17-Number-Letter-Counts/wordnums.py | 46 +++++++++++ README.md | 19 ++++- bigfactors2 | Bin 16752 -> 0 bytes bigfactors2.c | 31 -------- bigfactors2.py | 34 -------- collatz | Bin 16696 -> 0 bytes collatz.c | 31 -------- collatz.py | 28 ------- euler1 | Bin 8608 -> 0 bytes euler1.c | 13 --- fib | Bin 16776 -> 0 bytes fib.c | 33 -------- fib.py | 34 -------- largestprime | Bin 16648 -> 0 bytes largestprime.c | 21 ----- largestprime.py | 16 ---- palindrome | Bin 16824 -> 0 bytes palindrome.c | 59 -------------- palindrome.py | 36 --------- prodgrid | Bin 10312 -> 0 bytes prodgrid.c | 87 --------------------- productofdigits | Bin 16680 -> 0 bytes productofdigits.c | 50 ------------ productofdigits.py | 20 ----- reworking/sumexp.c | 15 ++++ smallmult | Bin 16648 -> 0 bytes smallmult.c | 23 ------ smallmult.py | 29 ------- sumexp.c | 21 ----- sumexp.py | 9 --- sumofprimes | Bin 16904 -> 0 bytes sumofprimes.c | 50 ------------ sumofprimes.py | 28 ------- 70 files changed, 842 insertions(+), 732 deletions(-) create mode 100755 01-Multiples-of-3-and-5/euler1 create mode 100644 01-Multiples-of-3-and-5/euler1.c create mode 100755 02-Even-Fibonacci-numbers/fib create mode 100644 02-Even-Fibonacci-numbers/fib.c create mode 100644 02-Even-Fibonacci-numbers/fib.py create mode 100755 03-Largest-Prime-Factor/largestprime create mode 100644 03-Largest-Prime-Factor/largestprime.c create mode 100644 03-Largest-Prime-Factor/largestprime.py create mode 100755 04-Largest-Palindrome-Product/palindrome create mode 100644 04-Largest-Palindrome-Product/palindrome.c create mode 100644 04-Largest-Palindrome-Product/palindrome.py create mode 100755 05-Smallest-Multiple/smallmult create mode 100644 05-Smallest-Multiple/smallmult.c create mode 100644 05-Smallest-Multiple/smallmult.py create mode 100644 06-Sum-Square-Difference/sumsq.py create mode 100755 07-10001st-Prime/10001prime create mode 100644 07-10001st-Prime/10001prime.c create mode 100644 07-10001st-Prime/10001prime.py create mode 100755 08-Largest-Product-In-a-Series/productofdigits create mode 100644 08-Largest-Product-In-a-Series/productofdigits.c create mode 100644 08-Largest-Product-In-a-Series/productofdigits.py create mode 100644 09-Special-Pythagorean-Triplet/pyth.py create mode 100755 10-Summation-of-Primes/sumofprimes create mode 100644 10-Summation-of-Primes/sumofprimes.c create mode 100644 10-Summation-of-Primes/sumofprimes.py delete mode 100755 10001prime delete mode 100644 10001prime.c delete mode 100644 10001prime.py create mode 100755 11-Largest-Product-In-Grid/prodgrid create mode 100644 11-Largest-Product-In-Grid/prodgrid.c create mode 100755 12-Highly-Divisible-Triangular-Number/bigfactors2 create mode 100644 12-Highly-Divisible-Triangular-Number/bigfactors2.c create mode 100644 12-Highly-Divisible-Triangular-Number/bigfactors2.py create mode 100755 14-Longest-Collatz-Sequence/collatz create mode 100644 14-Longest-Collatz-Sequence/collatz.c create mode 100644 14-Longest-Collatz-Sequence/collatz.py create mode 100644 16-Power-Digit-Sum/sumexp.py create mode 100644 17-Number-Letter-Counts/wordnums.py delete mode 100755 bigfactors2 delete mode 100644 bigfactors2.c delete mode 100644 bigfactors2.py delete mode 100755 collatz delete mode 100644 collatz.c delete mode 100644 collatz.py delete mode 100755 euler1 delete mode 100644 euler1.c delete mode 100755 fib delete mode 100644 fib.c delete mode 100644 fib.py delete mode 100755 largestprime delete mode 100644 largestprime.c delete mode 100644 largestprime.py delete mode 100755 palindrome delete mode 100644 palindrome.c delete mode 100644 palindrome.py delete mode 100755 prodgrid delete mode 100644 prodgrid.c delete mode 100755 productofdigits delete mode 100644 productofdigits.c delete mode 100644 productofdigits.py create mode 100644 reworking/sumexp.c delete mode 100755 smallmult delete mode 100644 smallmult.c delete mode 100644 smallmult.py delete mode 100644 sumexp.c delete mode 100644 sumexp.py delete mode 100755 sumofprimes delete mode 100644 sumofprimes.c delete mode 100644 sumofprimes.py diff --git a/01-Multiples-of-3-and-5/euler1 b/01-Multiples-of-3-and-5/euler1 new file mode 100755 index 0000000..585d380 Binary files /dev/null and b/01-Multiples-of-3-and-5/euler1 differ diff --git a/01-Multiples-of-3-and-5/euler1.c b/01-Multiples-of-3-and-5/euler1.c new file mode 100644 index 0000000..d89956e --- /dev/null +++ b/01-Multiples-of-3-and-5/euler1.c @@ -0,0 +1,13 @@ +#include +#include +//Problem 1: multiples of 3 and 5 +int main(){ + int max = 1000; + int i; double sum; + + for (i=0;i +#include +#include + +const long int MAX = 4 * pow(10, 6); +int *fib; + +int main(){ + // Intialize sequence with first two terms + fib = (int *) malloc (2 * sizeof(int)); + fib[0] = 1; + fib[1] = 1; + + // loop counter + int k = 1; + // sum of even terms + int sum = 0; + //next term in sequence + int next; + while(1){ + next = fib[k] + fib[k - 1]; + printf("%d\n", next); + if(next > MAX) + break; + if(next % 2 == 0) + sum += next; + k++; + fib = (int *) realloc (fib, (k + 1) * sizeof(int)); + fib[k] = next; + } + printf("The sum of the even terms is %d\n", sum); + return 0; +} diff --git a/02-Even-Fibonacci-numbers/fib.py b/02-Even-Fibonacci-numbers/fib.py new file mode 100644 index 0000000..c2f2691 --- /dev/null +++ b/02-Even-Fibonacci-numbers/fib.py @@ -0,0 +1,34 @@ +import PIL +import 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/03-Largest-Prime-Factor/largestprime b/03-Largest-Prime-Factor/largestprime new file mode 100755 index 0000000..c2afe6f Binary files /dev/null and b/03-Largest-Prime-Factor/largestprime differ diff --git a/03-Largest-Prime-Factor/largestprime.c b/03-Largest-Prime-Factor/largestprime.c new file mode 100644 index 0000000..bf98414 --- /dev/null +++ b/03-Largest-Prime-Factor/largestprime.c @@ -0,0 +1,21 @@ +#include +#include + +long lpf(long num){ + long factor = 2; + while(num > factor){ + if(num % factor == 0){ + num = num / factor; + factor = 2; + } + else{ + factor ++; + } + } + return factor; +} + +int main(){ + printf("%d\n", lpf(600851475143)); + return 0; +} diff --git a/03-Largest-Prime-Factor/largestprime.py b/03-Largest-Prime-Factor/largestprime.py new file mode 100644 index 0000000..387de01 --- /dev/null +++ b/03-Largest-Prime-Factor/largestprime.py @@ -0,0 +1,16 @@ +import PIL +import math +# Problem 3 Largest Prime Factor +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/04-Largest-Palindrome-Product/palindrome b/04-Largest-Palindrome-Product/palindrome new file mode 100755 index 0000000..f4d607f Binary files /dev/null and b/04-Largest-Palindrome-Product/palindrome differ diff --git a/04-Largest-Palindrome-Product/palindrome.c b/04-Largest-Palindrome-Product/palindrome.c new file mode 100644 index 0000000..aad741a --- /dev/null +++ b/04-Largest-Palindrome-Product/palindrome.c @@ -0,0 +1,59 @@ +#include +#include +#include + +int isPalindrome(long num){ + long len = 0; + long n = num; + while(n != 0){ + n = n / 10; + len++; + } + char *numchar = (char *) malloc (len * sizeof(char)); + sprintf(numchar, "%ld", num); + //printf("%s\n", numchar); + // Backwards counter + long j = len - 1; + // Forwards counter + long i = 0; + + while(i < len){ + if(numchar[i] != numchar[j]) + return 0; + i++; + j--; + } + return 1; +} + +long *findProduct(long num){ + for(long i = 999; i > 100; i--) + for(long j = 999; j > 100; j--) + if(i * j == num){ + long *facts = (long *) malloc (2 * sizeof(long)); + facts[0] = i; + facts[1] = j; + return facts; + } +} + +long findMaxPalindrome(){ + long large = 0; + for(long i = 999; i > 100; i--){ + for(long j = 999; j > 100; j--){ + long test = i * j; + if(isPalindrome(test) && test > large){ + large = test; + } + } + } + return large; +} + +int main(){ + long answer = findMaxPalindrome(); + printf("%d\n", answer); + long *prod = findProduct(answer); + printf("The factors are %d and %d\n", prod[0], prod[1]); +} + diff --git a/04-Largest-Palindrome-Product/palindrome.py b/04-Largest-Palindrome-Product/palindrome.py new file mode 100644 index 0000000..ed2a779 --- /dev/null +++ b/04-Largest-Palindrome-Product/palindrome.py @@ -0,0 +1,36 @@ +# Problem 4 - Palindrome Products + + +def isPalindrome(number): + numchar = str(number) + middle = int(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: ") + diff --git a/05-Smallest-Multiple/smallmult b/05-Smallest-Multiple/smallmult new file mode 100755 index 0000000..5f4e692 Binary files /dev/null and b/05-Smallest-Multiple/smallmult differ diff --git a/05-Smallest-Multiple/smallmult.c b/05-Smallest-Multiple/smallmult.c new file mode 100644 index 0000000..302f19b --- /dev/null +++ b/05-Smallest-Multiple/smallmult.c @@ -0,0 +1,23 @@ +#include +#include + +// 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); +} diff --git a/05-Smallest-Multiple/smallmult.py b/05-Smallest-Multiple/smallmult.py new file mode 100644 index 0000000..a8a6344 --- /dev/null +++ b/05-Smallest-Multiple/smallmult.py @@ -0,0 +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? + +import PIL +import 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))) diff --git a/06-Sum-Square-Difference/sumsq.py b/06-Sum-Square-Difference/sumsq.py new file mode 100644 index 0000000..fdd43e4 --- /dev/null +++ b/06-Sum-Square-Difference/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 +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))) diff --git a/07-10001st-Prime/10001prime b/07-10001st-Prime/10001prime new file mode 100755 index 0000000..a9cc351 Binary files /dev/null and b/07-10001st-Prime/10001prime differ diff --git a/07-10001st-Prime/10001prime.c b/07-10001st-Prime/10001prime.c new file mode 100644 index 0000000..2ef3bd8 --- /dev/null +++ b/07-10001st-Prime/10001prime.c @@ -0,0 +1,42 @@ +#include +#include +#include +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/07-10001st-Prime/10001prime.py b/07-10001st-Prime/10001prime.py new file mode 100644 index 0000000..bc51d49 --- /dev/null +++ b/07-10001st-Prime/10001prime.py @@ -0,0 +1,34 @@ +import PIL +import 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/08-Largest-Product-In-a-Series/productofdigits b/08-Largest-Product-In-a-Series/productofdigits new file mode 100755 index 0000000..9d3f349 Binary files /dev/null and b/08-Largest-Product-In-a-Series/productofdigits differ diff --git a/08-Largest-Product-In-a-Series/productofdigits.c b/08-Largest-Product-In-a-Series/productofdigits.c new file mode 100644 index 0000000..433c433 --- /dev/null +++ b/08-Largest-Product-In-a-Series/productofdigits.c @@ -0,0 +1,50 @@ +#include +#include + +const char bigBoy[1001] = "73167176531330624919225119674426574742355349194934" + "96983520312774506326239578318016984801869478851843" + "85861560789112949495459501737958331952853208805511" + "12540698747158523863050715693290963295227443043557" + "66896648950445244523161731856403098711121722383113" + "62229893423380308135336276614282806444486645238749" + "30358907296290491560440772390713810515859307960866" + "70172427121883998797908792274921901699720888093776" + "65727333001053367881220235421809751254540594752243" + "52584907711670556013604839586446706324415722155397" + "53697817977846174064955149290862569321978468622482" + "83972241375657056057490261407972968652414535100474" + "82166370484403199890008895243450658541227588666881" + "16427171479924442928230863465674813919123162824586" + "17866458359124566529476545682848912883142607690042" + "24219022671055626321111109370544217506941658960408" + "07198403850962455444362981230987879927244284909188" + "84580156166097919133875499200524063689912560717606" + "05886116467109405077541002256983155200055935729725" + "71636269561882670428252483600823257530420752963450"; +// parseNum: Splits in segements of r, to find out the largest product of digits +// for that segment size +// max is the maximum product found within the number +// x: the number (as a char string) to be parsed +// r: the range to parse over +long long parseNum(const char *x, int r){ + int len = 1000; + long max; + for(int i = 0; i < len - r; i++){ + // Digits in ascii start at 48 + long long prod = (x[i] - 48); + for(int k = i + 1; k < i + r; k++) + prod *= (x[k] - 48); + if(prod > max) + max = prod; + } + return max; +} + + + +int main(){ + long long p = parseNum(bigBoy, 13); + printf("%ld\n", p); + //printf("%ld\n", parseNum(bigBoy, 13)); + return 0; +} diff --git a/08-Largest-Product-In-a-Series/productofdigits.py b/08-Largest-Product-In-a-Series/productofdigits.py new file mode 100644 index 0000000..0d1e132 --- /dev/null +++ b/08-Largest-Product-In-a-Series/productofdigits.py @@ -0,0 +1,20 @@ +import PIL +import math + +# 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/09-Special-Pythagorean-Triplet/pyth.py b/09-Special-Pythagorean-Triplet/pyth.py new file mode 100644 index 0000000..5b1e6f9 --- /dev/null +++ b/09-Special-Pythagorean-Triplet/pyth.py @@ -0,0 +1,29 @@ +#A Pythagorean triplet is a set of three natural numbers, +#a < b < c, for which, +#a2 + b2 = c2 +#For example, 32 + 42 = 9 + 16 = 25 = 52. +#There exists exactly one Pythagorean triplet for which a + b + c = 1000. +#Find the product abc. + +import PIL, math + +def isTriple(abc): + if len(abc) > 3: + return False + legs = abc[0]**2 +abc[1]**2 + hyp = abc[2]**2 + + if (hyp == legs): + return True + else: + return False + + +for i in range(1,1000): + for j in range(1,1000): + for k in range(1,1000): + if (i+j+k)==1000 and isTriple([i,j,k]): + print([i, j, k]) + print(i*j*k) + exit() + diff --git a/10-Summation-of-Primes/sumofprimes b/10-Summation-of-Primes/sumofprimes new file mode 100755 index 0000000..a175f6a Binary files /dev/null and b/10-Summation-of-Primes/sumofprimes differ diff --git a/10-Summation-of-Primes/sumofprimes.c b/10-Summation-of-Primes/sumofprimes.c new file mode 100644 index 0000000..90e0bbb --- /dev/null +++ b/10-Summation-of-Primes/sumofprimes.c @@ -0,0 +1,50 @@ +#include +#include +#include + +// 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/10-Summation-of-Primes/sumofprimes.py b/10-Summation-of-Primes/sumofprimes.py new file mode 100644 index 0000000..d0db224 --- /dev/null +++ b/10-Summation-of-Primes/sumofprimes.py @@ -0,0 +1,28 @@ +import PIL +import math + +# Problem 10 sum of 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))) diff --git a/10001prime b/10001prime deleted file mode 100755 index a9cc351..0000000 Binary files a/10001prime and /dev/null differ diff --git a/10001prime.c b/10001prime.c deleted file mode 100644 index 2ef3bd8..0000000 --- a/10001prime.c +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -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 deleted file mode 100644 index bc51d49..0000000 --- a/10001prime.py +++ /dev/null @@ -1,34 +0,0 @@ -import PIL -import 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/11-Largest-Product-In-Grid/prodgrid b/11-Largest-Product-In-Grid/prodgrid new file mode 100755 index 0000000..5054666 Binary files /dev/null and b/11-Largest-Product-In-Grid/prodgrid differ diff --git a/11-Largest-Product-In-Grid/prodgrid.c b/11-Largest-Product-In-Grid/prodgrid.c new file mode 100644 index 0000000..a4f27c3 --- /dev/null +++ b/11-Largest-Product-In-Grid/prodgrid.c @@ -0,0 +1,87 @@ +#include +#include + +//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 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/12-Highly-Divisible-Triangular-Number/bigfactors2 b/12-Highly-Divisible-Triangular-Number/bigfactors2 new file mode 100755 index 0000000..3e409b1 Binary files /dev/null and b/12-Highly-Divisible-Triangular-Number/bigfactors2 differ diff --git a/12-Highly-Divisible-Triangular-Number/bigfactors2.c b/12-Highly-Divisible-Triangular-Number/bigfactors2.c new file mode 100644 index 0000000..97ba17d --- /dev/null +++ b/12-Highly-Divisible-Triangular-Number/bigfactors2.c @@ -0,0 +1,31 @@ +#include +#include +#include + +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/12-Highly-Divisible-Triangular-Number/bigfactors2.py b/12-Highly-Divisible-Triangular-Number/bigfactors2.py new file mode 100644 index 0000000..9e51502 --- /dev/null +++ b/12-Highly-Divisible-Triangular-Number/bigfactors2.py @@ -0,0 +1,34 @@ +import PIL +import 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)) diff --git a/14-Longest-Collatz-Sequence/collatz b/14-Longest-Collatz-Sequence/collatz new file mode 100755 index 0000000..37da709 Binary files /dev/null and b/14-Longest-Collatz-Sequence/collatz differ diff --git a/14-Longest-Collatz-Sequence/collatz.c b/14-Longest-Collatz-Sequence/collatz.c new file mode 100644 index 0000000..ec77af6 --- /dev/null +++ b/14-Longest-Collatz-Sequence/collatz.c @@ -0,0 +1,31 @@ +#include +#include +#include +// In C, it's immediately obvious my python solution is inefficient + +// For keeping track of the length of sequences +// c is a temporary counter, s changes when c reaches a new maximum +long int c = 0; +long int s = 0; +long int collatz(long int 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; + collatz(i); + if(c > s){ + s = c; + } + } + printf("The longest sequence has %d numbers \n", s); + return 0; +} diff --git a/14-Longest-Collatz-Sequence/collatz.py b/14-Longest-Collatz-Sequence/collatz.py new file mode 100644 index 0000000..74d5e1e --- /dev/null +++ b/14-Longest-Collatz-Sequence/collatz.py @@ -0,0 +1,28 @@ +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 +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/16-Power-Digit-Sum/sumexp.py b/16-Power-Digit-Sum/sumexp.py new file mode 100644 index 0000000..a4d8eaf --- /dev/null +++ b/16-Power-Digit-Sum/sumexp.py @@ -0,0 +1,9 @@ +# 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/17-Number-Letter-Counts/wordnums.py b/17-Number-Letter-Counts/wordnums.py new file mode 100644 index 0000000..f1276b9 --- /dev/null +++ b/17-Number-Letter-Counts/wordnums.py @@ -0,0 +1,46 @@ +import PIL, math + +ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] +weirdos = ["thirteen", "twelve", "eleven", "ten"] +tens = ["", "teen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"] +nd = "AND" + +#num must be less than 999# + +def numToWord(num): + digits = str(num) + if (len(digits) == 1):#one digit + return ones[num] + if (len(digits) == 2):#two digits + if(num < 14):#special case for ten eleven twelve + return weirdos[13 - num] + if (num == 15): + return "fifteen" + if (num == 18): + return "eighteen" + t = digits[0] + o = digits[1] + if (int(t)==1): + return numToWord(int(o)) + tens[int(t)] + else: + return tens[int(t)] + numToWord(int(o)) + #number is three digits# + h = digits[0] + t = digits[1] + o = digits[2] + if(num % 100 == 0): + return numToWord(int(h)) + "hundred" + else: + return numToWord(int(h)) + "hundred"+nd+numToWord(int(t+o)) + + + +n = int(input("Number: ")) +out ="" +for i in range(1, n): + out += numToWord(int(i)) + +out+="onethousand" +print(out) +print(str(len(out)) + " letters") + diff --git a/README.md b/README.md index 1496221..9e888da 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,20 @@ # Project_Euler_Solutions Solutions to some problems on https://projecteuler.net/archives -Apologies that they are unoragnized! I did these back in Summer of 2017 and I need to go back and rename these guys -Problems are specified in the comments. +I'm building this repository as I go, mostly as a convenience to myself so I +can push and pull from work and home. -There is a mix of C and Python since I was studying both languages at the time +Solutions are assorted in Python and C. I started with Python since I was more +familiar with it, but now I'm working to translate everything to C. It really +forces you to think up more efficient solutions :) + +Some python solutions I had to remove since they don't work in Python 3 anymore. +Most were easy to update, but a few I couldn't figure out. + +Improving organization of these day by so apologies if it's not up to snuff! + +PLEASE do not use these solutions to cheat through the site! These are only provided +as resource for you to learn when you get suck, you do not need to copy my solutions. +Please re-write my solutions in another language. Or if you can't, at least go and +retype each line, renaming the variables so they make sense to you (I was kind of +deliberately cryptic). diff --git a/bigfactors2 b/bigfactors2 deleted file mode 100755 index 3e409b1..0000000 Binary files a/bigfactors2 and /dev/null differ diff --git a/bigfactors2.c b/bigfactors2.c deleted file mode 100644 index 97ba17d..0000000 --- a/bigfactors2.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include - -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 deleted file mode 100644 index 9e51502..0000000 --- a/bigfactors2.py +++ /dev/null @@ -1,34 +0,0 @@ -import PIL -import 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)) diff --git a/collatz b/collatz deleted file mode 100755 index 37da709..0000000 Binary files a/collatz and /dev/null differ diff --git a/collatz.c b/collatz.c deleted file mode 100644 index ec77af6..0000000 --- a/collatz.c +++ /dev/null @@ -1,31 +0,0 @@ -#include -#include -#include -// In C, it's immediately obvious my python solution is inefficient - -// For keeping track of the length of sequences -// c is a temporary counter, s changes when c reaches a new maximum -long int c = 0; -long int s = 0; -long int collatz(long int 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; - collatz(i); - if(c > s){ - s = c; - } - } - printf("The longest sequence has %d numbers \n", s); - return 0; -} diff --git a/collatz.py b/collatz.py deleted file mode 100644 index 74d5e1e..0000000 --- a/collatz.py +++ /dev/null @@ -1,28 +0,0 @@ -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 -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 b/euler1 deleted file mode 100755 index 585d380..0000000 Binary files a/euler1 and /dev/null differ diff --git a/euler1.c b/euler1.c deleted file mode 100644 index d89956e..0000000 --- a/euler1.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include -//Problem 1: multiples of 3 and 5 -int main(){ - int max = 1000; - int i; double sum; - - for (i=0;i -#include -#include - -const long int MAX = 4 * pow(10, 6); -int *fib; - -int main(){ - // Intialize sequence with first two terms - fib = (int *) malloc (2 * sizeof(int)); - fib[0] = 1; - fib[1] = 1; - - // loop counter - int k = 1; - // sum of even terms - int sum = 0; - //next term in sequence - int next; - while(1){ - next = fib[k] + fib[k - 1]; - printf("%d\n", next); - if(next > MAX) - break; - if(next % 2 == 0) - sum += next; - k++; - fib = (int *) realloc (fib, (k + 1) * sizeof(int)); - fib[k] = next; - } - printf("The sum of the even terms is %d\n", sum); - return 0; -} diff --git a/fib.py b/fib.py deleted file mode 100644 index c2f2691..0000000 --- a/fib.py +++ /dev/null @@ -1,34 +0,0 @@ -import PIL -import 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 b/largestprime deleted file mode 100755 index c2afe6f..0000000 Binary files a/largestprime and /dev/null differ diff --git a/largestprime.c b/largestprime.c deleted file mode 100644 index bf98414..0000000 --- a/largestprime.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include - -long lpf(long num){ - long factor = 2; - while(num > factor){ - if(num % factor == 0){ - num = num / factor; - factor = 2; - } - else{ - factor ++; - } - } - return factor; -} - -int main(){ - printf("%d\n", lpf(600851475143)); - return 0; -} diff --git a/largestprime.py b/largestprime.py deleted file mode 100644 index 387de01..0000000 --- a/largestprime.py +++ /dev/null @@ -1,16 +0,0 @@ -import PIL -import math -# Problem 3 Largest Prime Factor -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 b/palindrome deleted file mode 100755 index f4d607f..0000000 Binary files a/palindrome and /dev/null differ diff --git a/palindrome.c b/palindrome.c deleted file mode 100644 index aad741a..0000000 --- a/palindrome.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -int isPalindrome(long num){ - long len = 0; - long n = num; - while(n != 0){ - n = n / 10; - len++; - } - char *numchar = (char *) malloc (len * sizeof(char)); - sprintf(numchar, "%ld", num); - //printf("%s\n", numchar); - // Backwards counter - long j = len - 1; - // Forwards counter - long i = 0; - - while(i < len){ - if(numchar[i] != numchar[j]) - return 0; - i++; - j--; - } - return 1; -} - -long *findProduct(long num){ - for(long i = 999; i > 100; i--) - for(long j = 999; j > 100; j--) - if(i * j == num){ - long *facts = (long *) malloc (2 * sizeof(long)); - facts[0] = i; - facts[1] = j; - return facts; - } -} - -long findMaxPalindrome(){ - long large = 0; - for(long i = 999; i > 100; i--){ - for(long j = 999; j > 100; j--){ - long test = i * j; - if(isPalindrome(test) && test > large){ - large = test; - } - } - } - return large; -} - -int main(){ - long answer = findMaxPalindrome(); - printf("%d\n", answer); - long *prod = findProduct(answer); - printf("The factors are %d and %d\n", prod[0], prod[1]); -} - diff --git a/palindrome.py b/palindrome.py deleted file mode 100644 index ed2a779..0000000 --- a/palindrome.py +++ /dev/null @@ -1,36 +0,0 @@ -# Problem 4 - Palindrome Products - - -def isPalindrome(number): - numchar = str(number) - middle = int(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: ") - diff --git a/prodgrid b/prodgrid deleted file mode 100755 index 5054666..0000000 Binary files a/prodgrid and /dev/null differ diff --git a/prodgrid.c b/prodgrid.c deleted file mode 100644 index a4f27c3..0000000 --- a/prodgrid.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include - -//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 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/productofdigits b/productofdigits deleted file mode 100755 index 9d3f349..0000000 Binary files a/productofdigits and /dev/null differ diff --git a/productofdigits.c b/productofdigits.c deleted file mode 100644 index 433c433..0000000 --- a/productofdigits.c +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include - -const char bigBoy[1001] = "73167176531330624919225119674426574742355349194934" - "96983520312774506326239578318016984801869478851843" - "85861560789112949495459501737958331952853208805511" - "12540698747158523863050715693290963295227443043557" - "66896648950445244523161731856403098711121722383113" - "62229893423380308135336276614282806444486645238749" - "30358907296290491560440772390713810515859307960866" - "70172427121883998797908792274921901699720888093776" - "65727333001053367881220235421809751254540594752243" - "52584907711670556013604839586446706324415722155397" - "53697817977846174064955149290862569321978468622482" - "83972241375657056057490261407972968652414535100474" - "82166370484403199890008895243450658541227588666881" - "16427171479924442928230863465674813919123162824586" - "17866458359124566529476545682848912883142607690042" - "24219022671055626321111109370544217506941658960408" - "07198403850962455444362981230987879927244284909188" - "84580156166097919133875499200524063689912560717606" - "05886116467109405077541002256983155200055935729725" - "71636269561882670428252483600823257530420752963450"; -// parseNum: Splits in segements of r, to find out the largest product of digits -// for that segment size -// max is the maximum product found within the number -// x: the number (as a char string) to be parsed -// r: the range to parse over -long long parseNum(const char *x, int r){ - int len = 1000; - long max; - for(int i = 0; i < len - r; i++){ - // Digits in ascii start at 48 - long long prod = (x[i] - 48); - for(int k = i + 1; k < i + r; k++) - prod *= (x[k] - 48); - if(prod > max) - max = prod; - } - return max; -} - - - -int main(){ - long long p = parseNum(bigBoy, 13); - printf("%ld\n", p); - //printf("%ld\n", parseNum(bigBoy, 13)); - return 0; -} diff --git a/productofdigits.py b/productofdigits.py deleted file mode 100644 index 0d1e132..0000000 --- a/productofdigits.py +++ /dev/null @@ -1,20 +0,0 @@ -import PIL -import math - -# 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/reworking/sumexp.c b/reworking/sumexp.c new file mode 100644 index 0000000..8078909 --- /dev/null +++ b/reworking/sumexp.c @@ -0,0 +1,15 @@ +#include +#include +#include + +// Compile Notes: use `gcc .c -lgmp` + +mpz_t num; + +int main(){ + + // From docs, this assigns num the value of (op1*2) ^ op2 + mpz_mul_2exp(num, ) + return 0; +} + diff --git a/smallmult b/smallmult deleted file mode 100755 index 5f4e692..0000000 Binary files a/smallmult and /dev/null differ diff --git a/smallmult.c b/smallmult.c deleted file mode 100644 index 302f19b..0000000 --- a/smallmult.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include - -// 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); -} diff --git a/smallmult.py b/smallmult.py deleted file mode 100644 index a8a6344..0000000 --- a/smallmult.py +++ /dev/null @@ -1,29 +0,0 @@ -# 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 - - -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.c b/sumexp.c deleted file mode 100644 index f050773..0000000 --- a/sumexp.c +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include - -typedef struct bigint bigint; - -struct bigint{ - char digit; - bigint *nextdigit; -}; - -// struct methods -int length(); - -// struct globals -bigint *end = NULL; -bigint *current = { .digit = '0', .nextdigit = end}; - -int length(bigint num){ - num->digits -} diff --git a/sumexp.py b/sumexp.py deleted file mode 100644 index a4d8eaf..0000000 --- a/sumexp.py +++ /dev/null @@ -1,9 +0,0 @@ -# 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 b/sumofprimes deleted file mode 100755 index a175f6a..0000000 Binary files a/sumofprimes and /dev/null differ diff --git a/sumofprimes.c b/sumofprimes.c deleted file mode 100644 index 90e0bbb..0000000 --- a/sumofprimes.c +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include - -// 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 deleted file mode 100644 index d0db224..0000000 --- a/sumofprimes.py +++ /dev/null @@ -1,28 +0,0 @@ -import PIL -import math - -# Problem 10 sum of 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))) -- cgit v1.2.3