
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pierpont Prime in C++
In this problem, we are given a number n. Our task is to print all Pierpont prime numbers less than n.
Pierpont Prime number is a special type of prime number that is of the form,
p= 2i . 3k + 1.
Where p is a prime number, and i and k are some integers.
Let’s take an example to understand the problem,
Input − n = 50
Output − 2, 3, 5, 7, 13, 17, 19, 37
To solve this problem, we have to find all the prime numbers that follow the condition. For this, we will find a number with factors of powers of 2 and 3. And find all prime numbers. And print those numbers that are both, a prime number that follows the condition.
Example
Program to show an implementation of our solution,
#include <bits/stdc++.h> using namespace std; void printPierpontPrimes(int n){ bool arr[n+1]; memset(arr, false, sizeof arr); int two = 1, three = 1; while (two + 1 < n) { arr[two] = true; while (two * three + 1 < n) { arr[three] = true; arr[two * three] = true; three *= 3; } three = 1; two *= 2; } vector<int> primes; for (int i = 0; i < n; i++) if (arr[i]) primes.push_back(i + 1); memset(arr, false, sizeof arr); for (int p = 2; p * p < n; p++) { if (arr[p] == false) for (int i = p * 2; i< n; i += p) arr[i] = true; } for (int i = 0; i < primes.size(); i++) if (!arr[primes[i]]) cout<<primes[i]<<"\t"; } int main(){ int n = 50; cout<<"All Pierpont Prime Numbers less than "<<n<<" are :\n"; printPierpontPrimes(n); return 0; }
Output
All Pierpont Prime Numbers less than 50 are : 2 3 5 7 13 17 19 37
Advertisements