+ 1

#help Write a program in python to check whether a number can be expanded as the sum of two prime numbers.

For example, the number can be expanded as two prime numbers 2 and 7, 9=2+7

4th Apr 2019, 3:06 PM
Misal Chugh
Misal Chugh - avatar
3 Respostas
+ 3
Misal, I am not sure what is your question? What have you tried so far? Can you share us some code?
4th Apr 2019, 3:51 PM
Lothar
Lothar - avatar
+ 3
hi Misla, Hmmmm, i am not sure what your code is doing. It creates a long list of numbers - i have no glue what this means. I would suggest you start from scratch. Create a list of primes from 2 up to the input value. Then iterate through this list and find the 2 prime values that give in sum the input input number. Here something you can start with: def getprime(n): primelist = [] for i in range(2, n + 1): if i > 1: for n in range(2, i): if (i % n) == 0: break else: print(i) primelist.append(i) print('\n', primelist) The rest of the code is your part.
4th Apr 2019, 7:12 PM
Lothar
Lothar - avatar
0
Misal, I am not sure what is your question? What have you tried so far? Can you share us some code? Hii..Lother here is my code.. def nextPrime(n): while not isPrime(n - 1): n = n + 1 return n def isPrime(n): for i in range(2, n): print(n) if n % i == 0: return 0 return 1 x = int(input('Enter any number : ')) for i in range(2, x - 1): i = nextPrime(i) if isPrime(x - 1): print(i, ' + ', x - 1, ' = ', x)
4th Apr 2019, 4:02 PM
Misal Chugh
Misal Chugh - avatar