The Wayback Machine - https://web.archive.org/web/20241008201617/https://www.geeksforgeeks.org/prime-numbers/
Open In App

Prime Numbers

Last Updated : 08 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prime numbers are those natural numbers divisible by only 1 and the number itself. Numbers with more than two factors are called composite numbers. All primes are odd numbers except for 2.

A total of 25 prime numbers are there between 1 and 100. These are: 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.

Mathematically prime numbers are defined as:

If p ∈ N, then p is prime if for any integers a and b, if p = a ⋅ b, then either a = 1 or b = 1
OR
p is prime ⟺ p > 1 and if p = a ⋅ b, then either a = 1 or b = 1.

prime-factors

In this article, we will discuss in detail about prime numbers, the list of prime numbers 1 to 100, the differences between prime and composite numbers, their examples, the prime numbers chart, etc.

There are 25 prime numbers between 1 to 100. The image added below shows the prime numbers from 1 to 100.

Prime-Numbers

Read More: Prime Number Chart

Odd and Even Prime Numbers

The only even prime number is 2. This is because it can be divided by 1 and 2, fulfilling the condition of having exactly two divisors. All prime numbers other than 2 are odd. This is because any even number greater than 2 is divisible by 2, which means it cannot be prime

Read More: Odd and Even Prime Numbers

Smallest and Largest Prime Number

The smallest prime number is 2. It is unique because it’s the only even prime number. There is no largest prime number, as prime numbers continue infinitely

Read More: Smallest and Largest Prime Number

Properties of Prime Numbers

Prime numbers have some properties which are listed as follows:

  • A Prime number is always a natural number i.e., a number greater than 1.
  • There are infinitely many prime numbers. This was proved by Euclid around 300 BC.
  • Every positive integer greater than 1 can be written as a product of prime numbers in a unique way, up to the order of the factors. This is known as the Fundamental Theorem of Arithmetic.

Prime Factorization

Prime factorization involves breaking down a composite number into a product of prime numbers. For example, the prime factorization of 60 is 22 x 3 x 5. This is useful for solving problems involving divisibility, greatest common divisors, and least common multiples.

Prime Factorization of Some Numbers

Prime factorization of some of the common composite number are:

Numbers Prime Factorization

Numbers

Prime Factorization

36 22 × 32

40

23 × 5

24 23 × 3

50

2 × 52

60 22 × 3 × 5

48

24 × 3

18 2 × 32

30

2 × 3 × 5

72 23 × 32

42

2 × 3 × 7

45 32 × 5

20

22 × 5

Prime Numbers vs Composite Numbers

There are a few differences between Prime and Composite numbers, which are as follows:

Prime Numbers

Composite Numbers

All Prime Numbers have only two factors such as 1 and the number itself. For example, 5 has only two factors 1 and 5. All Composites Numbers must have any factor other than 1 and the number itself. For example, 6 has 2 and 3 as its factors other than 1 and 6.
All Primes are Odd numbers except 2. Composites can be either odd or even.
Some of the first few primes are 2, 3, 5, 7, 11, 13, 17, etc. Some of the first few composites are 4, 6, 8, 9, 12, 14, 15, etc.

Fact about Prime Number-3

Coprime Numbers

Coprime numbers have just a single common factor, which is 1. This means that a pair of numbers is said to be coprime if their largest common factor is 1.

Note: All primes numbers are coprime to each other.

Read More: Co Prime Numbers

How to Find Prime Numbers?

Let’s learn some simple methods, which can help us easily identify that given number is a Prime or not.

1. Even Numbers are not Prime [Except 2]

All even numbers are NOT Prime, except for 2. This means if a number ends with 0, 2, 4, 6, or 8, it is not prime.

2. Divisibility Rules for Small Primes

For checking small prime numbers we can use these divisibility rules:

Divisibility by 3 – If sum of digits is divisible by 3, then the number can never be Prime.

Divisibility by 5 – If a number ends with 0 or 5, it is not Prime.

Divisibility by 7 – Double the last digit of given number then subtract it with rest of the digits, and check if the result is divisible by 7.
Example – N=203. Double the last digit [3×2=6] then subtract 6 with 20 [remaining two digits]. 20-6=14 which is divisible by 7. So, 203 is not Prime.

3. Division Method

One simple method is to divide number by all integers from 2 upto square root of given number.

Example –

Number = 29

Find square root of 29, which is 5.39. Now, we need to divide 29 by each whole number which is 2 to 5.

