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)



R program to find prime and composite numbers in an interval

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)

Similar Reads

Method 2

...

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

Step-by-step algorithm for implementing the approach:...

Contact Us