-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimeNumbers.py
72 lines (56 loc) · 1.96 KB
/
PrimeNumbers.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
def program():
num = int(input("Enter a prime number: "))
arr = []
if num > 1:
for i in range(2, num):
if (num % i ) == 0:
print(num,"is not a prime number.")
break
check = True
for j in range(2,i):
if (i % j) == 0:
check = False
if check == True:
arr.append(i)
else:
arr.append(num)
combine(arr,num)
else:
print("1 is not a prime number.")
again()
def combine(arr,userInput):
if len(arr) > 1:
print("\n===== List of prime numbers till " +str(userInput)+ " ====")
total = 0
for index, num in enumerate(arr):
total = index + 1
print(str(total) + ". ",num)
print("\n===== All possible combinations =====")
for i in arr:
print("\n-----",i,"-----")
combinations = list(findCombinations(arr,i))
if len(combinations) > 0:
for x,j in enumerate(combinations):
print("["+str(x+1)+"] "+' + '.join(str(item) for item in j)+" = "+str(i))
else:
print("No possible combination for",i)
else:
print("No possible combination for",userInput)
def findCombinations(numbers, target, partial=[]):
if sum(partial) == target:
if len(partial) > 1:
yield partial
if sum(partial) >= target:
return
for i, n in enumerate(numbers):
remaining = numbers[i + 1:]
yield from findCombinations(remaining, target, partial + [n])
def again():
check_again = input('\nDo you want to run the program again? [Y/N]')
if check_again.upper() == 'Y':
program()
elif check_again.upper() == 'N':
print('See you later.')
else:
again()
program()