
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Largest Special Prime which is less than or equal to a given number in C++
Suppose we have a number n. We have to find the largest special prime which is less than or equal to N. The special prime is a number, which can be created by placing digits one after another, so all the resultant numbers are prime.
Here we will use Sieve Of Eratosthenes. We will create the sieve array up to the number n. Then start iteratively back from the number N, by checking if the number is prime. When this is prime, check whether this is special prime or not.
Example
#include<iostream> using namespace std; bool isSpecialPrime(bool sieve[], int num) { while (num) { if (!sieve[num]) { return false; } num /= 10; } return true; } void findSpecialPrime(int N) { bool sieve[N + 10]; for(int i = 0; i<N+10; i++){ sieve[i] = true; } sieve[0] = sieve[1] = false; for (long long i = 2; i <= N; i++) { if (sieve[i]) { for (long long j = i * i; j <= N; j += i) { sieve[j] = false; } } } while (true) { if (isSpecialPrime(sieve, N)) { cout << N << '\n'; break; } else N--; } } int main() { cout << "Special prime in range (2 -> 400): "; findSpecialPrime(400); cout << "Special prime in range (2 -> 100): "; findSpecialPrime(100); }
Output
Special prime in range (2 -> 400): 379 Special prime in range (2 -> 100): 79
- Related Articles
- Number of elements less than or equal to a given number in a given subarray in C++
- Nearest prime less than given number n C++
- How to get the largest integer less than or equal to a number in JavaScript?
- Print all prime numbers less than or equal to N in C++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Java Program to display a prime number less than the given number
- Check which element in a masked array is less than or equal to a given value in Numpy
- Largest number smaller than or equal to N divisible by K in C++
- Find all factorial numbers less than or equal to n in C++
- Maximum sum subarray having sum less than or equal to given sums in C++
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Find Multiples of 2 or 3 or 5 less than or equal to N in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Linear magnification produced by a concave mirror may be:(a) less than 1 or equal to 1 (b) more than 1 or equal to 1(c) less than 1, more than 1 or equal to 1 (d) less than 1 or more than 1

Advertisements