
- Swift Tutorial
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Data Types
- Swift - Variables
- Swift - Optionals
- Swift - Tuples
- Swift - Constants
- Swift - Literals
- Swift - Operators
- Swift - Decision Making
- Swift - Loops
- Swift - Strings
- Swift - Characters
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift - Functions
- Swift - Closures
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift - Initialization
- Swift - Deinitialization
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Type Casting
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift Useful Resources
- Swift - Compile Online
- Swift - Quick Guide
- Swift - Useful Resources
- Swift - Discussion
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.
- Related Articles
- C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Java Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Haskell Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers
- Check if a prime number can be expressed as sum of two Prime Numbers in Python
- C program for a number to be expressed as a sum of two prime numbers.
- Check if a number can be expressed as sum two abundant numbers in C++
- Check if a number can be expressed as a sum of consecutive numbers in C++
- Check if an integer can be expressed as a sum of two semi-primes in Python
- Check if a number can be expressed as power in C++
- Check if a number can be expressed as a^b in C++
- Check if a number can be expressed as a^b in Python
- Program to check a number can be written as a sum of distinct factorial numbers or not in Python
- C++ program to find ways an integer can be expressed as sum of n-th power of unique natural numbers
- Check if a number can be represented as a sum of 2 triangular numbers in C++
- Check if a number can be expressed as 2^x + 2^y in C++
