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
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:
C++
// C++ program to print the // nearest prime number by // sequentially adding the // prime numebers #include<bits/stdc++.h> using namespace std; // Function to store prime // numbers using prime seive void prime_seive(int MAX, vector<int> &isprime;, vector<int> ′) { // iterate for all // the numbers int i = 2; while (i * i <= MAX) { // If prime[p] is not changed, // then it is a prime if (isprime[i] == 1) { // append the prime // to the list prime.push_back(i); // Update all multiples of p for (int j = i * 2; j < MAX; j += i) { isprime[j] = 0; } } i += 1; } } // Function to print // the nearest prime int printNearest(int N) { int MAX = 1e6; // store all the // index with 1 vector<int> isprime(MAX, 1); // 0 and 1 are not prime isprime[0] = isprime[1] = 0; // list to store // prime numbers vector<int> prime; // variable to // add primes int i = 0; // call the seive function prime_seive(MAX, isprime, prime); // Keep on adding prime // numbers till the nearest // prime number is acheived while (!isprime[N]) { N += prime[i]; i += 1; } // return the // nearest prime return N ; } // Driver Code int main() { int N = 8; printf("%d", printNearest(N)); return 0; } // This code is contributed // by Harshit Saini |
Java
// Java program to print the // nearest prime number by // sequentially adding the // prime numebers import java.util.*; class GFG { // Function to store prime // numbers using prime seive static void prime_seive(int MAX, int []isprime, Vector<Integer> prime) { // iterate for all // the numbers int i = 2; while (i * i <= MAX) { // If prime[p] is not changed, // then it is a prime if (isprime[i] == 1) { // append the prime // to the list prime.add(i); // Update all multiples of p for (int j = i * 2; j < MAX; j += i) { isprime[j] = 0; } } i += 1; } } // Function to print // the nearest prime static int printNearest(int N) { int MAX = (int) 1e6; // store all the // index with 1 except 0,1 index int [] isprime = new int[MAX]; for(int i = 2; i < MAX; i++) isprime[i] = 1; // list to store // prime numbers Vector<Integer> prime = new Vector<Integer>(); // variable to add primes int i = 0; // call the seive function prime_seive(MAX, isprime, prime); // Keep on adding prime // numbers till the nearest // prime number is acheived while (isprime[N] == 0) { N += prime.get(i); i += 1; } // return the // nearest prime return N ; } // Driver Code public static void main(String[] args) { int N = 8; System.out.printf("%d", printNearest(N)); } } // This code is contributed by Rajput-Ji |
Python3
# Python program to print the nearest prime # number by sequentially adding the prime numebers # Function to store prime numbers using prime seive def prime_seive(MAX, isprime, prime): # iterate for all the numbers i = 2 while (i * i <= MAX): # If prime[p] is not changed, # then it is a prime if (isprime[i] == 1): # append the prime to the list prime.append(i) # Update all multiples of p for j in range(i * 2, MAX, i): isprime[j] = 0 i += 1 # Function to print the nearest prime def printNearest(N): MAX = 10**6 # store all the index with 1 isprime = [1] * MAX # 0 and 1 are not prime isprime[0] = isprime[1] = 0 # list to store prime numbers prime = [] # variable to add primes i = 0 # call the seive function prime_seive(MAX, isprime, prime) # Keep on adding prime numbers # till the nearest prime number # is acheived while not isprime[N]: N += prime[i] i += 1 # return the nearest prime return N # Driver Code N = 8print(printNearest(N)) |
C#
// C# program to print the
// nearest prime number by
// sequentially adding the
// prime numebers
using System;
using System.Collections.Generic;
class GFG
{
// Function to store prime
// numbers using prime seive
static void prime_seive(int MAX, int []isprime,
List
{
// iterate for all the numbers
int i = 2;
while (i * i <= MAX)
{
// If prime[p] is not changed,
// then it is a prime
if (isprime[i] == 1)
{
// append the prime to the list
prime.Add(i);
// Update all multiples of p
for (int j = i * 2;
j < MAX; j += i)
{
isprime[j] = 0;
}
}
i += 1;
}
}
// Function to print
// the nearest prime
static int printNearest(int N)
{
int MAX = (int) 1e6;
int i = 0;
// store all the
// index with 1 except 0,1 index
int [] isprime = new int[MAX];
for(i = 2; i < MAX; i++)
isprime[i] = 1;
// list to store
// prime numbers
List
// variable to add primes
i = 0;
// call the seive function
prime_seive(MAX, isprime, prime);
// Keep on adding prime
// numbers till the nearest
// prime number is acheived
while (isprime[N] == 0)
{
N += prime[i];
i += 1;
}
// return the
// nearest prime
return N;
}
// Driver Code
public static void Main(String[] args)
{
int N = 8;
Console.Write(“{0}”, printNearest(N));
}
}
// This code is contributed by Princi Singh
13
Recommended Posts:
- Print prime numbers with prime sum of digits in an array
- Nearest prime less than given number n
- Check if a prime number can be expressed as sum of two Prime Numbers
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print prime numbers in a given range using C++ STL
- Print all prime numbers less than or equal to N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Print all Semi-Prime Numbers less than or equal to N
- Print prime numbers from 1 to N in reverse order
- Python program to print all Prime numbers in an Interval
- Nearest element with at-least one common prime factor
- Count occurrences of a prime number in the prime factorization of every element from the given range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



