-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path41 polynomial.py
53 lines (50 loc) · 1.45 KB
/
41 polynomial.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
from math import sqrt, log2
from random import randint, choice
def dgtlRoot(number):
number = sum([int(char) for char in str(number)])
if len(str(number)) != 1:
return dgtlRoot(number)
return number
def prime(num): # Slightly different to prime finder.py
# Presents reasons for (non-)primality
if num == 1:
return False
else:
factors = set()
x = 1
if (str(num)[-1] in '245680'\
and num not in [2,5]):
# print('Final digit!')
pass
elif (dgtlRoot(num) % 3 == 0\
and num != 3):
# print('Digital root shows...')
return False
while x <= sqrt(num):
if (num % 2 == 1 and not x % 2):
pass
elif not num % x:
factors.update((x, num//x))
if len(factors) > 2:
return False
x += 1
if len(factors) == 2:
# print('Two factors.')
return True
entry = 1
while True:
# entryVal = False
# while not entryVal:
# try:
# entry = int(input('Enter number to check primality: '))
# entryVal = True
# except ValueError:
# print('Please enter a valid number.\n')
poly = entry**2+entry+41
print(f'n = {entry}')
if prime(poly):
print(f'{poly} is prime.\n')
else:
print(f'{poly} is NOT prime.\n')
break
entry += 1