29 ÷ 2 = not a whole number,
29 ÷ 3 = not a whole number,
29 ÷ 4 = not a whole number,
29 ÷ 5 = not a whole number.

We can see non of these results are in whole number. So, 29 is a Prime Number.

This method is not suitable for very small primes [like 2, 3, 5, 7 etc]. It is better to use divisibility rules for such small number. Also, this method doesn’t work well for large numbers [say 100003] since the square root of large number itself might be a large number [316 in this case]. You might need to check divisibility of those many numbers.

4. Trial Division Method

Trial Division method checks prime by dividing the numbers by all possible divisors, to check if any divisor evenly divides without the remainder. This method is very similar to above discussed method. Divisor Method checks only upto square root of number, but Trial Division method continues dividing by all integers upto the number itself [except 1 and number itself].

Example –

Number = 17

Divide 17 by 2 -> doesn’t divide evenly (not divisible by 2).
Divide 17 by 3 -> doesn’t divide evenly.

–> We can skip checking for all even numbers. Since it is not divisible by 2, it can’t be divisible by any higher even numbers.

Divide 17 by 5 -> doesn’t divide evenly.

We can continue checking with all odd numbers till 15 [7, 9, 11, etc.]. Since none of these divide 17 evenly, it’s clear that 17 is prime.

This method is time consuming as well for large numbers. If N is 100000, then you will have to check all divisors upto N-1. Hence, this method is useful for small number only.

5. Factorization Method

In this method, we break down the numbers into prime factors. If the number has a product of two smaller numbers (other than 1 and itself), then it is composite. Otherwise, it is Prime.

Examples –

Number = 17

Divide 17 by 2 -> Not divisible, leaves a remainder
Divide 17 by 3 -> Not divisible, leaves a remainder
Divide 17 by 5 -> Not divisible, leaves a remainder
We can keep on checking. There is no smaller number which divide 17 evenly. So, 17 is Prime.

Number = 35

Divide 35 by 2 -> Not divisible, leaves a remainder
Divide 35 by 3 -> Not divisible, leaves a remainder
Divide 35 by 3 -> Not divisible, leaves a remainder
Divide 35 by 7 -> It’s divisible evenly. 5 x 7. So, 35 is Not Prime.

6. Prime Numbers in the Form 6n ± 1

Any prime number (greater than 3) can be written in the form of 6n + 1 or 6n – 1, where n is a whole number.

For example:

  • The number 19 can be written as 6(3) + 1 = 19, making it a prime number.
  • The number 23 can be expressed as 6(4) – 1 = 23, confirming it is also prime.

Exceptions:

This rule helps identify possible prime numbers, but not all numbers of the form 6n+1 or 6n−1 are prime, so further checking is needed.

25 = 6(4) +1 is not prime (it is 52).
35 = 6(6) – 1 is not prime (it is 5 × 7).

7. For Prime Numbers greater than 40

To know the prime numbers greater than 40, this formula can be used n2 + n + 41, where n = 0, 1, 2, ….., 39
For Example:
(0)2 + 0 + 0 = 41
(1)2 + 1 + 41 = 43
(2)2 + 2 + 41 = 47

Note –

For n>39, this formula can give composite numbers which is non-prime. So, we need to further verify to confirm if the result is correct or not.
Example – (41)2 + 41 + 41 = 1763. It is divisible by 41 and 43, so 1763 is not Prime.

Let’s see some advanced methods which works faster for checking Prime number.

Fastest Way to Find a Prime Number: AKS Primality Test

The AKS primality test is one of the fastest deterministic methods for determining if a number is prime. It checks whether a number is prime by using polynomial congruence and works for all integers. The algorithm has a running time of approximately O((log n)6), which makes it efficient even for large numbers. Unlike other primality tests that may only work for specific cases or rely on randomness, the AKS test guarantees an answer in a finite and predictable time.

Also Read: AKS Primality Test

Another ancient method to filter prime and composite number is “Sieve of Eratosthenes”. Let’s discuss this in detail.

Sieve of Eratosthenes

Sieve of Eratosthenes is a classical algorithm in number theory, designed to efficiently identify all prime numbers up to a given limit, n. Named after the ancient Greek mathematician Eratosthenes of Cyrene, this method systematically filters out composite numbers, leaving only prime numbers, which are the natural numbers greater than 1 that have no positive divisors other than 1 and themselves.

