-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Project Euler 70 Solution #3041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dhruvmanila
merged 11 commits into
TheAlgorithms:master
from
peteryao7:peteryao7_project-euler-70
Nov 21, 2020
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bbdaeb1
Add solution for Project Euler 70, Fixes: #2695
peteryao7 ca5da04
Remove parameter from solution()
peteryao7 ebdd4ed
Add tests for all functions, add fstring and positional arg for solut…
peteryao7 75bb975
Merge branch 'master' into peteryao7_project-euler-70
peteryao7 a3fb36c
Merge branch 'master' into peteryao7_project-euler-70
peteryao7 4d189fb
Rename directory to 070
peteryao7 8d89b90
Move up explanation to module code block
peteryao7 c34bd6a
Move solution() below helper functions, rename variables
peteryao7 4c4d3d2
Remove whitespace from defining min_numerator
peteryao7 bd354fe
Add whitespace
peteryao7 efc6e1d
Improve type hints with typing.List
dhruvmanila File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
""" | ||
Project Euler 70 | ||
https://projecteuler.net/problem=70 | ||
|
||
Euler's Totient function, φ(n) [sometimes called the phi function], is used to | ||
determine the number of positive numbers less than or equal to n which are | ||
relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than | ||
nine and relatively prime to nine, φ(9)=6. | ||
|
||
The number 1 is considered to be relatively prime to every positive number, so | ||
φ(1)=1. | ||
|
||
Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation | ||
of 79180. | ||
|
||
Find the value of n, 1 < n < 10^7, for which φ(n) is a permutation of n and | ||
the ratio n/φ(n) produces a minimum. | ||
|
||
----- | ||
|
||
This is essentially brute force. Calculate all totients up to 10^7 and | ||
find the minimum ratio of n/φ(n) that way. To minimize the ratio, we want | ||
to minimize n and maximize φ(n) as much as possible, so we can store the | ||
minimum fraction's numerator and denominator and calculate new fractions | ||
with each totient to compare against. To avoid dividing by zero, I opt to | ||
use cross multiplication. | ||
|
||
References: | ||
Finding totients | ||
https://en.wikipedia.org/wiki/Euler's_totient_function#Euler's_product_formula | ||
""" | ||
|
||
|
||
def solution(max: int = 10000000) -> int: | ||
""" | ||
Finds the value of n from 1 to max such that n/φ(n) produces a minimum. | ||
|
||
>>> solution(100) | ||
21 | ||
|
||
>>> solution(10000) | ||
4435 | ||
""" | ||
|
||
min_num = 1 # i | ||
min_den = 0 # φ(i) | ||
dhruvmanila marked this conversation as resolved.
Show resolved
Hide resolved
|
||
totients = get_totients(max + 1) | ||
|
||
for i in range(2, max + 1): | ||
t = totients[i] | ||
|
||
if i * min_den < min_num * t and has_same_digits(i, t): | ||
min_num = i | ||
min_den = t | ||
|
||
return min_num | ||
|
||
|
||
def get_totients(max_one: int) -> list: | ||
""" | ||
Calculates a list of totients from 0 to max_one exclusive, using the | ||
definition of Euler's product formula. | ||
|
||
>>> get_totients(5) | ||
[0, 1, 1, 2, 2] | ||
|
||
>>> get_totients(10) | ||
[0, 1, 1, 2, 2, 4, 2, 6, 4, 6] | ||
""" | ||
totients = [0] * max_one | ||
|
||
for i in range(0, max_one): | ||
totients[i] = i | ||
|
||
for i in range(2, max_one): | ||
if totients[i] == i: | ||
for j in range(i, max_one, i): | ||
totients[j] -= totients[j] // i | ||
|
||
return totients | ||
|
||
|
||
def has_same_digits(num1: int, num2: int) -> bool: | ||
""" | ||
Return True if num1 and num2 have the same frequency of every digit, False | ||
otherwise. | ||
|
||
digits[] is a frequency table where the index represents the digit from | ||
0-9, and the element stores the number of appearances. Increment the | ||
respective index every time you see the digit in num1, and decrement if in | ||
num2. At the end, if the numbers have the same digits, every index must | ||
contain 0. | ||
|
||
>>> has_same_digits(123456789, 987654321) | ||
True | ||
|
||
>>> has_same_digits(123, 12) | ||
False | ||
|
||
>>> has_same_digits(1234566, 123456) | ||
False | ||
""" | ||
digits = [0] * 10 | ||
|
||
while num1 > 0 and num2 > 0: | ||
digits[num1 % 10] += 1 | ||
digits[num2 % 10] -= 1 | ||
num1 //= 10 | ||
num2 //= 10 | ||
|
||
for digit in digits: | ||
if digit != 0: | ||
return False | ||
|
||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.