Open In App

R program to find prime and composite numbers in an interval

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A natural number (1, 2, 3, 4, 5 and so on) is called a prime number if it is greater than 1 and cannot be written as the product of two smaller natural numbers. The numbers greater than 1 that are not prime are called composite numbers.

A composite number is a positive integer that can be formed by multiplying two smaller positive integers. Equivalently, it is a positive integer that has at least one divisor other than 1 and itself.

Example:

Input: 2

Output: Prime

Explanation: it is divisible by only 2 so prime.

Input:

Output: Composite

Explanation: it is divisible by 2 and 4 so composite.

Input: 5

Output: Prime

Explanation: it is divisible by only 5 so prime.

Algorithm:

  • Initialize the range till where prime and composite numbers to be displayed.
  • Create a separate empty lists to store prime and composite numbers.
  • Since 1 is neither prime nor composite,
  • We start checking condition for prime from 2 as i.
  • Starting from 2 checks each and every digit that divides i exactly
  • If No number divides i except that i then number gets stored in prime number list,
  • Else gets stored in composite number list.
  • It executes till n(given by us) limit reaches.
  • Once it exits from loop, it prints both prime and composite numbers as separate list.

Example:

R




# R code for Finding composite  and prime numbers  upto 100
# initialize number n
n=100
 
# arranging sequence
x = seq(1, n)
 
# creating an empty place to store the numbers
prime_numbers=c()
 
composite_numbers = c()
for (i in seq(2, n)) {
  if (any(x == i)) {
 
    # prime numbers gets stored in a sequence order
    prime_numbers = c(prime_numbers, i)
    x = c(x[(x %% i) != 0], i)
  }
 
  else{
 
     # composite numbers gets stored in a sequence order
     composite_numbers = c(composite_numbers, i)
  }
}
 
# printing the series
print("prime_numbers")
print(prime_numbers)
 
print("composite_numbers")
print(composite_numbers)


Output:

[1] “prime_numbers”

 [1]  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

[1] “composite_numbers”

 [1]   4   6   8   9  10  12  14  15  16  18  20  21  22  24  25  26  27  28  30

[20]  32  33  34  35  36  38  39  40  42  44  45  46  48  49  50  51  52  54  55

[39]  56  57  58  60  62  63  64  65  66  68  69  70  72  74  75  76  77  78  80

[58]  81  82  84  85  86  87  88  90  91  92  93  94  95  96  98  99 100

The time complexity of this code is O(n^2) since there is a nested loop in the function. 

The auxiliary space used by this code is O(n)

Method 2

Step-by-step algorithm for implementing the approach:

Define a function is_prime(n) that takes an integer n as input and returns a boolean value indicating whether n is a prime number or not.
a. If n is less than or equal to 1, return False.
b. If n is less than or equal to 3, return True.
c. If n is divisible by 2 or 3, return False.
d. Initialize a variable i to 5.
e. While i * i is less than or equal to n, check if n is divisible by i or i+2. If it is, return False. Increment i by 6.
f. If none of the above conditions are met, return True.

Define a function find_primes_composites(start, end) that takes two integers start and end as input and returns a list containing two vectors: one with all the prime numbers between start and end, and the other with all the composite numbers between start and end.
a. Initialize two empty vectors primes and composites.
b. For each integer i from start to end, check if it is prime using the is_prime function. If it is, append it to the primes vector. Otherwise, append it to the composites vector.
c. Return a list containing the primes and composites vectors.

Use the find_primes_composites function to find the prime and composite numbers in an interval.

Print the prime and composite numbers using the returned list.

R




# Function to check if a number is prime
is_prime <- function(n) {
  if (n <= 1) {
    return(FALSE)
  } else if (n <= 3) {
    return(TRUE)
  } else if (n %% 2 == 0 || n %% 3 == 0) {
    return(FALSE)
  }
  i <- 5
  while (i * i <= n) {
    if (n %% i == 0 || n %% (i + 2) == 0) {
      return(FALSE)
    }
    i <- i + 6
  }
  return(TRUE)
}
 
# Function to find prime and composite numbers in an interval
find_primes_composites <- function(start, end) {
  primes <- c()
  composites <- c()
  for (i in start:end) {
    if (is_prime(i)) {
      primes <- c(primes, i)
    } else {
      composites <- c(composites, i)
    }
  }
  return(list(primes = primes, composites = composites))
}
 
# Example usage
result <- find_primes_composites(1, 20)
print(result$primes)      # 2 3 5 7 11 13 17 19
print(result$composites)  # 1 4 6 8 9 10 12 14 15 16 18 20


Output:

[1]  2  3  5  7 11 13 17 19
 [1]  1  4  6  8  9 10 12 14 15 16 18 20

Time complexity:

The time complexity of the is_prime function is O(sqrt(n)), since it only checks divisors up to the square root of n. The time complexity of the find_primes_composites function is O((end-start)*sqrt(end)), since it calls the is_prime function for each integer in the range from start to end. Therefore, the overall time complexity is O((end-start)*sqrt(end)).

Auxiliary space complexity:

The auxiliary space complexity of the is_prime function is O(1), since it only uses a fixed number of variables regardless of the input size. The auxiliary space complexity of the find_primes_composites function is O(end-start), since it stores the prime and composite numbers in vectors whose length depends on the input size. Therefore, the overall auxiliary space complexity is O(end-start).

Approach: a loop-based user input prime/composite number checker

Here are the steps for the approach used in the R program to find whether a given number is prime or composite:

  1. Define a function is_prime(n) that takes a positive integer n as input and returns TRUE if n is prime and FALSE otherwise.
    To determine whether n is prime, check if it is less than or equal to 1 and return FALSE if it is.
    Then, iterate over all integers i from 2 to the square root of n, and check if n is divisible by i. If n is divisible by i, return FALSE, since this means that n is composite.
    If no divisors are found, return TRUE, since n is prime.
  2. Use a loop to repeatedly prompt the user to enter a positive integer, until they enter a non-positive integer (e.g., 0).
  3. For each input number n, call the is_prime(n) function to determine whether it is prime or composite.
  4. Print out a message indicating whether n is prime or composite, using the cat() function.
  5. Repeat steps 2-4 until the user enters a non-positive integer.

R




is_prime <- function(n) {
  if (n <= 1) {
    return(FALSE)
  }
  for (i in 2:(sqrt(n))) {
    if (n %% i == 0) {
      return(FALSE)
    }
  }
  return(TRUE)
}
 
while (TRUE) {
  n <- as.integer(readline("Enter a number (enter 0 to exit): "))
  if (n == 0) {
    break
  }
  if (is_prime(n)) {
    cat(n, "is prime\n")
  } else {
    cat(n, "is composite\n")
  }
}


Enter a number (enter 0 to exit): 2
2 is prime
Enter a number (enter 0 to exit): 4
4 is composite
Enter a number (enter 0 to exit): 5
5 is prime
Enter a number (enter 0 to exit): 0

Time complexity: O(q * sqrt(n))

auxiliary space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads