From 73239e1c23c763adf560eea9e154e20f14f8ae0a Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Fri, 5 Feb 2016 21:54:01 -0800 Subject: [PATCH] Couple more problems --- .idea/vcs.xml | 6 +++++ problem_10.py | 29 ++++++++++++++++++++++ problem_11.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ problem_5.py | 25 +++++++++++++++++++ problem_6.py | 17 +++++++++++++ problem_7.py | 23 +++++++++++++++++ problem_8.py | 42 +++++++++++++++++++++++++++++++ problem_9.py | 19 ++++++++++++++ 8 files changed, 229 insertions(+) create mode 100644 .idea/vcs.xml create mode 100644 problem_10.py create mode 100644 problem_11.py create mode 100644 problem_5.py create mode 100644 problem_6.py create mode 100644 problem_7.py create mode 100644 problem_8.py create mode 100644 problem_9.py diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/problem_10.py b/problem_10.py new file mode 100644 index 0000000..be7c9de --- /dev/null +++ b/problem_10.py @@ -0,0 +1,29 @@ +import math + +#find the sum of all primes under 2 million +#once again, it works, but it's sloooowwwww + +def isPrime(number): + if number == 1: + return False + prime = True + i = 2 + while i < math.sqrt(number) + 1: + if number % i == 0: + prime = False # found a factor, not prime + break + else: + i += 1 + + return prime + +number = 3 +sum = 2 + +while (number < 2000000): + if isPrime(number): + sum += number + number += 2 + +print(number) +print(sum) \ No newline at end of file diff --git a/problem_11.py b/problem_11.py new file mode 100644 index 0000000..d6a525b --- /dev/null +++ b/problem_11.py @@ -0,0 +1,68 @@ +#in a grid of numbers find the 4 numbers, in any orientation, that has the highest product + +grid = [ 8, 2, 22, 97, 38, 15, 00, 40, 00, 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, 00, + 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, 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, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92, + 16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57, + 86, 56, 00, 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, 4, 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] + +def getItem(x, y): + return grid[x + int(y * 20)] + +highest = 0 + +def testHighestVertical(): + for x in range(0, 20): + for y in range(0, 17): + product = getItem(x, y) * getItem(x, y + 1) * getItem(x, y + 2) * getItem(x, y + 3) + global highest + if highest < product: + highest = product + +def testHighestHorizontal(): + for x in range(0, 17): + for y in range(0, 20): + product = getItem(x, y) * getItem(x + 1, y) * getItem(x + 2, y) * getItem(x + 3, y) + global highest + if highest < product: + highest = product + +def testHighestDiagonalVariantOne(): + for x in range(0, 17): + for y in range(0, 17): + product = getItem(x, y) * getItem(x + 1, y + 1) * getItem(x + 2, y + 2) * getItem(x + 3, y + 3) + global highest + if highest < product: + highest = product + +def testHighestDiagonalViariantTwo(): + for x in range(0, 17): + for y in range(3, 20): + product = getItem(x, y) * getItem(x + 1, y - 1) * getItem(x + 2, y - 2) * getItem(x + 3, y - 3) + global highest + if highest < product: + highest = product + + + +testHighestDiagonalVariantOne() +testHighestDiagonalViariantTwo() +testHighestHorizontal() +testHighestVertical() + +print(highest) \ No newline at end of file diff --git a/problem_5.py b/problem_5.py new file mode 100644 index 0000000..db65527 --- /dev/null +++ b/problem_5.py @@ -0,0 +1,25 @@ +# Smallest positive number that is evenly divisible by all of the numbers from 1 to 20 +# Oh boy is this slow. But it works! + +counter = 20 +running = True + +def check_divisor(number): + q = True + for i in range(1, 21): + if number % i != 0: + q = False + + return q + +while running: + + if not check_divisor(counter): + counter += 20 + else: + running = False + +print(counter) + + + diff --git a/problem_6.py b/problem_6.py new file mode 100644 index 0000000..a0f2db8 --- /dev/null +++ b/problem_6.py @@ -0,0 +1,17 @@ +#Difference between the sum of squares and the square of the sum of the first 100 numbers + +#find the sum of the squares +sum_of_squares = 0; +for i in range (1, 101): + sum_of_squares += i * i + +#find the square of the sum +square_of_sums = 0 +for i in range(1, 101): + square_of_sums += i; + +square_of_sums *= square_of_sums + +#find the difference +square_of_sums -= sum_of_squares +print(square_of_sums) \ No newline at end of file diff --git a/problem_7.py b/problem_7.py new file mode 100644 index 0000000..45d3759 --- /dev/null +++ b/problem_7.py @@ -0,0 +1,23 @@ +import math +# find the 10001st prime number + +def isPrime(number): + prime = True + i = 2 + while i < math.sqrt(number) + 1: + if number % i == 0: + prime = False # found a factor, not prime + break + else: + i += 1 + + return prime + +i = 1; +primeCount = 1 +while (primeCount != 10001): + if isPrime(i): + primeCount += 1 + i += 1 + +print(i) \ No newline at end of file diff --git a/problem_8.py b/problem_8.py new file mode 100644 index 0000000..5e67fc2 --- /dev/null +++ b/problem_8.py @@ -0,0 +1,42 @@ +import collections + +# find the 13 consecutive digits with the largest product + +number = "73167176531330624919225119674426574742355349194934"+\ + "96983520312774506326239578318016984801869478851843"+\ + "85861560789112949495459501737958331952853208805511"+\ + "12540698747158523863050715693290963295227443043557"+\ + "66896648950445244523161731856403098711121722383113"+\ + "62229893423380308135336276614282806444486645238749"+\ + "30358907296290491560440772390713810515859307960866"+\ + "70172427121883998797908792274921901699720888093776"+\ + "65727333001053367881220235421809751254540594752243"+\ + "52584907711670556013604839586446706324415722155397"+\ + "53697817977846174064955149290862569321978468622482"+\ + "83972241375657056057490261407972968652414535100474"+\ + "82166370484403199890008895243450658541227588666881"+\ + "16427171479924442928230863465674813919123162824586"+\ + "17866458359124566529476545682848912883142607690042"+\ + "24219022671055626321111109370544217506941658960408"+\ + "07198403850962455444362981230987879927244284909188"+\ + "84580156166097919133875499200524063689912560717606"+\ + "05886116467109405077541002256983155200055935729725"+\ + "71636269561882670428252483600823257530420752963450" + +queue = collections.deque() + +for i in range(0, 13): + queue.append(number[i]) + +max_product = 0; +for i in range(13, 1000): + product = 1 + for q in queue: + product *= int(q) + if max_product < product: + max_product = product + + queue.popleft() + queue.append(number[i]) + +print(max_product) \ No newline at end of file diff --git a/problem_9.py b/problem_9.py new file mode 100644 index 0000000..5268458 --- /dev/null +++ b/problem_9.py @@ -0,0 +1,19 @@ +#find the pythagorean triplet that a^2 + b^2 + c^2 = 1000 + +#just gonna do a nested for loop for this and brute force it +#it works, it's just slow as hell + +def is_triplet(a, b, c): + if (a < b and b < c): + return True + else: + return False + +for a in range(0, 999): + for b in range(0, 999): + for c in range(0, 999): + if is_triplet(a, b, c): + if a*a + b*b == c*c: + if (a+b+c == 1000): + print(a*b*c) + break \ No newline at end of file