VOOZH about

URL: https://www.geeksforgeeks.org/r-language/r-program-to-check-prime-number/

⇱ R Program to Check Prime Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

R Program to Check Prime Number

Last Updated : 23 Jul, 2025

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

  • Prime Number: A prime number is a positive number that is greater than 1 and is divisible by 1 and the number itself.
  • which( ): This function is used to find the index or positions of elements in an array or vector that satisfy the condition
  • all( ): This function checks if all elements in a vector satisfy some given condition or not. It returns TRUE if all elements are TRUE else FALSE
  • length( ): This function is used to find the number of elements or size of an array, or vector.
  • sum( ): This function adds all the numeric values in a vector.
  • seq(from, to, by): This function is used to generate sequences of numbers. Generating numbers from to using b

Example 1: General way to find prime number

Output:

[1] "13 is a prime number"
  • First we make a function which will check the whether a number is prime or not.
  • We apply if condtion to check for number 2. Since 2 is special case i.e only even prime number.
  • We use if condition to check whether num is less than equal to 1 and return Flase is condition satisfy.
  • Now apply for loop from2 to (number -1) and check if it is divisible by any number
  • if condition is true i.e. numb is havinf factor other than 1 and number itself so return false.
  • Now if it come outside for loop then it is prime number and return true.

Example 2: Use 'all' Function to find prime number

Output:

[1] "39 is not a prime number"
  • In this example, we define Find_Prime_No function to check if a number is prime or not
  • We use if condition to check whether num is less than equal to 1 and return 'FALSE' is condition satisfy.
  • We apply if condtion to check for number 2. Since 2 is special case i.e only even prime number.
  • Declare div_vector from 2 to (n1-1).
  • Now we use the all function it checks if the remainder is not equal to 0 when n is divided by all elements in the div_vector and return TRUE.
  • If this condition is true for all the element of div_vector then return 'TRUE' and print that it is a prime number else return 'FALSE'.

Example 3: Use 'which' Function to find prime number

Output:

[1] "31 is a prime number"
  • In this example, we define Find_Prime_No function to check if a number is prime or not.
  • We use if condition to check whether n1 is less than equal to 1 and return Flase is condition satisfy.
  • We apply if condtion to check for number 2. Since 2 is special case i.e only even prime number.
  • Declare div_vector from 2 to square root of n.
  • We use 'which' function to identify the positions where the n1 %% div_vector == 0 is true.
  • We use 'length' function to calculate the number of elements in the result obtained from 'which' function.
  • If the length is 0, it means that no divisors were found and return 'TRUE'.
  • If this condition is true for all the element of div_vector then return 'TRUE' and print that it is a prime number else return false.


Example 4: Find Prime Number Using Vectorized Operations

Output:

[1] "Yes it is a prime number"
  • In this example, Find_Prime_No function is used to check if a number is prime or not
  • We use if condition to check whether n1 is less than equal to 1 and return 'FALSE' is condition satisfy.
  • We apply if condtion to check for number 2. Since 2 is special case i.e only even prime number.
  • Declare div_vector from 2 to (n1-1).
  • n1 %% div_vector to 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
  • If this condition is true for all the element of div vector then return true and print that it is a prime number else return false.

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.

Comment
Article Tags:

Explore