-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmersenne-primes-gui.py
93 lines (69 loc) · 2.39 KB
/
mersenne-primes-gui.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
82
83
84
85
86
87
88
89
90
91
92
93
import tkinter as tk
from tkinter import messagebox
import math
def is_prime(n):
"""
Checks if a number is prime.
Parameters:
- n: int
The number to be checked.
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
def generate_mersenne_primes(limit):
"""
Generates Mersenne primes up to a given limit.
Parameters:
- limit: int
The upper limit for generating Mersenne primes.
Returns:
- list:
A list of Mersenne primes up to the given limit.
"""
mersenne_primes = []
for n in range(limit + 1):
mersenne_number = 2 ** n - 1
if is_prime(n) and is_prime(mersenne_number):
mersenne_primes.append(mersenne_number)
return mersenne_primes
def generate_mersenne_primes_gui():
"""
Generates Mersenne primes based on user input using a GUI.
This function creates a GUI window that prompts the user to enter a limit for generating Mersenne primes.
It then calls the generate_mersenne_primes function to generate the Mersenne primes and displays the result
in a message box.
"""
def generate():
"""
Event handler for the Generate button.
This function gets the user input from the entry field, calls the generate_mersenne_primes function,
and displays the result in a message box.
"""
try:
limit = int(entry.get())
mersenne_primes = generate_mersenne_primes(limit)
messagebox.showinfo("Mersenne Primes", f"The Mersenne primes up to {limit} are: {mersenne_primes}")
except ValueError:
messagebox.showerror("Error", "Invalid input. Please enter a valid integer.")
# Create the GUI window
window = tk.Tk()
window.title("Mersenne Primes Generator")
# Create the label and entry field for user input
label = tk.Label(window, text="Enter the limit:")
label.pack()
entry = tk.Entry(window)
entry.pack()
# Create the Generate button
button = tk.Button(window, text="Generate", command=generate)
button.pack()
# Start the GUI event loop
window.mainloop()
# Call the generate_mersenne_primes_gui function to start the Mersenne primes generator GUI
generate_mersenne_primes_gui()