function
stringlengths
18
3.86k
intent_category
stringlengths
5
24
def is_prime(number: int) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5, int(math.sqrt(number) + 1), 6): if number % i == 0 or number % (i + 2) == 0: return False return True
project_euler
def solution(nth: int = 10001) -> int: try: nth = int(nth) except (TypeError, ValueError): raise TypeError("Parameter nth must be int or castable to int.") from None if nth <= 0: raise ValueError("Parameter nth must be greater than or equal to one.") primes: list[int] = [] num = 2 while len(primes) < nth: if is_prime(num): primes.append(num) num += 1 else: num += 1 return primes[len(primes) - 1]
project_euler
def is_prime(number: int) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5, int(sqrt(number) + 1), 6): if number % i == 0 or number % (i + 2) == 0: return False return True
project_euler
def solution(nth: int = 10001) -> int: count = 0 number = 1 while count != nth and number < 3: number += 1 if is_prime(number): count += 1 while count != nth: number += 2 if is_prime(number): count += 1 return number
project_euler
def solution(pence: int = 200) -> int: coins = [1, 2, 5, 10, 20, 50, 100, 200] number_of_ways = [0] * (pence + 1) number_of_ways[0] = 1 # base case: 1 way to make 0 pence for coin in coins: for i in range(coin, pence + 1, 1): number_of_ways[i] += number_of_ways[i - coin] return number_of_ways[pence]
project_euler
def one_pence() -> int: return 1
project_euler
def two_pence(x: int) -> int: return 0 if x < 0 else two_pence(x - 2) + one_pence()
project_euler
def five_pence(x: int) -> int: return 0 if x < 0 else five_pence(x - 5) + two_pence(x)
project_euler
def ten_pence(x: int) -> int: return 0 if x < 0 else ten_pence(x - 10) + five_pence(x)
project_euler
def twenty_pence(x: int) -> int: return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x)
project_euler
def fifty_pence(x: int) -> int: return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x)
project_euler
def one_pound(x: int) -> int: return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x)
project_euler
def two_pound(x: int) -> int: return 0 if x < 0 else two_pound(x - 200) + one_pound(x)
project_euler
def solution(n: int = 200) -> int: return two_pound(n)
project_euler
def get_pascal_triangle_unique_coefficients(depth: int) -> set[int]: coefficients = {1} previous_coefficients = [1] for _ in range(2, depth + 1): coefficients_begins_one = [*previous_coefficients, 0] coefficients_ends_one = [0, *previous_coefficients] previous_coefficients = [] for x, y in zip(coefficients_begins_one, coefficients_ends_one): coefficients.add(x + y) previous_coefficients.append(x + y) return coefficients
project_euler
def get_squarefrees(unique_coefficients: set[int]) -> set[int]: non_squarefrees = set() for number in unique_coefficients: divisor = 2 copy_number = number while divisor**2 <= copy_number: multiplicity = 0 while copy_number % divisor == 0: copy_number //= divisor multiplicity += 1 if multiplicity >= 2: non_squarefrees.add(number) break divisor += 1 return unique_coefficients.difference(non_squarefrees)
project_euler
def solution(n: int = 51) -> int: unique_coefficients = get_pascal_triangle_unique_coefficients(n) squarefrees = get_squarefrees(unique_coefficients) return sum(squarefrees)
project_euler
def solution() -> int: return [ a * b * (1000 - a - b) for a in range(1, 999) for b in range(a, 999) if (a * a + b * b == (1000 - a - b) ** 2) ][0]
project_euler
def solution(n: int = 1000) -> int: product = -1 candidate = 0 for a in range(1, n // 3): # Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c b = (n * n - 2 * a * n) // (2 * n - 2 * a) c = n - a - b if c * c == (a * a + b * b): candidate = a * b * c if candidate >= product: product = candidate return product
project_euler
def solution() -> int: for a in range(300): for b in range(a + 1, 400): for c in range(b + 1, 500): if (a + b + c) == 1000 and (a**2) + (b**2) == (c**2): return a * b * c return -1
project_euler
def solution_fast() -> int: for a in range(300): for b in range(400): c = 1000 - a - b if a < b < c and (a**2) + (b**2) == (c**2): return a * b * c return -1
project_euler
def benchmark() -> None: import timeit print( timeit.timeit("solution()", setup="from __main__ import solution", number=1000) ) print( timeit.timeit( "solution_fast()", setup="from __main__ import solution_fast", number=1000 ) )
project_euler
def is_palindrome(n: int | str) -> bool: n = str(n) return n == n[::-1]
project_euler
def solution(n: int = 1000000): total = 0 for i in range(1, n): if is_palindrome(i) and is_palindrome(bin(i).split("b")[1]): total += i return total
project_euler
def solution(max_base: int = 5) -> int: freqs = defaultdict(list) num = 0 while True: digits = get_digits(num) freqs[digits].append(num) if len(freqs[digits]) == max_base: base = freqs[digits][0] ** 3 return base num += 1
project_euler
def get_digits(num: int) -> str: return "".join(sorted(str(num**3)))
project_euler
def is_right(x1: int, y1: int, x2: int, y2: int) -> bool: if x1 == y1 == 0 or x2 == y2 == 0: return False a_square = x1 * x1 + y1 * y1 b_square = x2 * x2 + y2 * y2 c_square = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) return ( a_square + b_square == c_square or a_square + c_square == b_square or b_square + c_square == a_square )
project_euler
def solution(limit: int = 50) -> int: return sum( 1 for pt1, pt2 in combinations(product(range(limit + 1), repeat=2), 2) if is_right(*pt1, *pt2) )
project_euler
def sum_digits(num: int) -> int: digit_sum = 0 while num > 0: digit_sum += num % 10 num //= 10 return digit_sum
project_euler
def solution(max_n: int = 100) -> int: pre_numerator = 1 cur_numerator = 2 for i in range(2, max_n + 1): temp = pre_numerator e_cont = 2 * i // 3 if i % 3 == 0 else 1 pre_numerator = cur_numerator cur_numerator = e_cont * pre_numerator + temp return sum_digits(cur_numerator)
project_euler
def combinations(n, r): return factorial(n) / (factorial(r) * factorial(n - r))
project_euler
def solution(): total = 0 for i in range(1, 101): for j in range(1, i + 1): if combinations(i, j) > 1e6: total += 1 return total
project_euler
def generate_random_hand(): play, oppo = randrange(len(SORTED_HANDS)), randrange(len(SORTED_HANDS)) expected = ["Loss", "Tie", "Win"][(play >= oppo) + (play > oppo)] hand, other = SORTED_HANDS[play], SORTED_HANDS[oppo] return hand, other, expected
project_euler
def generate_random_hands(number_of_hands: int = 100): return (generate_random_hand() for _ in range(number_of_hands))
project_euler
def test_hand_is_flush(hand, expected): assert PokerHand(hand)._is_flush() == expected
project_euler
def test_hand_is_straight(hand, expected): assert PokerHand(hand)._is_straight() == expected
project_euler
def test_hand_is_five_high_straight(hand, expected, card_values): player = PokerHand(hand) assert player._is_five_high_straight() == expected assert player._card_values == card_values
project_euler
def test_hand_is_same_kind(hand, expected): assert PokerHand(hand)._is_same_kind() == expected
project_euler
def test_hand_values(hand, expected): assert PokerHand(hand)._hand_type == expected
project_euler
def test_compare_simple(hand, other, expected): assert PokerHand(hand).compare_with(PokerHand(other)) == expected
project_euler
def test_compare_random(hand, other, expected): assert PokerHand(hand).compare_with(PokerHand(other)) == expected
project_euler
def test_hand_sorted(): poker_hands = [PokerHand(hand) for hand in SORTED_HANDS] list_copy = poker_hands.copy() shuffle(list_copy) user_sorted = chain(sorted(list_copy)) for index, hand in enumerate(user_sorted): assert hand == poker_hands[index]
project_euler
def test_custom_sort_five_high_straight(): # Test that five high straights are compared correctly. pokerhands = [PokerHand("2D AC 3H 4H 5S"), PokerHand("2S 3H 4H 5S 6C")] pokerhands.sort(reverse=True) assert pokerhands[0].__str__() == "2S 3H 4H 5S 6C"
project_euler
def test_multiple_calls_five_high_straight(): # Multiple calls to five_high_straight function should still return True # and shouldn't mutate the list in every call other than the first. pokerhand = PokerHand("2C 4S AS 3D 5C") expected = True expected_card_values = [5, 4, 3, 2, 14] for _ in range(10): assert pokerhand._is_five_high_straight() == expected assert pokerhand._card_values == expected_card_values
project_euler
def __init__(self, hand: str) -> None: if not isinstance(hand, str): raise TypeError(f"Hand should be of type 'str': {hand!r}") # split removes duplicate whitespaces so no need of strip if len(hand.split(" ")) != 5: raise ValueError(f"Hand should contain only 5 cards: {hand!r}") self._hand = hand self._first_pair = 0 self._second_pair = 0 self._card_values, self._card_suit = self._internal_state() self._hand_type = self._get_hand_type() self._high_card = self._card_values[0]
project_euler
def str_eval(s: str) -> int: product = 1 for digit in s: product *= int(digit) return product
project_euler
def solution(n: str = N) -> int: largest_product = -sys.maxsize - 1 substr = n[:13] cur_index = 13 while cur_index < len(n) - 13: if int(n[cur_index]) >= int(substr[0]): substr = substr[1:] + n[cur_index] cur_index += 1 else: largest_product = max(largest_product, str_eval(substr)) substr = n[cur_index : cur_index + 13] cur_index += 13 return largest_product
project_euler
def solution(n: str = N) -> int: return max( # mypy cannot properly interpret reduce int(reduce(lambda x, y: str(int(x) * int(y)), n[i : i + 13])) for i in range(len(n) - 12) )
project_euler
def solution(n: str = N) -> int: largest_product = -sys.maxsize - 1 for i in range(len(n) - 12): product = 1 for j in range(13): product *= int(n[i + j]) if product > largest_product: largest_product = product return largest_product
project_euler
def is_prime(number: int) -> bool: if 1 < number < 4: # 2 and 3 are primes return True elif number < 2 or number % 2 == 0 or number % 3 == 0: # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes return False # All primes number are in format of 6k +/- 1 for i in range(5, int(math.sqrt(number) + 1), 6): if number % i == 0 or number % (i + 2) == 0: return False return True
project_euler
def list_truncated_nums(n: int) -> list[int]: str_num = str(n) list_nums = [n] for i in range(1, len(str_num)): list_nums.append(int(str_num[i:])) list_nums.append(int(str_num[:-i])) return list_nums
project_euler
def validate(n: int) -> bool: if len(str(n)) > 3: if not is_prime(int(str(n)[-3:])) or not is_prime(int(str(n)[:3])): return False return True
project_euler
def compute_truncated_primes(count: int = 11) -> list[int]: list_truncated_primes: list[int] = [] num = 13 while len(list_truncated_primes) != count: if validate(num): list_nums = list_truncated_nums(num) if all(is_prime(i) for i in list_nums): list_truncated_primes.append(num) num += 2 return list_truncated_primes
project_euler
def solution() -> int: return sum(compute_truncated_primes(11))
project_euler
def total_frequency_distribution(sides_number: int, dice_number: int) -> list[int]: max_face_number = sides_number max_total = max_face_number * dice_number totals_frequencies = [0] * (max_total + 1) min_face_number = 1 faces_numbers = range(min_face_number, max_face_number + 1) for dice_numbers in product(faces_numbers, repeat=dice_number): total = sum(dice_numbers) totals_frequencies[total] += 1 return totals_frequencies
project_euler
def solution() -> float: peter_totals_frequencies = total_frequency_distribution( sides_number=4, dice_number=9 ) colin_totals_frequencies = total_frequency_distribution( sides_number=6, dice_number=6 ) peter_wins_count = 0 min_peter_total = 9 max_peter_total = 4 * 9 min_colin_total = 6 for peter_total in range(min_peter_total, max_peter_total + 1): peter_wins_count += peter_totals_frequencies[peter_total] * sum( colin_totals_frequencies[min_colin_total:peter_total] ) total_games_number = (4**9) * (6**6) peter_win_probability = peter_wins_count / total_games_number rounded_peter_win_probability = round(peter_win_probability, ndigits=7) return rounded_peter_win_probability
project_euler
def digits_fifth_powers_sum(number: int) -> int: return sum(DIGITS_FIFTH_POWER[digit] for digit in str(number))
project_euler
def solution() -> int: return sum( number for number in range(1000, 1000000) if number == digits_fifth_powers_sum(number) )
project_euler
def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]: triplets: typing.Counter[int] = Counter() for base in range(1, max_perimeter + 1): for perpendicular in range(base, max_perimeter + 1): hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5 if hypotenuse == int(hypotenuse): perimeter = int(base + perpendicular + hypotenuse) if perimeter > max_perimeter: continue triplets[perimeter] += 1 return triplets
project_euler
def solution(n: int = 1000) -> int: triplets = pythagorean_triple(n) return triplets.most_common(1)[0][0]
project_euler
def prime_sieve(n: int) -> list: is_prime = [True] * n is_prime[0] = False is_prime[1] = False is_prime[2] = True for i in range(3, int(n**0.5 + 1), 2): index = i * 2 while index < n: is_prime[index] = False index = index + i primes = [2] for i in range(3, n, 2): if is_prime[i]: primes.append(i) return primes
project_euler
def solution(limit: int = 999_966_663_333) -> int: primes_upper_bound = math.floor(math.sqrt(limit)) + 100 primes = prime_sieve(primes_upper_bound) matches_sum = 0 prime_index = 0 last_prime = primes[prime_index] while (last_prime**2) <= limit: next_prime = primes[prime_index + 1] lower_bound = last_prime**2 upper_bound = next_prime**2 # Get numbers divisible by lps(current) current = lower_bound + last_prime while upper_bound > current <= limit: matches_sum += current current += last_prime # Reset the upper_bound while (upper_bound - next_prime) > limit: upper_bound -= next_prime # Add the numbers divisible by ups(current) current = upper_bound - next_prime while current > lower_bound: matches_sum += current current -= next_prime # Remove the numbers divisible by both ups and lps current = 0 while upper_bound > current <= limit: if current <= lower_bound: # Increment the current number current += last_prime * next_prime continue if current > limit: break # Remove twice since it was added by both ups and lps matches_sum -= current * 2 # Increment the current number current += last_prime * next_prime # Setup for next pair last_prime = next_prime prime_index += 1 return matches_sum
project_euler
def solution(n: int = 100) -> int: sum_of_squares = sum(i * i for i in range(1, n + 1)) square_of_sum = int(math.pow(sum(range(1, n + 1)), 2)) return square_of_sum - sum_of_squares
project_euler
def solution(n: int = 100) -> int: sum_cubes = (n * (n + 1) // 2) ** 2 sum_squares = n * (n + 1) * (2 * n + 1) // 6 return sum_cubes - sum_squares
project_euler
def solution(n: int = 100) -> int: sum_of_squares = 0 sum_of_ints = 0 for i in range(1, n + 1): sum_of_squares += i**2 sum_of_ints += i return sum_of_ints**2 - sum_of_squares
project_euler
def solution(n: int = 100) -> int: sum_of_squares = n * (n + 1) * (2 * n + 1) / 6 square_of_sum = (n * (n + 1) / 2) ** 2 return int(square_of_sum - sum_of_squares)
project_euler
def solution(n: int = 1000) -> int: result = 0 for i in range(n): if i % 3 == 0 or i % 5 == 0: result += i return result
project_euler
def solution(n: int = 1000) -> int: total = 0 num = 0 while 1: num += 3 if num >= n: break total += num num += 2 if num >= n: break total += num num += 1 if num >= n: break total += num num += 3 if num >= n: break total += num num += 1 if num >= n: break total += num num += 2 if num >= n: break total += num num += 3 if num >= n: break total += num return total
project_euler
def solution(n: int = 1000) -> int: total = 0 terms = (n - 1) // 3 total += ((terms) * (6 + (terms - 1) * 3)) // 2 # total of an A.P. terms = (n - 1) // 5 total += ((terms) * (10 + (terms - 1) * 5)) // 2 terms = (n - 1) // 15 total -= ((terms) * (30 + (terms - 1) * 15)) // 2 return total
project_euler
def solution(n: int = 1000) -> int: a = 3 result = 0 while a < n: if a % 3 == 0 or a % 5 == 0: result += a elif a % 15 == 0: result -= a a += 1 return result
project_euler
def solution(n: int = 1000) -> int: return sum(e for e in range(3, n) if e % 3 == 0 or e % 5 == 0)
project_euler
def solution(n: int = 1000) -> int: return sum(i for i in range(n) if i % 3 == 0 or i % 5 == 0)
project_euler
def solution(n: int = 1000) -> int: xmulti = [] zmulti = [] z = 3 x = 5 temp = 1 while True: result = z * temp if result < n: zmulti.append(result) temp += 1 else: temp = 1 break while True: result = x * temp if result < n: xmulti.append(result) temp += 1 else: break collection = list(set(xmulti + zmulti)) return sum(collection)
project_euler
def is_palindrome(n: int) -> bool: return str(n) == str(n)[::-1]
project_euler
def sum_reverse(n: int) -> int: return int(n) + int(str(n)[::-1])
project_euler
def solution(limit: int = 10000) -> int: lychrel_nums = [] for num in range(1, limit): iterations = 0 a = num while iterations < 50: num = sum_reverse(num) iterations += 1 if is_palindrome(num): break else: lychrel_nums.append(a) return len(lychrel_nums)
project_euler
def solution(): i = 1 while True: if ( sorted(str(i)) == sorted(str(2 * i)) == sorted(str(3 * i)) == sorted(str(4 * i)) == sorted(str(5 * i)) == sorted(str(6 * i)) ): return i i += 1
project_euler
def solution(data_file: str = "base_exp.txt") -> int: largest: float = 0 result = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))): a, x = list(map(int, line.split(","))) if x * log10(a) > largest: largest = x * log10(a) result = i + 1 return result
project_euler
def continuous_fraction_period(n: int) -> int: numerator = 0.0 denominator = 1.0 root = int(sqrt(n)) integer_part = root period = 0 while integer_part != 2 * root: numerator = denominator * integer_part - numerator denominator = (n - numerator**2) / denominator integer_part = int((root + numerator) / denominator) period += 1 return period
project_euler
def solution(n: int = 10000) -> int: count_odd_periods = 0 for i in range(2, n + 1): sr = sqrt(i) if sr - floor(sr) != 0 and continuous_fraction_period(i) % 2 == 1: count_odd_periods += 1 return count_odd_periods
project_euler
def solution(n: int = 10) -> str: if not isinstance(n, int) or n < 0: raise ValueError("Invalid input") modulus = 10**n number = 28433 * (pow(2, 7830457, modulus)) + 1 return str(number % modulus)
project_euler
def solution(max_base: int = 10, max_power: int = 22) -> int: bases = range(1, max_base) powers = range(1, max_power) return sum( 1 for power in powers for base in bases if len(str(base**power)) == power )
project_euler
def least_divisible_repunit(divisor: int) -> int: if divisor % 5 == 0 or divisor % 2 == 0: return 0 repunit = 1 repunit_index = 1 while repunit: repunit = (10 * repunit + 1) % divisor repunit_index += 1 return repunit_index
project_euler
def solution(limit: int = 1000000) -> int: divisor = limit - 1 if divisor % 2 == 0: divisor += 1 while least_divisible_repunit(divisor) <= limit: divisor += 2 return divisor
project_euler
def solution(length: int = 50) -> int: different_colour_ways_number = [[0] * 3 for _ in range(length + 1)] for row_length in range(length + 1): for tile_length in range(2, 5): for tile_start in range(row_length - tile_length + 1): different_colour_ways_number[row_length][tile_length - 2] += ( different_colour_ways_number[row_length - tile_start - tile_length][ tile_length - 2 ] + 1 ) return sum(different_colour_ways_number[length])
project_euler
def solution(n: int = 1000) -> int: return sum(2 * a * ((a - 1) // 2) for a in range(3, n + 1))
project_euler
def solution(limit: int = 1000000) -> int: answer = 0 for outer_width in range(3, (limit // 4) + 2): if outer_width**2 > limit: hole_width_lower_bound = max(ceil(sqrt(outer_width**2 - limit)), 1) else: hole_width_lower_bound = 1 if (outer_width - hole_width_lower_bound) % 2: hole_width_lower_bound += 1 answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1 return answer
project_euler
def is_sq(number: int) -> bool: sq: int = int(number**0.5) return number == sq * sq
project_euler
def add_three( x_num: int, x_den: int, y_num: int, y_den: int, z_num: int, z_den: int ) -> tuple[int, int]: top: int = x_num * y_den * z_den + y_num * x_den * z_den + z_num * x_den * y_den bottom: int = x_den * y_den * z_den hcf: int = gcd(top, bottom) top //= hcf bottom //= hcf return top, bottom
project_euler
def solution(order: int = 35) -> int: unique_s: set = set() hcf: int total: Fraction = Fraction(0) fraction_sum: tuple[int, int] for x_num in range(1, order + 1): for x_den in range(x_num + 1, order + 1): for y_num in range(1, order + 1): for y_den in range(y_num + 1, order + 1): # n=1 z_num = x_num * y_den + x_den * y_num z_den = x_den * y_den hcf = gcd(z_num, z_den) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: fraction_sum = add_three( x_num, x_den, y_num, y_den, z_num, z_den ) unique_s.add(fraction_sum) # n=2 z_num = ( x_num * x_num * y_den * y_den + x_den * x_den * y_num * y_num ) z_den = x_den * x_den * y_den * y_den if is_sq(z_num) and is_sq(z_den): z_num = int(sqrt(z_num)) z_den = int(sqrt(z_den)) hcf = gcd(z_num, z_den) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: fraction_sum = add_three( x_num, x_den, y_num, y_den, z_num, z_den ) unique_s.add(fraction_sum) # n=-1 z_num = x_num * y_num z_den = x_den * y_num + x_num * y_den hcf = gcd(z_num, z_den) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: fraction_sum = add_three( x_num, x_den, y_num, y_den, z_num, z_den ) unique_s.add(fraction_sum) # n=2 z_num = x_num * x_num * y_num * y_num z_den = ( x_den * x_den * y_num * y_num + x_num * x_num * y_den * y_den ) if is_sq(z_num) and is_sq(z_den): z_num = int(sqrt(z_num)) z_den = int(sqrt(z_den)) hcf = gcd(z_num, z_den) z_num //= hcf z_den //= hcf if 0 < z_num < z_den <= order: fraction_sum = add_three( x_num, x_den, y_num, y_den, z_num, z_den ) unique_s.add(fraction_sum) for num, den in unique_s: total += Fraction(num, den) return total.denominator + total.numerator
project_euler
def solution(t_limit: int = 1000000, n_limit: int = 10) -> int: count: defaultdict = defaultdict(int) for outer_width in range(3, (t_limit // 4) + 2): if outer_width * outer_width > t_limit: hole_width_lower_bound = max( ceil(sqrt(outer_width * outer_width - t_limit)), 1 ) else: hole_width_lower_bound = 1 hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2 for hole_width in range(hole_width_lower_bound, outer_width - 1, 2): count[outer_width * outer_width - hole_width * hole_width] += 1 return sum(1 for n in count.values() if 1 <= n <= 10)
project_euler
def reversible_numbers( remaining_length: int, remainder: int, digits: list[int], length: int ) -> int: if remaining_length == 0: if digits[0] == 0 or digits[-1] == 0: return 0 for i in range(length // 2 - 1, -1, -1): remainder += digits[i] + digits[length - i - 1] if remainder % 2 == 0: return 0 remainder //= 10 return 1 if remaining_length == 1: if remainder % 2 == 0: return 0 result = 0 for digit in range(10): digits[length // 2] = digit result += reversible_numbers( 0, (remainder + 2 * digit) // 10, digits, length ) return result result = 0 for digit1 in range(10): digits[(length + remaining_length) // 2 - 1] = digit1 if (remainder + digit1) % 2 == 0: other_parity_digits = ODD_DIGITS else: other_parity_digits = EVEN_DIGITS for digit2 in other_parity_digits: digits[(length - remaining_length) // 2] = digit2 result += reversible_numbers( remaining_length - 2, (remainder + digit1 + digit2) // 10, digits, length, ) return result
project_euler
def solution(max_power: int = 9) -> int: result = 0 for length in range(1, max_power + 1): result += reversible_numbers(length, 0, [0] * length, length) return result
project_euler
def digit_sum(n: int) -> int: return sum(int(digit) for digit in str(n))
project_euler
def solution(n: int = 30) -> int: digit_to_powers = [] for digit in range(2, 100): for power in range(2, 100): number = int(math.pow(digit, power)) if digit == digit_sum(number): digit_to_powers.append(number) digit_to_powers.sort() return digit_to_powers[n - 1]
project_euler
def solution(num_turns: int = 15) -> int: total_prob: float = 0.0 prob: float num_blue: int num_red: int ind: int col: int series: tuple[int, ...] for series in product(range(2), repeat=num_turns): num_blue = series.count(1) num_red = num_turns - num_blue if num_red >= num_blue: continue prob = 1.0 for ind, col in enumerate(series, 2): if col == 0: prob *= (ind - 1) / ind else: prob *= 1 / ind total_prob += prob return int(1 / total_prob)
project_euler
def solution(length: int = 50) -> int: ways_number = [1] * (length + 1) for row_length in range(length + 1): for tile_length in range(2, 5): for tile_start in range(row_length - tile_length + 1): ways_number[row_length] += ways_number[ row_length - tile_start - tile_length ] return ways_number[length]
project_euler
def circle_bottom_arc_integral(point: float) -> float: return ( (1 - 2 * point) * sqrt(point - point**2) + 2 * point + asin(sqrt(1 - point)) ) / 4
project_euler
def concave_triangle_area(circles_number: int) -> float: intersection_y = (circles_number + 1 - sqrt(2 * circles_number)) / ( 2 * (circles_number**2 + 1) ) intersection_x = circles_number * intersection_y triangle_area = intersection_x * intersection_y / 2 concave_region_area = circle_bottom_arc_integral( 1 / 2 ) - circle_bottom_arc_integral(intersection_x) return triangle_area + concave_region_area
project_euler
def solution(fraction: float = 1 / 1000) -> int: l_section_area = (1 - pi / 4) / 4 for n in count(1): if concave_triangle_area(n) / l_section_area < fraction: return n return -1
project_euler