
- 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
Print all Prime Quadruplet of a number less than it in C++
In this problem, we are given a positive integer N, and we have to print all prime quadruplet less than or equal to n.
Prime quadruplets are the set of four prime numbers calculated as {p, p+2, p+6, p+8}. Example − 5 7 11 13.
Let’s take an example to understand the problem −
Input: N = 15 Output: 5 7 11 13.
To solve this problem, a simple approach is to generate all quadruplets of prime number p and check if all p, p+2, p+6, p+8 are prime numbers. This solution is easy but is more complex for the compiler.
Another efficient approach is to fond all prime numbers (using the sieve of Eratosthenes) and store them in an array up to a certain range. And traverse the array and check p, p+2, p+6, p+8 are prime or not and print if they all are prime.
Example
#include <bits/stdc++.h> using namespace std; #define MAX 100000 bool prime[MAX]; void primeNumberGenerator() { memset(prime, true, sizeof(prime)); for (int p = 2; p * p < MAX; p++) { if (prime[p] == true) { for (int i = p * 2; i < MAX; i += p) prime[i] = false; } } } void printPrimeQuadruplet(int n) { for (int i = 0; i < n - 7; i++) { if (prime[i] && prime[i + 2] && prime[i + 6] && prime[i + 8]) { cout<<i<<" "<<i+2<<" "<<i+6<<" "<<i+8<<endl; } } } int main() { primeNumberGenerator(); int n = 42; cout<<"All prime Quadruplets are :\n"; printPrimeQuadruplet(20); return 0; }
Output
All prime Quadruplets are −
5 7 11 13 11 13 17 19
- Related Articles
- 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++
- Nearest prime less than given number n C++
- Print a number strictly less than a given number such that all its digits are distinct in C++
- Java Program to display a prime number less than the given number
- Print all nodes less than a value x in a Min Heap in C++
- C Program for efficiently print all prime factors of a given number?
- Count pairs with sum as a prime number and less than n in C++
- Python Program for Efficient program to print all prime factors of a given number
- Find Largest Special Prime which is less than or equal to a given number in C++
- Print all numbers less than N with at-most 2 unique digits in C++
- Count all the numbers less than 10^6 whose minimum prime factor is N C++
- Java program to print a prime number
- Find all prime factors of a number - JavaScript
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only in C++

Advertisements