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.
Input: N = 8
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
Approach Using Sieve of Eratosthenes, mark the prime index by 1 in isprime list and store all the prime numbers in a list prime. Keep adding prime numbers sequentially to N, till it becomes prime.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Nearest prime less than given number n
- Nearest prime number in the array of every array element
- Nearest element with at-least one common prime factor
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Print prime numbers with prime sum of digits in an array
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- K-Nearest Neighbours
- Implementation of K Nearest Neighbors
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Maximize length of increasing subsequence possible by replacing array element by nearest primes
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Minimize the sum of squares of sum of N/2 paired formed by N numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.