The algorithm operates by iterating through numbers starting from 2, the smallest prime. It marks the multiples of each prime as composite, ensuring that no prime number is mistakenly eliminated. This process is repeated for every unmarked number, which is then recognized as prime, and its multiples are subsequently marked as composite. The efficiency of the Sieve of Eratosthenes stems from the fact that once a number is determined to be prime, all of its multiples can be skipped, significantly reducing the number of operations required.

Steps for Algorithm

Here’s how the algorithm works:

  1. Create a list of consecutive integers from 2 through n (the upper limit).
  2. Start with the first number, 2. This is a prime number.
  3. Mark all multiples of 2 as composite (i.e., not prime).
  4. Move to the next number that is unmarked. This number is prime.
  5. Repeat the process for all numbers less than or equal to √n.
  6. The remaining unmarked numbers are primes.

The-Sieve-of-Eratosthenes

Applications of Prime Numbers in Real Life

There are so many real life examples of prime numbers in real life. These uses and applications are mentioned below:

  • Cryptography
  • RSA Encryption
  • Internet Security
  • Digital Signatures
  • Credit Card Transactions
  • Image Compression
  • Signal Processing
  • Error Detection and Correction

Read More: Applications of Prime Numbers in Real Life

Some of the important theorems related to prime numbers are:

  • Euclid Division Lemma
  • Wilson Theorem
  • Fermat’s Little Theorem

Euclid Division Lemma

Euclid’s Division Lemma gives the relation between the various components of Division. It explains that for any two positive integers say a and b there exist two unique integers q and r such that a = bq + r where q the quotient of the division, and r is the remainder of the division.

Read in Detail: Euclid’s Division Lemma

Wilson Theorem

Wilson Theorem states that if p is any natural number greater than 1, then p is said to be a prime number if and only if the product of all the positive integers less than p is one less than a multiple of number p.

Read in Detail: Wilson’s Theorem

Fermat’s Little Theorem

According to Fermat’s Little theorem, if p is a prime number and a is a positive integer not divisible by p, then there exists a positive integer k such that ak – 1 is divisible by p.

Read in Detail: Fermat’s Little Theorem

Importance of Prime Numbers

Prime Numbers hold a significant place in the evolution of mathematics, primarily for their crucial role in improving cryptography and security standards in the modern world. Despite years of research, Prime Numbers continue to be an active topic in the field of mathematical research, fascinating scientists and scholars with their elegant properties and wide range of applications.

  • Prime numbers are the basic building blocks of natural numbers. Every integer greater than 1 can be uniquely expressed as a product of primes, known as the Fundamental Theorem of Arithmetic. This uniqueness is crucial for number theory.
  • Prime numbers play a vital role in modern cryptography, particularly in algorithms like RSA. They are used to create keys for secure data transmission, ensuring that sensitive information remains private.
  • Primes are central to many important theorems and conjectures in mathematics, such as the Goldbach Conjecture and the Prime Number Theorem, which describes the distribution of prime numbers among the integers.
  • In computer algorithms, prime numbers are used in hashing functions and data structures, helping to optimize performance and reduce collisions in hash tables.
  • Studying prime numbers contributes to the understanding of randomness and patterns in mathematics. Their distribution can reveal insights into more complex mathematical structures and theories.

Prime Numbers Examples

Example 1: Determine whether 37 is a prime number or not.

Solution: 

As a prime number is a positive integer greater than 1 that has no positive integer divisors other than 1 and itself.
As 37 = 1 × 37, 
Therefore, 37 has no divisors other than 1 and itself, and it is a prime number.
So, 37 is a prime number.

Example 2: Find all the prime numbers between 20 and 40.

Solution: 

The prime numbers between 20 and 40 are 23, 29, and 37.

Example 3: Is 19 a Prime Number?

Solution:

Let us write the given number in the form of 6n ± 1.
6(3) + 1 = 18 + 1 = 19
Therefore, 19 is a prime number

Example 4: Which is the greatest prime number between 1 to 10?

Solution:

There are 4 prime numbers between 1 and 10 and the greatest prime number between 1 and 10 is 7.

Examples 5: Why is 20 not a prime number?

Solution:

The factors of 20 are 1, 2, 4, 5, 10, and 20. Thus, 20 has more than two factors. Since the number of factors of 20 is more than two numbers, it is NOT a prime number.

Also Read:

Practice Questions on Prime Numbers

