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


This tutorial will discuss how to write swift program to check whether a number can be expressed as a sum of two prime numbers.

Prime numbers are those numbers that are greater than 1 and has exactly two factors that is 1 and the number itself. For example, 7 is the prime number because it has only two factors that are 1 and 7.

Here we check that the given number can be expressed as Sum of two prime numbers. For example −

Number = 10

So 10 can be expressed as a sum of prime numbers −

3+7 = 10
5+5 = 10
Similarly
Number  = 11

So 11 can not be expressed as a sum of prime numbers -

10+1 = 11
5+6 = 11

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 34

Output

The desired output would be −

Yes 34 is the sum of prime numbers.

Algorithm

Following is the algorithm −

Step 1 − Create a function to check prime numbers.

Step 2 − Declare a variable to store number.

Step 3 − Run a for loop starting from 2 to N/2.

Step 4 − Check if y is a prime number not.

Step 5 − If y is a prime number, then check N-y is a prime number of not.

Step 6 − If both y and N-y are prime numbers, then the given number can be expressed as a sum of y and N-y.

Step 7 − If both y and N-y or either y or N-y is not a prime number, then the given number can not be expressed as the sum of prime numbers.

Step 8 − Print the output.

Example

The following program shows how to check whether a number can be expressed as a sum of two prime numbers.

import Swift
import Foundation
func CheckPrime(num: Int)->Bool{
   if (num == 0 || num == 1){
      return false
   }
   for x in stride(from:2, to: num/2 + 1, by:1){
      if ((num % x) == 0){
         return false
      }
   }
   return true
}
var N = 10
var mFlag = 0
for y in stride(from:2, to: N/2 + 1, by:1){
   if (CheckPrime(num: y) == true){
      if(CheckPrime(num: N-y) == true){
         print("\(N) can be expressed as the sum of \(y) and \(N-y)")
         mFlag = 1
      }
   }
}
if(mFlag == 0){
   print("\(N) can not be expressed as the sum of two prime numbers")
}

Output

10 can be expressed as the sum of 3 and 7
10 can be expressed as the sum of 5 and 5

Here, in the above code, first we create a function named CheckPrime(). This function is used to check the given number is prime number or not. If the given number is prime number then it return true. Otherwise return false.

Now we declare a variable named mFlag = 0. If mFlag = 1, that means the given number can be expressed as the sum of prime number. If mFlag = 0, that means the given number cannot be expressed as the sum of prime numbers.

Now we iterate a for loop(starting from 2 to n/2). In each, iteration it is used to check whether y and is a prime number or not. If y is prime number, then check N-y is prime number or not. When both y and N-y are prime number that means the given number can be expressed as the sum of y and N-y prime numbers. Hence print the output and set the value of mFlag = 1. This process continue till the for loop ends.

Updated on: 13-Dec-2022

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements