From b3831fb36056c7c3f00334f096ace036b46ba905 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Sun, 23 Apr 2017 22:19:11 -0700 Subject: [PATCH] Few more problems --- problem_26.py | 35 ++++++++++++++++++++++++++++------- problem_27.py | 43 +++++++++++++++++++++++++++++++++++++++++++ problem_28.py | 29 +++++++++++++++++++++++++++++ problem_29.py | 12 ++++++++++++ problem_30.py | 15 +++++++++++++++ problem_31.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 172 insertions(+), 7 deletions(-) create mode 100644 problem_27.py create mode 100644 problem_28.py create mode 100644 problem_29.py create mode 100644 problem_30.py create mode 100644 problem_31.py diff --git a/problem_26.py b/problem_26.py index 0a0b675..909e0d7 100644 --- a/problem_26.py +++ b/problem_26.py @@ -10,16 +10,37 @@ # 1/8 = 0.125 # 1/9 = 0.(1) # 1/10 = 0.1 - +# # Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. # # Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. -def longest_permutation(input): +def longest_permutation(denominator): + + mod_list = [] + modulo = 1.0 + + while True: + + modulo *= 10 + if modulo in mod_list: + break + + mod_list.append(modulo) + modulo %= denominator + + return len(mod_list) + +longest_perm = 0 +longest_perm_denominator = 0 + +for i in range(1, 1000): + + perm_num = longest_permutation(i) - return input + if longest_perm < perm_num: + longest_perm = perm_num + longest_perm_denominator = i -i = str(1.0/7.0) -print(i) -if "142857" in i: - print(True) \ No newline at end of file +print(longest_perm_denominator) +print(longest_perm) diff --git a/problem_27.py b/problem_27.py new file mode 100644 index 0000000..71f3115 --- /dev/null +++ b/problem_27.py @@ -0,0 +1,43 @@ +import math + +# n^2 + a*n + b , where |a|<1000 and |b|<1000 + + +def is_prime(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 + +largest_n = 0 +largest_n_a = 0 +largest_n_b = 0 + +for a in range(-1000, 1000): + for b in range(-1000, 1000): + + n = 1 + while True: + if not is_prime(abs(n*n + a*n + b)): + break + else: + n += 1 + + if n > largest_n: + largest_n = n + largest_n_a = a + largest_n_b = b + + print("{0}, {1}".format(a, b)) + +print(largest_n) +print(largest_n_a) +print(largest_n_b) \ No newline at end of file diff --git a/problem_28.py b/problem_28.py new file mode 100644 index 0000000..0468260 --- /dev/null +++ b/problem_28.py @@ -0,0 +1,29 @@ + +# What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? + +# top right +tr_sum = 1 +for i in range(1, 501): + tr_sum += pow(i * 2 + 1, 2) + +# bottom right +br_sum = 1 +br_tmp = 1 +for i in range(1, 501): + br_tmp += 2 + 8 * (i-1) + br_sum += br_tmp + +# bottom left +bl_sum = 1 +for i in range(1, 501): + bl_sum += pow(i * 2, 2) + 1 + +# top left +tl_sum = 1 +tl_tmp = 1 +for i in range(1, 501): + tl_tmp += (i-1) * 8 + 6 + tl_sum += tl_tmp + +# I guess the problem is looking to not repeat the center case to take away 3 +print(tl_sum + bl_sum + br_sum + tr_sum - 3) \ No newline at end of file diff --git a/problem_29.py b/problem_29.py new file mode 100644 index 0000000..46849e1 --- /dev/null +++ b/problem_29.py @@ -0,0 +1,12 @@ +import itertools + +set_list = set() + +permutation_list = list(itertools.product(range(2, 101), range(2, 101))) + +for i in permutation_list: + set_list.add(pow(i[0], i[1])) + +print(len(set_list)) + + diff --git a/problem_30.py b/problem_30.py new file mode 100644 index 0000000..ddb9e89 --- /dev/null +++ b/problem_30.py @@ -0,0 +1,15 @@ + + +total_sum = 0 + +# just gonna take a wild guess and say there aren't any more higher than 1 million +for i in range(2, 1000000): + num_str = str(i) + sum = 0 + for ch in num_str: + sum += pow(int(ch), 5) + if sum == i: + print(i) + total_sum += i + +print("Total sum {0}".format(total_sum)) \ No newline at end of file diff --git a/problem_31.py b/problem_31.py new file mode 100644 index 0000000..7021e35 --- /dev/null +++ b/problem_31.py @@ -0,0 +1,45 @@ + +# solving this recursively is an interesting solution + +import itertools + +set_list = set() +permutation_list = list(itertools.combinations(list([1, 2, 5, 10, 20, 50, 100, 200]), 2)) + +success_count = 0 + + +def recursive_coin_add(value, ciel): + + if value == ciel: + global success_count + success_count += 1 + return + + if 1 + value <= ciel: + recursive_coin_add(1 + value, ciel) + + if 2 + value <= ciel: + recursive_coin_add(2 + value, ciel) + + if 5 + value <= ciel: + recursive_coin_add(5 + value, ciel) + + if 10 + value <= ciel: + recursive_coin_add(10 + value, ciel) + + if 20 + value <= ciel: + recursive_coin_add(20 + value, ciel) + + if 50 + value <= ciel: + recursive_coin_add(50 + value, ciel) + + if 100 + value <= ciel: + recursive_coin_add(100 + value, ciel) + + if 200 + value <= ciel: + recursive_coin_add(200 + value, ciel) + +recursive_coin_add(0, 200) + +print(success_count) \ No newline at end of file