created and first commit(3 solusions)
This commit is contained in:
26
solutions/0000.zero/euler_0.py
Normal file
26
solutions/0000.zero/euler_0.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
Project Euler Problem 0 :
|
||||
Find the sum of all the odd squares which do not exceed 764000.
|
||||
|
||||
这是注册问题。
|
||||
"""
|
||||
|
||||
UP_NUM = 764000
|
||||
|
||||
|
||||
def is_even(num: int) -> bool:
|
||||
"""Check if a number is even."""
|
||||
return num & 1 == 0
|
||||
|
||||
|
||||
def main():
|
||||
res: list[int] = []
|
||||
for i in range(UP_NUM):
|
||||
t = (i + 1) ** 2
|
||||
if not is_even(t):
|
||||
res.append(t)
|
||||
print(sum(res))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
26
solutions/0001.multiples/euler_1.py
Normal file
26
solutions/0001.multiples/euler_1.py
Normal file
@@ -0,0 +1,26 @@
|
||||
"""
|
||||
Project Euler Problem 1 :
|
||||
Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
"""
|
||||
|
||||
UP_NUM = 1000
|
||||
|
||||
|
||||
def condition(num: int) -> bool:
|
||||
if num % 3 == 0:
|
||||
return True
|
||||
elif num % 5 == 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
res = []
|
||||
for i in range(1, UP_NUM):
|
||||
if condition(i):
|
||||
res.append(i)
|
||||
print(sum(res))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
48
solutions/0002.even_fibonacci/euler_2.py
Normal file
48
solutions/0002.even_fibonacci/euler_2.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Even Fibonacci Numbers
|
||||
|
||||
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
1, 2, 3, 5, 8, 13, 21, 34, 55, 89
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||
"""
|
||||
|
||||
UP_NUM = 4000000
|
||||
|
||||
|
||||
def is_even(num: int) -> bool:
|
||||
"""Check if a number is even."""
|
||||
return num & 1 == 0
|
||||
|
||||
|
||||
"""
|
||||
下面这个方法实在太慢,计算400万项还会内存溢出。
|
||||
```python
|
||||
def fibonacci_sequence(n: int) -> list[int]:
|
||||
a, b = 1, 2
|
||||
res = [a, b]
|
||||
while b <= n:
|
||||
a, b = b, a + b
|
||||
res.append(b)
|
||||
return res
|
||||
|
||||
|
||||
def main():
|
||||
fib_seq = fibonacci_sequence(UP_NUM)
|
||||
even_sum = sum(num for num in fib_seq if is_even(num))
|
||||
print(even_sum)
|
||||
```
|
||||
"""
|
||||
|
||||
|
||||
def even_fibonacci(limit: int) -> int:
|
||||
a, b = 1, 2
|
||||
even_sum = 0
|
||||
while b <= limit:
|
||||
if is_even(b):
|
||||
even_sum += b
|
||||
a, b = b, a + b
|
||||
return even_sum
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(even_fibonacci(UP_NUM))
|
||||
103
solutions/0003.largestprime/euler_3.py
Normal file
103
solutions/0003.largestprime/euler_3.py
Normal file
@@ -0,0 +1,103 @@
|
||||
import random
|
||||
from math import gcd
|
||||
from typing import List, Set
|
||||
|
||||
|
||||
def is_probable_prime(n: int, trials: int = 10) -> bool:
|
||||
"""Miller-Rabin素性测试(快速判断是否为质数)"""
|
||||
if n < 2:
|
||||
return False
|
||||
if n in (2, 3):
|
||||
return True
|
||||
if n % 2 == 0:
|
||||
return False
|
||||
|
||||
# 将 n-1 写成 d * 2^s 的形式
|
||||
d = n - 1
|
||||
s = 0
|
||||
while d % 2 == 0:
|
||||
d //= 2
|
||||
s += 1
|
||||
|
||||
# 测试
|
||||
for _ in range(trials):
|
||||
a = random.randrange(2, n - 1)
|
||||
x = pow(a, d, n)
|
||||
if x == 1 or x == n - 1:
|
||||
continue
|
||||
for _ in range(s - 1):
|
||||
x = pow(x, 2, n)
|
||||
if x == n - 1:
|
||||
break
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def pollards_rho(n: int, max_iter: int = 100000) -> int:
|
||||
"""
|
||||
Pollard's Rho 算法:返回n的一个非平凡因子
|
||||
|
||||
Args:
|
||||
n: 待分解的合数
|
||||
max_iter: 最大迭代次数防止无限循环
|
||||
|
||||
Returns:
|
||||
n的一个因子(可能是质数也可能是合数)
|
||||
若失败返回None
|
||||
"""
|
||||
if n % 2 == 0:
|
||||
return 2
|
||||
|
||||
# 随机生成多项式 f(x) = x^2 + c (mod n)
|
||||
c = random.randrange(1, n - 1)
|
||||
|
||||
def f(x):
|
||||
return (pow(x, 2, n) + c) % n
|
||||
|
||||
# Floyd 判圈算法
|
||||
x = random.randrange(2, n - 1)
|
||||
y = x
|
||||
d = 1
|
||||
|
||||
iter_count = 0
|
||||
while d == 1 and iter_count < max_iter:
|
||||
x = f(x) # 乌龟:走一步
|
||||
y = f(f(y)) # 兔子:走两步
|
||||
d = gcd(abs(x - y), n)
|
||||
iter_count += 1
|
||||
|
||||
if d == n:
|
||||
# 失败,尝试其他参数(递归或返回None)
|
||||
return pollards_rho(n, max_iter) if max_iter > 1000 else None
|
||||
|
||||
return d
|
||||
|
||||
|
||||
def factorize(n: int | None) -> List[int | None]:
|
||||
"""
|
||||
完整因数分解:递归分解所有质因数
|
||||
|
||||
Args:
|
||||
n: 待分解的正整数
|
||||
|
||||
Returns:
|
||||
质因数列表(可能含重复)
|
||||
"""
|
||||
if n == 1:
|
||||
return []
|
||||
|
||||
# 如果是质数,直接返回
|
||||
if is_probable_prime(n):
|
||||
return [n]
|
||||
|
||||
# 获取一个因子
|
||||
factor = pollards_rho(n)
|
||||
|
||||
# 递归分解
|
||||
return factorize(factor) + factorize(n // factor)
|
||||
|
||||
|
||||
def get_prime_factors(n: int) -> Set[int | None]:
|
||||
"""获取所有不重复的质因数"""
|
||||
return set(factorize(n))
|
||||
Reference in New Issue
Block a user