1. Find the first 5 prime numbers.

2. Determine whether the following numbers are prime or not: 17, 25, 29, 37, 41.

3. Calculate the product of the first 3 prime numbers.

4. What is the sum of the first 10 prime numbers?

5. Find the prime factorization of the number 56.

6. Is 97 a prime number? Explain why or why not.

7. What is the List all prime numbers between 50 and 100.

8. What is the prime factorization of 84?

9. Find the sum of the prime numbers between 10 and 30.

10. Identify the twin primes between 40 and 60 (twin primes are pairs of primes that differ by 2).

Answers

  1. 2, 3, 5, 7, 11
  2. Prime: 17, 29, 37, 41
    Not Prime: 25
  3. 30 (2 × 3 × 5)
  4. 129
  5. 23 × 7
  6. Yes, 97 is prime.
  7. 53, 59, 61, 67, 71, 73, 79, 83, 89
  8. 22 × 3 × 7
  9. 60 (11 + 13 + 17 + 19)
  10. 41 and 43, 59 and 61 (twin primes)

Articles Related to Prime Numbers

Real Numbers

Imaginary Numbers

Rational Number

Irrational Numbers

Complex Numbers

Triangular Numbers

Conclusion

Prime numbers are fundamental building blocks in mathematics, with applications from basic arithmetic to advanced theories and applications in cryptography, computer science, and number theory. Understanding prime numbers helps us learn about the simplicity and complexity of numbers, as they are the indivisible elements that shape various mathematical structures. By exploring the properties of prime numbers, we gain deeper insights into the nature of numbers and their crucial role in both theoretical and practical domains of mathematics.

Also Check: Prime Numbers Worksheet

Prime Numbers – FAQs

What are prime numbers?

Prime numbers are positive integers greater than 1 that has no positive integer divisors other than 1 and itself.

What prime numbers are even?

There is only one such prime number which is even i.e. 2. Therefore 2 is called only even prime.

Is 1 a prime number?

1 is neither prime nor composite number.

What are twin prime numbers?

Twin prime are the pair of primes which has difference of 2(when smaller is subtracted from greater). For example 5 and 7 as 7-5=2.
Some more examples of twin primes are 3 and 5 (5-3=2), 11 and 13, 17 and 19, 29 and 31, 41 and 43, etc.

What are the first 10 prime numbers?

First ten primes are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.

What are the prime numbers between 70 and 90?

The prime numbers between 70 and 90 are 71, 73, 79, 83, and 89

What are twisted prime numbers?

A pair of number is called Twisted Primes if both numbers are palindrome as well as prime themselves. For example, 17 and 71 are Twisted Prime Pair as both are palindrome (reversing the digits of 17 number become 71 and same for the 71) as well as prime themselves.  

Why is 2 a prime number?

2 is considered a prime number because it meets the fundamental definition of a prime number: it is a positive integer greater than 1 that has no positive integer divisors other than 1 and itself. In the case of 2, it can only be divided by 1 and 2 without leaving a remainder.

What is a prime number for kids?

A prime number is a special kind of whole number greater than 1 that has only two factors: 1 and the number itself. In other words, it cannot be evenly divided by any other numbers except 1 and the number itself. Examples of prime numbers include 2, 3, 5, 7, and 11.

Is 1 composite or prime?

No, 1 is neither composite nor prime. Prime numbers have exactly two distinct positive divisors (1 and the number itself), and composite numbers have more than two divisors. Since 1 only has one distinct positive divisor (itself), it doesn’t fit the definition of a prime or composite number.

What is the fastest way to find a prime number?

The fastest way to find a prime number depends on the context. However, using a method like the Sieve of Eratosthenes is efficient for finding smaller prime numbers. For larger primes, probabilistic algorithms like the Miller-Rabin test or deterministic ones like the AKS primality test are commonly used.

Which is the largest known prime number?

The largest known prime number is a Mersenne prime of the form 282,589,933−12^{82,589,933} – 1282,589,933−1. It was discovered in December 2018 and contains over 24 million digits.

What is the lowest prime number?

The smallest prime number is 2.



Previous Article
Next Article

