Check whether a number can be expressed as a sum of two prime numbers In Python

               

    Check whether a number can be expressed as a sum of two prime numbers In Python

For example, the number 34 is given as input.

34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17

Program:

    # Python program to check whether a number can be expressed as a sum of two prime numbers

    def prime(n):
        isPrime = 1
        for i in range (2,int(n/2),1):
            if(n % i == 0):
                isPrime = 0
                break
        return isPrime

    n = int(input("Enter a number : "))
    f = 0;
    i = 2
    for i in range (2,int(n/2),1):
        if(prime(i) == 1):
            if(prime(n-i) == 1):
                print(n,"can be expressed as the sum of",i,"and",n-i)
                f = 1;
    if (f == 0):
        print(n,"cannot be expressed as the sum of two prime numbers")

    OutPut:
    Enter a number : 34
    34 can be expressed as the sum of 3 and 31
    34 can be expressed as the sum of 5 and 29
    34 can be expressed as the sum of 11 and 23


    Enter a number : 35
    35 can be expressed as the sum of 4 and 31

    Enter a number : 37
    37 cannot be expressed as the sum of two prime numbers

























    1 comment: