-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
81 lines (59 loc) · 1.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import math
def calculate_mersenne_primes(limit: int) -> list:
"""
Calculates Mersenne primes up to a given limit.
Parameters:
- limit: int
The upper limit for calculating Mersenne primes.
Returns:
- list:
A list of Mersenne primes up to the given limit.
Raises:
- ValueError:
Raises an error if the limit is too large.
"""
if limit > 1000000:
raise ValueError("The limit is too large. Please provide a smaller limit.")
mersenne_primes = []
for i in range(2, limit + 1):
if is_mersenne_prime(i):
mersenne_primes.append(i)
return mersenne_primes
def is_mersenne_prime(n: int) -> bool:
"""
Checks if a given number is a Mersenne prime.
Parameters:
- n: int
The number to check.
Returns:
- bool:
True if the number is a Mersenne prime, False otherwise.
"""
if not is_prime(n):
return False
mersenne_number = 2 ** n - 1
return is_prime(mersenne_number)
def is_prime(n: int) -> bool:
"""
Checks if a given number is prime.
Parameters:
- n: int
The number to check.
Returns:
- bool:
True if the number is prime, False otherwise.
"""
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
# Example usage:
try:
limit = int(input("Enter the limit for calculating Mersenne primes: "))
mersenne_primes = calculate_mersenne_primes(limit)
print("Mersenne primes up to the limit:")
print(mersenne_primes)
except ValueError as e:
print(f"Error: {e}")