Similar Reads

Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between product of non-prime numbers and prime numbers.Note: 1 is neither prime nor non-prime.Examples: Input : arr[] = {1, 3, 5, 10, 15, 7} Output : 45 Explanation : Product of non-primes = 150 Product of primes = 105 Input : arr[] = {3, 4, 6, 7} Output : 3 Naive
15 min read
Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
Given an array arr[] of N positive integers, the task is to calculate the absolute difference between XOR of non-prime numbers and prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 4 Xor of non-primes = 10 ^ 15 = 5 Xor of primes = 3 ^ 5 ^ 7 = 1 |5 - 1| = 4Input: arr[] = {3, 4, 6, 7} Outp
8 min read
Count prime numbers up to N that can be represented as a sum of two prime numbers
Given a positive integer N, the task is to find the count of prime numbers less than or equal to N that can be represented as a sum of two prime numbers. Examples: Input: N = 6Output: 1Explanation:5 is the only prime number over the range [1, 6] that can be represented as sum of 2 prime numbers i.e., 5 = 2 + 3, where 2, 3 and 5 are all primes.There
15+ min read
Count prime numbers that can be expressed as sum of consecutive prime numbers
Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes. Examples: Input: N = 45Output: 3Explanation:Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers: 5 = 2 + 317 = 2 + 3 + 5 + 741 = 2 + 3 + 5 + 7 + 11 + 13Therefore, the count is 3
8 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Prime numbers after prime P with sum S
Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.Examples : Input : N = 2, P = 7, S = 28 Output : 11 17 Explanation : 11 and 17 are primes after prime 7 and (11 + 17 = 28) Input : N = 3, P = 2, S = 23 Output : 3 7 13 5 7 11 Explanation : 3, 5, 7, 11 and 13 are primes after prime 2.
13 min read
Print the nearest prime number formed by adding prime numbers to N
Given a number N. The task is to print the nearest prime if the number is not prime by making it prime by adding prime numbers sequentially from 2. Examples: Input: N = 8 Output: 13 8 is not prime, so add the first prime to it to get 10 10 is not prime, hence add the second prime, i.e., 3 to get 13 which is prime. Input: N = 45 Output: 47 Naive App
11 min read
Check if a prime number can be expressed as sum of two Prime Numbers
Given a prime number N. The task is to check if it is possible to express N as the sum of two separate prime numbers.Note: The range of N is less than 108. Examples: Input: N = 13Output: YesExplanation: The number 13 can be written as 11 + 2, here 11 and 2 are both prime.Input: N = 11Output: NoRecommended: Please try your approach on {IDE} first, b
13 min read
Print prime numbers with prime sum of digits in an array
Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[] = {2, 3, 19, 13, 25, 7} Output: 2, 3, 7 A simple
8 min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Sum of prime numbers without odd prime digits
Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosthenes to store all prime numbers.Next check for eac
8 min read
Permutation of first N positive integers such that prime numbers are at prime indices
Given an integer N, the task is to find the number of permutations of first N positive integers such that prime numbers are at prime indices (for 1-based indexing).Note: Since, the number of ways may be very large, return the answer modulo 109 + 7. Examples: Input: N = 3 Output: 2 Explanation: Possible permutation of first 3 positive integers, such
6 min read
Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
Given an integer N, the task is to find the number of permutations of first N positive integers such that prime numbers are at prime indices (for 1-based indexing).Note: Since, the number of ways may be very large, return the answer modulo 109 + 7.Examples: Input: N = 3 Output: 2 Explanation: Possible permutation of first 3 positive integers, such
6 min read
Count numbers in a given range whose count of prime factors is a Prime Number
Given a 2D array Q[][] of size N * 2 representing queries of the form {L, R}. For each query, the task is to print the count of numbers in the range [L, R] with a count of prime factors equal to a prime number. Examples: Input: Q[][] = {{4, 8}, {30, 32}} Output: 3 2 Explanation: Query 1: Prime factors of 4 = {2, 2} and count of prime factors = 2 Pr
15 min read
Count of unordered pairs of semi-prime numbers with prime sum in range [1, N]
Given a positive integer N, the task is to find the number of unordered pairs of semi-prime numbers in the range [1, N] such that their sum is prime. Examples: Input: N = 25Output: 5Explanation:The valid pairs of semi-prime numbers whose sum is also prime are (10, 21), (14, 15), (15, 22), (20, 21), and (21, 22). The count of such numbers is 5. Inpu
8 min read
Count prime numbers in range [L, R] whose single digit sum is also prime
Given two integers L and R. The task is to count the prime numbers in the range [L, R], whose single sum is also a prime number. A single sum is obtained by adding the digits of a number until a single digit is left. Examples Input: L = 5, R = 20 Output: 3Explanation: Prime numbers in the range L = 5 to R = 20 are {5, 7, 11, 13, 17, 19}Their "singl
13 min read
Check prime numbers in Array against prime index positions
Given an array arr[], the task is to check for each prime number in the array, from its position(indexing from 1) to each non-prime position index, and check if the number present in that position is prime or not. Examples: Input: arr[] = {5, 9, 3, 7, 8, 2}Output: PrimeExplanation: First prime number found is 5. For 5, two non-prime positions are 4
11 min read
Numbers less than N which are product of exactly two distinct prime numbers
Given a number [Tex]N [/Tex]. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers. For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.Note: These numbers cannot be a perfect square.Examples: Input : N
8 min read
Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
Given an array of positive numbers, the task is to find the bitwise AND of the sum of non-prime numbers and the sum of prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 9 Sum of non-primes = 10 + 15 = 25 Sum of primes = 3 + 5 + 7 = 15 25 & 15 = 9Input: arr[] = {3, 4, 6, 7} Output: 10
9 min read
Count of numbers upto M divisible by given Prime Numbers
Given an array arr[] of Prime Numbers and a number M, the task is to count the number of elements in the range [1, M] that are divisible by any of the given prime numbers. Examples: Input: arr[] = {2, 3, 5, 7} M = 100 Output: 78 Explanation:In total there are 78 numbers that are divisible by either of 2 3 5 or 7. Input: arr[] = {2, 5, 7, 11} M = 20
5 min read
Count numbers up to N that can be expressed as powers of Prime Numbers
Given an integer N, the task is to count numbers from the range [1, N] which are the power of prime numbers. Examples: Input: N = 6Output: 3Explanation:Numbers from the range [1, 6] that can be expressed as powers of prime numbers are:2 = 213 = 314 = 225 = 51 Input: N = 9Output: 7Explanation:Numbers from the range [1, 9] that can be expressed as po
9 min read
Bitwise XOR of first N natural numbers that are product of two distinct Prime Numbers
Given a positive integer N, the task is to calculate the Bitwise XOR of first N numbers which are a product of exactly two distinct prime numbers. Examples: Input: N = 20Output: 7Explanation: The numbers from the range [1, 20] which are a product of exactly two distinct prime numbers are {6, 10, 12, 14, 15, 18, 20}.Bitwise XOR of these numbers = 6
8 min read
Can Decimal Numbers be Prime Numbers?
Can Decimal Numbers be Prime Numbers?Answer: No, decimal numbers are not considered prime because prime numbers are defined for natural numbers greater than 1 with exactly two distinct positive divisors, and decimals do not meet this criterion. What Are Prime Numbers?Prime numbers are a special category of numbers that have exactly two distinct pos
3 min read
Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times
Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Output: Difference = 631, Count of operations = 6Expl
14 min read
Check whether the sum of prime elements of the array is prime or not
Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not. Examples: Input: arr[] = {1, 2, 3} Output: Yes As there are two primes in the array i.e. 2 and 3. So, the sum of prime is 2 + 3 = 5 and 5 is also prime. Input: arr[] = {2, 3, 2, 2} Output: No Approach: First find prime number up to 10^5
10 min read
Count prime pairs whose difference is also a Prime Number
Given an integer N, the task is to count the number of pairs of prime numbers in the range [1, N] such that the difference between elements of each pair is also a prime number. Examples: Input: N = 5 Output: 2 Explanations: Pair of prime numbers in the range [1, 5] whose difference between elements is also a prime number are: (2, 5) = 3 (Prime numb
9 min read
Count distinct prime triplets up to N such that sum of two primes is equal to the third prime
Given an integer N, the task is to count the number of distinct prime triplets (a, b, c) from the range [1, N] such that a < b < c ? N and a + b = c.Note: Two prime tuples are distinct if at least one of the primes present in them are different. Examples: Input: N = 6Output: 1Explanation: Among numbers in the range [1, 6], the only prime trip
6 min read
Cpp14 Program to Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times
Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Output: Difference = 631, Count of operations = 6Expl
5 min read
Minimize swaps required to make all prime-indexed elements as prime
Given an array arr[] of size N. The task is to find the minimum number of swaps required to re-arrange the array such that all prime-indexed elements are prime, If the task can't be achieved, print "-1" Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}Output: 0Explanation: All the prime indices {2, 3, 5} (one-based indexing) have prime elements prese
9 min read
Practice Tags :