Open In App

R Program to Check Prime Number

A prime number is a positive number that is greater than 1 and whose factors are 1 and the number itself. The numbers that are not prime are called Composite Numbers. To identify a Prime Number we have to find its factor. In this article, we will discuss five different to find prime numbers and write code in the R Programming language.

Example of Prime Number: 2, 3, 5, 7, 11, 13, 17, 19.......
Note: 1 is a number which is neither prime nor composite number.

Concepts related to the topic

Example 1: General way to find prime number




Find_Prime_No <- function(n1) {
  if (n1 == 2) {
    return(TRUE)
  }
  if (n1 <= 1) {
    return(FALSE)
  }
  for (i in 2:(n1-1)) {
    if (n1 %% i == 0) {
      return(FALSE)
    }
  }
  return(TRUE)
}
 
numb_1 <- 13
 
if (Find_Prime_No(numb_1)) {
  # Using paste function to include the number in the output
  print(paste(numb_1, "is a prime number"))
} else {
  print("It is not a prime number")
}

Output:



[1] "13 is a prime number"

Example 2: Use ‘all’ Function to find prime number




# Find_Prime_No function to check if a number is prime number or not
Find_Prime_No <- function(n1) {
  #if n1 is less than equal to 1 than return FALSE
  if (n1 <= 1) {
    return(FALSE)
  }
  #return true if n1 is 2 because 2 is special case i.e only even prime number
  if (n1 == 2) {
    return(TRUE)
  }
  #div vector from 2 to square root of n or we can use (n1-1)
  div_vector <- 2:(n1-1)
  #it checks if the remainder is not equal to 0 for all elements in the div_vector and return TRUE
  return(all(n1 %% div_vector != 0))
}
 
# Define a number to check whether it is prime or not
numb_1 <- 39
 
# use if statement to check the number is prime using the 'all' function
if (Find_Prime_No(numb_1)) {
  # print "It is a prime number" if the condtion is true
  print(paste(numb_1," is a prime number"))
} else {
  #print "It is not a prime number" if the condtion is false
  print(paste(numb_1," is not a prime number"))
}

Output:

[1] "39  is not a prime number"

Example 3: Use ‘which’ Function to find prime number




#this function to check if a number is prime or not
Find_Prime_No <- function(n1) {
  #return true if n1 is 2 because 2 is special case i.e only even prime number
  if (n1 == 2) {
    return(TRUE)
  }
  #if n1 is less than equal to 1 than return FALSE
  if (n1 <= 1) {
    return(FALSE)
  }
  #div_vector from 2 to square root of n1
  div_vector <- 2:sqrt(n1)
  #which function is used to identify the positions where the n1 %% div == 0 is TRUE
  #length function calculate the number of elements in the result obtained from which
  #If the length is 0, it means that no divisors were found and return true
  return(length(which(n1 %% div_vector == 0)) == 0)
}
# Define a Prime_no to check whether it is prime or not
Prime_no <- 31
 
# use if statement to check the number is prime using the 'all' function
if (Find_Prime_No(Prime_no)) {
  # print "Yes it is a prime number" if the condtion is true
  print("Yes it is a prime number")
} else {
  #print "No it is not a prime number" if the condtion is false
  print("No it is not a prime number")
}

Output:



[1] "31  is a prime number"

Example 4: Find Prime Number Using Vectorized Operations




# This function to check if a number is prime or not
Find_Prime_No <- function(n1) {
  #return true if n1 is 2 because 2 is special case i.e only even prime number
  if (n1 == 2) {
    return(TRUE)
  }
  #if n1 is less than equal to 1 then return FALSE
  if (n1 <= 1) {
    return(FALSE)
  }
  #div_vector from 2 to (n1-1)
  div_vector <- 2:(n1-1)
  # n1 %% divisors finds the remainder, when n1 is divided by each element of the div_vector.
  #'sum' finds the sum of the boolean values resulting from the condition.
  #If the sum is 0,then there are no divisors other than 1 and number itself.
  return(sum(n1 %% div_vector == 0) == 0)
}
# Define a number to check whether it is prime or not
numb_1 <- 19
 
# use if statement to check the number is prime using the 'sum' function
if (Find_Prime_No(numb_1)) {
  # print "Yes it is a prime number" if the condtion is true
  print("Yes it is a prime number")
} else {
  #print "No it is not a prime number" if the condtion is false
  print("No it is not a prime number")
}

Output:

[1] "Yes it is a prime number"

Conclusion

These are the few different way in which we can find whether a number is prime or not.f you need to check for prime numbers in a performance-critical application or for a large range of numbers, it’s often better to use specialized libraries or algorithms that are optimized for prime number generation and testing. However, the basic prime checking algorithm you provided serves as a fundamental and educational example.


Article Tags :