DEV Community

Kiruthiga S
Kiruthiga S

Posted on

Python

26) Sum of 2 Prime Numbers is given number

num = int(input("Enter a number: "))
i = 2
while i <= num // 2:
    second = num - i

    count1 = 0
    j = 1
    while j <= i:
        if i % j == 0:
            count1 += 1
        j += 1

    count2 = 0
    j = 1
    while j <= second:
        if second % j == 0:
            count2 += 1
        j += 1

    if count1 == 2 and count2 == 2:
        print(i, "+", second, "=", num)
    i += 1
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 10
3 + 7 = 10
5 + 5 = 10

27)Express 68 and 128 as the sum of two consecutive primes

num = int(input("Enter a number: "))
i = 2
while i < num:
    count1 = 0
    j = 1
    while j <= i:
        if i % j == 0:
            count1 += 1
        j += 1

    if count1 == 2:
        second = i + 1

        while True:
            count2 = 0
            j = 1
            while j <= second:
                if second % j == 0:
                    count2 += 1
                j += 1

            if count2 == 2:
                break

            second += 1
        if i + second == num:
            print(num, "=", i, "+", second)
            break
    i += 1 
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 68
68 = 31 + 37
Enter a number: 128
128 = 61 + 67

28)Express 79 and 104 as the sum of any three primes

num = int(input("Enter a number: "))
i = 2
while i < num:
    count1 = 0
    j = 1
    while j <= i:
        if i % j == 0:
            count1 += 1
        j += 1

    if count1 == 2:
        k = i + 1
        while k < num:

            count2 = 0
            j = 1
            while j <= k:
                if k % j == 0:
                    count2 += 1
                j += 1
            if count2 == 2:
                third = num - i - k

                count3 = 0
                j = 1
                while j <= third:
                    if third % j == 0:
                        count3 += 1
                    j += 1

                if count3 == 2:
                    print(num, "=", i, "+", k, "+", third)
                    break
            k += 1

        if count3 == 2:
            break
    i += 1
Enter fullscreen mode Exit fullscreen mode

Output:
Enter a number: 79
79 = 3 + 5 + 71
Enter a number: 104
104 = 2 + 5 + 97

Top comments (0)