Swift Program to find the prime numbers from the array


In this article, we will learn how to write a swift program to find the prime numbers from the array.

Prime numbers are those numbers that are only divisible by 1 and itself. Or we can say prime number have only two factors that are 1 and the number itself. For example, 1, 2, 7, 3, 11, 31, etc. So to find the prime numbers from the given array, we individually check each element of the given array is prime or not.

Algorithm

  • Step 1 − Create a function.

  • Step 2 − Check if the number is greater than equal to 1 or not. If yes, then return true.

  • Step 3 − Run for loop from 2 to num-1.

  • Step 4 − Check if the num is divisible by any other number. If yes, then return false. Otherwise return true.

  • Step 5 − Create an array of integer type.

  • Step 6 − Run a for loop to iterate through each element of the array.

  • Step 7 − Pass each element in the function to check for prime number.

  • Step 8 − Print the output.

Example 1

Following Swift program to find the prime numbers from the array.

import Foundation
import Glibc

// Function to check if a number is prime
func CheckPrime(_ num: Int) -> Bool {
   if num <= 1 {
       return false
   }
   for i in 2..<num  {
      if num % i == 0 {
         return false
       }
   }
   return true
}

// Creating an array of integer type
let mArr = [11, 97, 43, 24, 601, 444, 733]
print("Original array:", mArr)
print("Prime numbers are:")

// Check and print the prime numbers
for n in mArr  {
   if CheckPrime(n)  {
      print(n)
   }
}

Output

Original array: [11, 97, 43, 24, 601, 444, 733]
Prime numbers are:
11
97
43
601
733

Here in the above code, we have an array of integer type. Now we create a function to find the prime numbers from the given array. So in this function, first we check if the number is less than or equal to 1. If the number is 1 or less than 1, then it will return false because 1 is not a prime number. If the number is greater than 1, then it moves to the for loop from 2 to num-1 and check if the num is divisible by any of them. If yes, then this function return false. Otherwise return true.

Example 2

Following Swift program to find the prime numbers from the array.

import Foundation
import Glibc

// Function to check if a number is prime
func CheckPrime(_ num: Int) -> Bool {
   if num <= 1  {
      return false
   }
   for i in 2..<num  {
      if num % i == 0  {
         return false
      }
   }
   return true
}

// Creating an array of integer type
let mArr = [733, 10, 11, 53, 24, 151]
print("Original array:", mArr)

let primeNum = mArr.filter { CheckPrime($0) }
print("Prime numbers are:", primeNum)

Output

Original array: [733, 10, 11, 53, 24, 151]
Prime numbers are: [733, 11, 53, 151]

Here in the above code, we have an array of integer type. Now we create a function to check if the given number is prime number or not. So now we use filter() function to create an array which contains all the elements that satisfy the given condition that is { CheckPrime($0) }. Here, filter() function pass each element of mArr array in the CheckPrime() to check if the given number is prime or not. If the number is prime number, then the result is store in the primeNum array. Otherwise move to the next number. This process continue till the end of the array.

Conclusion

Therefore, this is how we can find prime numbers from the array using two different methods. In the first method, we created a user-defined function and called that function in the main function. In the second example, we performed the function inside the main function.

Updated on: 17-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements