# 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:4

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