-
-
Notifications
You must be signed in to change notification settings - Fork 49.4k
Add solution for Problem 810 #13413
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
Open
Heisenberg-Vader
wants to merge
9
commits into
TheAlgorithms:master
Choose a base branch
from
Heisenberg-Vader:soln-810
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add solution for Problem 810 #13413
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
67babd3
Add solution for Problem 810
Heisenberg-Vader 743830a
Add solution for problem 810
Heisenberg-Vader aa30a0b
Add solution for Problem 810
Heisenberg-Vader d88cb32
Add solution for Problem 810
Heisenberg-Vader ad1e62a
Add solution for Problem 810
Heisenberg-Vader 590e47b
Add solution for problem 810
Heisenberg-Vader c612420
Merge branch 'master' into soln-810
Heisenberg-Vader 56b682f
Merge branch 'master' into soln-810
Heisenberg-Vader b151e63
Merge branch 'master' into soln-810
Heisenberg-Vader 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
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,158 @@ | ||
| """ | ||
| Project Euler Problem 810: https://projecteuler.net/problem=810 | ||
|
|
||
| We use x ⊕ y for the bitwise XOR of x and y. | ||
| Define the XOR-product of x and y, denoted by x ⊗ y, | ||
| similar to a long multiplication in base 2, | ||
| except the intermediate results are XORed instead of usual integer addition. | ||
|
|
||
| For example, 7 ⊗ 3 = 9, or in base 2, 111_2 ⊗ 11_2 = 1001_2: | ||
| 111 | ||
| ⊗ 11 | ||
| ------- | ||
| 111 | ||
| 111 | ||
| ------- | ||
| 1001 | ||
| An XOR-Prime is an integer n greater than 1 that is not an | ||
| XOR-product of two integers greater than 1. | ||
| The above example shows that 9 is not an XOR-prime. | ||
| Similarly, 5 = 3 ⊗ 3 is not an XOR-prime. | ||
| The first few XOR-primes are 2, 3, 7, 11, 13, ... and the 10th XOR-prime is 41. | ||
|
|
||
| Find the 5,000,000.th XOR-prime. | ||
|
|
||
| References: | ||
| http://en.wikipedia.org/wiki/M%C3%B6bius_function | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| def xor_multiply(op_a: int, op_b: int) -> int: | ||
| """ | ||
| Perform XOR multiplication of two integers, equivalent to polynomial | ||
| multiplication in GF(2). | ||
|
|
||
| >>> xor_multiply(3, 5) # (011) ⊗ (101) | ||
| 15 | ||
| """ | ||
| res = 0 | ||
| while op_b: | ||
| if op_b & 1: | ||
| res ^= op_a | ||
| op_a <<= 1 | ||
| op_b >>= 1 | ||
| return res | ||
|
|
||
|
|
||
| def divisors(num: int) -> set[int]: | ||
| """ | ||
| Return all divisors of `num` (excluding 0). | ||
|
|
||
| >>> divisors(12) | ||
| {1, 2, 3, 4, 6, 12} | ||
| """ | ||
| s = {1} | ||
| for i in range(2, int(num**0.5) + 1): | ||
| if num % i == 0: | ||
| s.add(i) | ||
| s.add(num // i) | ||
| s.add(num) | ||
| return s | ||
|
|
||
|
|
||
| def mobius_table(n: int) -> list[int]: | ||
|
||
| """ | ||
| Generate a variant of Möbius function values from 1 to n. | ||
|
|
||
| >>> mobius_table(10)[:6] | ||
| [0, 1, -1, -1, 0, -1] | ||
| """ | ||
| mob = [1] * (n + 1) | ||
| is_prime = [True] * (n + 1) | ||
| mob[0] = 0 | ||
|
|
||
| for p in range(2, n + 1): | ||
| if is_prime[p]: | ||
| mob[p] = -1 | ||
| for j in range(2 * p, n + 1, p): | ||
| is_prime[j] = False | ||
| mob[j] *= -1 | ||
| p2 = p * p | ||
| if p2 <= n: | ||
| for j in range(p2, n + 1, p2): | ||
| mob[j] = 0 | ||
| return mob | ||
|
|
||
|
|
||
| def count_irreducibles(deg: int) -> int: | ||
| """ | ||
| Count the number of irreducible polynomials of degree `deg` over GF(2) | ||
| using the variant of Möbius function. | ||
|
|
||
| >>> count_irreducibles(3) | ||
| 2 | ||
| """ | ||
| mob = mobius_table(deg) | ||
| total = 0 | ||
| for div in divisors(deg) | {deg}: | ||
| total += mob[div] * (1 << (deg // div)) | ||
| return total // deg | ||
|
|
||
|
|
||
| def find_xor_prime(rank: int) -> int: | ||
| """ | ||
| Find the Nth XOR prime using a bitarray-based sieve. | ||
|
|
||
| >>> find_xor_prime(10) | ||
| 41 | ||
| """ | ||
| total, degree = 0, 1 | ||
| while True: | ||
| count = count_irreducibles(degree) | ||
| if total + count > rank: | ||
| break | ||
| total += count | ||
| degree += 1 | ||
|
|
||
| limit = 1 << (degree + 1) | ||
|
|
||
| sieve = [True] * limit | ||
| sieve[0] = sieve[1] = False | ||
|
|
||
| for even in range(4, limit, 2): | ||
| sieve[even] = False | ||
|
|
||
| current = 1 | ||
| for i in range(3, limit, 2): | ||
| if sieve[i]: | ||
| current += 1 | ||
| if current == rank: | ||
| return i | ||
|
|
||
| j = i | ||
| while True: | ||
| prod = xor_multiply(i, j) | ||
| if prod >= limit: | ||
| break | ||
| sieve[prod] = False | ||
| j += 2 | ||
|
|
||
| raise ValueError( | ||
| "Failed to locate the requested XOR-prime within the computed limit" | ||
| ) | ||
|
|
||
|
|
||
| def solution(limit: int = 5000001) -> int: | ||
| """ | ||
| Wrapper for Project Euler-style solution function. | ||
|
|
||
| >>> solution(10) | ||
| 41 | ||
| """ | ||
| result = find_xor_prime(limit) | ||
| return result | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| print(f"{solution(5000000) = }") | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
n