def square_diff(start, end): start_time = time.time() result = int(square_of_sums(start, end) - sum_squares_of_numbers(start, end)) print_time_log(start_time, result) return result
def find_truncatable_primes(limit: int, start_from: int): """ The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. :return: """ start_time = time.time() truncatable = set() next_prime = primes_generator_iterable(start_from) while len(truncatable) < limit: prime = next(next_prime) if is_truncatable(prime): truncatable.add(prime) result = sum(truncatable) print_time_log(start_time, result) return result
def find_largest_pandigital_prime(numbers: list): """ We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? :param numbers: :param number: :return: """ start_time = time.time() permutations = list( int(''.join(t)) for t in itertools.permutations(str(t) for t in numbers)) big_pandigital_primes = set() for n in permutations: if is_pandigital(n) and is_prime(n): big_pandigital_primes.add(n) result = max(big_pandigital_primes) print_time_log(start_time, result) return result
def find_even_fibonacci(max_num): start_time = time.time() nums = generate_fibonacci(max_num) result = sum([i for i in nums if i % 2 == 0]) print_time_log(start_time, result) return result
def main(upper_limit: int): start_time = time.time() # time log # 1 - calculate all abundant numbers less than upper_limit all_abundant = set() for n in range(1, upper_limit): if is_abundant(n): all_abundant.add(n) # 2 - Find the sum of all the positive integers which # cannot be written as the sum of two abundant numbers. result = 0 for a in range(1, upper_limit): result += a for b in all_abundant: c = a - b if c < 1: break if c in all_abundant: result -= a break print_time_log(start_time, result) # time log return result
def main(index: int): """ Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ... Pentagonal Pn=n(3n−1)/2 1, 5, 12, 22, 35, ... Hexagonal Hn=n(2n−1) 1, 6, 15, 28, 45, ... It can be verified that T285 = P165 = H143 = 40755. Find the next triangle number that is also pentagonal and hexagonal. Every hexagonal number is a triangular number since: r(2r-1)=1/2(2r-1)[(2r-1)+1]. Source: http://mathworld.wolfram.com/HexagonalNumber.html Every pentagonal number is 1/3 of a triangular number. Source: http://mathworld.wolfram.com/PentagonalNumber.html :return: """ start_time = time.time() while True: pentagonal = calc_pentagonal_number(index) triangular = pentagonal * 3 if is_pentagonal(triangular): print_time_log(start_time, triangular) return triangular index += 1
def get_first_ten_digit(numbers: list, limit: int): start_time = time.time() string_number = str(sum(numbers)) result = int(string_number[:limit]) print_time_log(start_time, result) return result
def get_max_product(numbers: list, counter: int): start_time = time.time() result = 0 temp = process_diagonal_down_left(numbers) if result < temp: result = temp temp = process_diagonal_down_right(numbers) if result < temp: result = temp temp = process_diagonal_up_left(numbers) if result < temp: result = temp temp = process_diagonal_up_right(numbers) if result < temp: result = temp temp = process_columns(numbers, counter) if result < temp: result = temp temp = process_rows(numbers, counter) if result < temp: result = temp print_time_log(start_time, result) return result
def get_sum(limit: int): start_time = time.time() primes = primes_generator(limit) result = sum(primes) print_time_log(start_time, result) return result
def main(): start_time = time.time() max_primes = {'max': 0, 'a': 0, 'b': 0, 'product': 0} for a in range(-40, 41, 1): for b in range(-41, 42, 1): n = 0 primes = set() while n <= a: temp = quadratic_primes(a, b, n) if is_prime(temp): primes.add(temp) a += 1 temp_max = len(primes) if max_primes['max'] < temp_max: max_primes['max'] = temp_max max_primes['a'] = a max_primes['b'] = b max_primes['product'] = a * b print_primes(max_primes) print_time_log(start_time, max_primes['product']) return max_primes
def large_palindrome_generator(start: int, end: int): start_time = time.time() max_palindrome = 0 i = end while i >= start: b = end while b >= start: n = i * b # break the loop since all next numbers will be smaller if n < max_palindrome: break if is_palindrome(n): if n > max_palindrome: max_palindrome = n else: break b -= 1 i -= 1 print_time_log(start_time, max_palindrome) return max_palindrome
def find_the_sum(number: int, power: int): start_time = time.time() powered_number = int(math.pow(number, power)) n_list = [int(i) for i in str(powered_number) if i != '0'] result = sum(n_list) print_time_log(start_time, result) return result
def get_largest_factor(number): start_time = time.time() results = factor(number) result = results[-1] print_time_log(start_time, result) return result
def triangle_number_generator(limit: int): start_time = time.time() triangle = {'number': 1, 'dividers': 1, 'triangle': 1} while triangle['dividers'] <= limit: triangle['number'] += 1 triangle['triangle'] += triangle['number'] triangle = find_possible_dividers(triangle) print_time_log(start_time, triangle) return triangle
def test_print_time_log_less_than_minute(self, mock_stdout): start_time = time.time() time.sleep(15) end_time = time.time() - start_time print_time_log(start_time) captured = mock_stdout.getvalue().replace('\n', '') expected = "The answer {0} " \ "returned in {1} seconds".format('', int(round(end_time, 3))) self.assertEqual(expected, captured)
def main(max_number: int): start_time = time.time() results = list() for n in range(3, max_number + 1): if is_equal(n): results.append(n) result = sum(results) print_time_log(start_time, result) return result
def calc_sum_of_palindromic_numbers(limit: int): start_time = time.time() palindromes = set() for n in range(1, limit): if is_palindrome(n) and is_palindrome(convert_to_binary(n)): palindromes.add(n) result = sum(palindromes) print_time_log(start_time, result) return result
def prime_generator(limit: int): start_time = time.time() primes = list() n = 2 while len(primes) < limit: if is_prime(n): primes.append(n) n += 1 print_time_log(start_time, primes[limit - 1]) return primes[limit - 1]
def calc_sum_of_digits(n: int): ''' Calculates the sum of the digits in the number :param n: :return: ''' start_time = time.time() factorial = calc_factorial(n) sum_of_digits = eval(' + '.join([i for i in str(factorial)])) print_time_log(start_time, sum_of_digits) return sum_of_digits
def get_max_counter(starting_number: int): start_time = time.time() result = {'number': 1, 'counter': 1} while starting_number > 0: temp = get_collatz_counter(starting_number) if result['counter'] < temp: result['counter'] = temp result['number'] = starting_number starting_number -= 1 print_time_log(start_time, result['number']) return result
def calc_lattice_path(width: int, height: int): start_time = time.time() head = multiply_members( list(range(max(width, height) + 1, width + height + 1))) if width == height: tail = multiply_members(list(range(1, max(width, height) + 1))) else: tail = multiply_members(list(range(1, min(width, height) + 1))) result = head / tail print_time_log(start_time, result) return result
def main(): start_time = time.time() num_sets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] combinations = set() for combination in itertools.permutations(num_sets): if has_sub_string_divisibility(combination): combinations.add(int(''.join([str(c) for c in combination]))) result = sum(combinations) print_time_log(start_time, result) return result
def number_to_words_counter(numbers: list): start_time = time.time() nums_0_19 = [ '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen' ] nums_20_90 = [ 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety' ] nums_dict = { 100: 'hundred', 1000: 'thousand', 1000000: 'million', 1000000000: 'billion' } result = '' for n in numbers: temp = '' # try: if n < 20: temp = nums_0_19[n] elif n < 100: temp = nums_20_90[(n // 10) - 2] + nums_0_19[n % 10] elif n < 1000: temp = nums_0_19[n // 100] + nums_dict[100] if n % 100 != 0: if n % 100 < 20: temp += 'and' + nums_0_19[n % 100] elif n % 100 < 100: temp += 'and' + nums_20_90[((n % 100) // 10) - 2] if (n % 100) % 10: temp += nums_0_19[(n % 100) % 10] elif n < 10000: temp = nums_0_19[n // 1000] + nums_dict[1000] result += temp result = len(result) print_time_log(start_time, result) return result
def test_print_time_log_more_than_minute(self, mock_stdout): answer = 1000 start_time = time.time() time.sleep(65) end_time = time.time() - start_time print_time_log(start_time, answer) captured = mock_stdout.getvalue().replace('\n', '') expected = "The answer {0} " \ "returned in {1} " \ "minutes and {2} " \ "seconds".format(answer, int(end_time // 60), round(end_time % 60, 3)) self.assertEqual(expected, captured)
def main(max_limit: int): start_time = time.time() circulars = set() primes = [n for n in range(1, max_limit) if is_prime(n) and is_circular_pattern(n)] for n in primes: if n not in circulars and is_circular(n): for p in get_rotations(n): circulars.add(p) result = len(circulars) print_time_log(start_time, result) return result
def find_greatest_product(number: str, adjacent_digits: int): start_time = time.time() result = 0 start = 0 end = adjacent_digits while end < len(number): subset = [int(i) for i in number[start:end]] temp = multiply_members(subset) if result < temp: result = temp start += 1 end += 1 print_time_log(start_time, result) return result
def lexicographic_permutations(numbers: list, index: int): """ What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? :param numbers: :param index: :return: """ start_time = time.time() result = int(''.join([ str(l) for l in [list(i) for i in itertools.permutations(numbers)][index] ])) print_time_log(start_time, result) return result
def main(stop_date: Date, current_date: Date): ''' Start date: 1 Jan 1900 was a Monday. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? :return: ''' start_time = time.time() sundays = 0 while current_date.get_year() <= stop_date.get_year(): for day in range(2, 33, 1): ''' print("{0}, {1}, {2}, {3}".format(current_date.get_day(), current_date.get_month(), current_date.get_year(), current_date.get_week())) ''' try: if current_date.get_day() == 1 and \ current_date.get_week() == Calendar.week_days[6] and \ current_date.get_year() >= 1901: sundays += 1 ''' print("\nSunday fell on the first of the month:") print("{0}, {1}, {2}, {3}\n".format(current_date.get_day(), current_date.get_month(), current_date.get_year(), current_date.get_week())) ''' # if current_date.get_day() == day: # continue current_date.set_day(day) except ValueError: # print(err.args) break print_time_log(start_time, sundays) return sundays
def main(): start_time = time.time() generator = pentagonal_number_generator() pentagonals = set() pentagonals.add(next(generator)) while True: b = next(generator) for a in pentagonals: if is_pentagonal(a + b) \ and is_pentagonal(abs(a - b)): print_time_log(start_time, abs(a - b)) return abs(a - b) pentagonals.add(b)
def get_multiplies_of_3_and_5(max_num, nums): """ Returns all multiplies of 3 and 5 within specified range :param max_num: :param nums: :return: """ start_time = time.time() result = 0 i = 0 while i < max_num: if div_by_nums(i, nums): result += i i += 1 print_time_log(start_time, result) return result