# Programma om interessantheid te berekenen
ISet = ((3, "D", "derde machten"),
(4, "D2", "driehoeksgetallen"),
(1, "E", "eindige decimale breuk getallen"),
(5, "F", "Fibonacci"),
(9, "F2", "Faculteit"),
(8, "K", "kwadraten"),
(2, "K2", "kwadraatvrije getallen"),
(3, "M", "Mersenne priemen"),
(7, "P", "priemgetallen"),
(6, "P3", "perfecte getallen"),
(1, "P4", "palindromen"))
M = [3,7,31,127,8191,131071,524287,2147483647, 2305843009213693951]
# Lijst van functies
def isCube(n):
d = math.floor(n ** (1 / 3) + 0.5)
return n == d ** 3
def isTriangular(n):
d1 = math.floor((8 * n + 1) ** (1 / 2) + 0.5)
d = math.floor((d1 - 1) / 2)
return n == d * (d + 1) / 2
def isFiniteDecimal(n):
d = n
while d % 2 == 0:
d = d/2
while d % 5 == 0:
d = d/5
return d == 1
def isFactorial(n):
i = 1;
while (True):
if (n % i == 0):
n //= i;
else:
break;
i += 1;
if (n == 1):
return True;
else:
return False;
def isPerfectSquare(x):
s = int(math.sqrt(x))
return s*s == x
def isFibonacci(n):
return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)
def isSquareFree(n):
d = 0
maxp = math.floor(n ** (1 / 2))
for pp in range(maxp):
p = pp + 2
if isPrime(p):
if n % (p * p) == 0:
d += 1
return d == 0
def isPrime(n):
if n == 2 or n == 3: return True
if n < 2 or n % 2 == 0: return False
if n < 9: return True
if n % 3 == 0: return False
r = int(n ** 0.5)
f = 5
while f <= r:
if n % f == 0: return False
if n % (f + 2) == 0: return False
f += 6
return True
def isPerfectNumber(n):
Sum = 0
for j in range(1, n):
if(n % j == 0):
Sum = Sum + j
if (Sum == n): return True;
else: return False;
def isPalindrome(n):
if n < 10: return True
if n < 100:
if n % 11 == 0:
return True
else:
return False
m = n % 101
h = n // 100
r = n - 100 * h
m2 = r // 10
return m == 10 * m2
def ISet_Run():
N = int(N_Range.get())
print(N)
for ii in range(N):
Interessantheid = 0
cnt = 0
i = ii+1
# derde machten
if isCube(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# driehoeksgetallen
if isTriangular(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# eindige decimale breuk getallen
if isFiniteDecimal(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# Fibonacci
if isFibonacci(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# Faculteit
if isFactorial(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# kwadraten
if isPerfectSquare(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# kwadraatvrije getallen
if isSquareFree(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# Mersenne priemen
if i in M:
Interessantheid += int(v_col[cnt].get())
cnt += 1
# priemgetallen
if isPrime(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# perfecte getallen
if isPerfectNumber(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# palindromen
if isPalindrome(i):
Interessantheid += int(v_col[cnt].get())
cnt += 1
# resultaat
print (i,Interessantheid)
return
iset = len(ISet)
# window
win = tk.Tk()
win.title("Interessantheid")
label_col = []
text_col = []
v_col = []
# interessante eigenschappen
for r in range(iset):
v = tk.StringVar(win, value=str(ISet[r][0]))
v_col.append(v)
ttk.Label(win, text=ISet[r][2]).grid(column = 1, row = r)
Ivalue = ttk.Entry(win, width = 3, textvariable = v).grid(column =
0, row = r)
text_col.append(Ivalue)
# Invoer
N_Label = ttk.Label(win, text="Range:").grid(column=2, row=0)
N_Range = tk.StringVar(win, value=str(100))
N_Entry = ttk.Entry(win, width=5, textvariable=N_Range).grid(column=2,
row=1)
# Start knop
Run = ttk.Button(win, text="Uitvoer", command=ISet_Run).grid(column=2,
row=2)
# Einde knop
Exit = ttk.Button(win, text="Einde", command=lambda win=win:
win.destroy()).grid(column=2, row=3)
win.mainloop()