|
| 1 | +""" |
| 2 | +Project Euler Problem 70: https://projecteuler.net/problem=70 |
| 3 | +
|
| 4 | +Euler's Totient function, φ(n) [sometimes called the phi function], is used to |
| 5 | +determine the number of positive numbers less than or equal to n which are |
| 6 | +relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than |
| 7 | +nine and relatively prime to nine, φ(9)=6. |
| 8 | +
|
| 9 | +The number 1 is considered to be relatively prime to every positive number, so |
| 10 | +φ(1)=1. |
| 11 | +
|
| 12 | +Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation |
| 13 | +of 79180. |
| 14 | +
|
| 15 | +Find the value of n, 1 < n < 10^7, for which φ(n) is a permutation of n and |
| 16 | +the ratio n/φ(n) produces a minimum. |
| 17 | +
|
| 18 | +----- |
| 19 | +
|
| 20 | +This is essentially brute force. Calculate all totients up to 10^7 and |
| 21 | +find the minimum ratio of n/φ(n) that way. To minimize the ratio, we want |
| 22 | +to minimize n and maximize φ(n) as much as possible, so we can store the |
| 23 | +minimum fraction's numerator and denominator and calculate new fractions |
| 24 | +with each totient to compare against. To avoid dividing by zero, I opt to |
| 25 | +use cross multiplication. |
| 26 | +
|
| 27 | +References: |
| 28 | +Finding totients |
| 29 | +https://en.wikipedia.org/wiki/Euler's_totient_function#Euler's_product_formula |
| 30 | +""" |
| 31 | +from typing import List |
| 32 | + |
| 33 | + |
| 34 | +def get_totients(max_one: int) -> List[int]: |
| 35 | + """ |
| 36 | + Calculates a list of totients from 0 to max_one exclusive, using the |
| 37 | + definition of Euler's product formula. |
| 38 | +
|
| 39 | + >>> get_totients(5) |
| 40 | + [0, 1, 1, 2, 2] |
| 41 | +
|
| 42 | + >>> get_totients(10) |
| 43 | + [0, 1, 1, 2, 2, 4, 2, 6, 4, 6] |
| 44 | + """ |
| 45 | + totients = [0] * max_one |
| 46 | + |
| 47 | + for i in range(0, max_one): |
| 48 | + totients[i] = i |
| 49 | + |
| 50 | + for i in range(2, max_one): |
| 51 | + if totients[i] == i: |
| 52 | + for j in range(i, max_one, i): |
| 53 | + totients[j] -= totients[j] // i |
| 54 | + |
| 55 | + return totients |
| 56 | + |
| 57 | + |
| 58 | +def has_same_digits(num1: int, num2: int) -> bool: |
| 59 | + """ |
| 60 | + Return True if num1 and num2 have the same frequency of every digit, False |
| 61 | + otherwise. |
| 62 | +
|
| 63 | + digits[] is a frequency table where the index represents the digit from |
| 64 | + 0-9, and the element stores the number of appearances. Increment the |
| 65 | + respective index every time you see the digit in num1, and decrement if in |
| 66 | + num2. At the end, if the numbers have the same digits, every index must |
| 67 | + contain 0. |
| 68 | +
|
| 69 | + >>> has_same_digits(123456789, 987654321) |
| 70 | + True |
| 71 | +
|
| 72 | + >>> has_same_digits(123, 12) |
| 73 | + False |
| 74 | +
|
| 75 | + >>> has_same_digits(1234566, 123456) |
| 76 | + False |
| 77 | + """ |
| 78 | + digits = [0] * 10 |
| 79 | + |
| 80 | + while num1 > 0 and num2 > 0: |
| 81 | + digits[num1 % 10] += 1 |
| 82 | + digits[num2 % 10] -= 1 |
| 83 | + num1 //= 10 |
| 84 | + num2 //= 10 |
| 85 | + |
| 86 | + for digit in digits: |
| 87 | + if digit != 0: |
| 88 | + return False |
| 89 | + |
| 90 | + return True |
| 91 | + |
| 92 | + |
| 93 | +def solution(max: int = 10000000) -> int: |
| 94 | + """ |
| 95 | + Finds the value of n from 1 to max such that n/φ(n) produces a minimum. |
| 96 | +
|
| 97 | + >>> solution(100) |
| 98 | + 21 |
| 99 | +
|
| 100 | + >>> solution(10000) |
| 101 | + 4435 |
| 102 | + """ |
| 103 | + |
| 104 | + min_numerator = 1 # i |
| 105 | + min_denominator = 0 # φ(i) |
| 106 | + totients = get_totients(max + 1) |
| 107 | + |
| 108 | + for i in range(2, max + 1): |
| 109 | + t = totients[i] |
| 110 | + |
| 111 | + if i * min_denominator < min_numerator * t and has_same_digits(i, t): |
| 112 | + min_numerator = i |
| 113 | + min_denominator = t |
| 114 | + |
| 115 | + return min_numerator |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + print(f"{solution() = }") |
0 commit comments