Open In App

Print all Proth primes up to N

Last Updated : 16 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number N, the task is to check whether the given number is Proth Prime or not. 
A Proth prime is a Proth Number which is prime.
The first few Proth primes are – 
 

3, 5, 13, 17, 41, 97, 113, 193, 241, 257, 353, 449, 577, 641, 673, 769, 929, 1153, 1217, …..

Examples: 

Input: 41
Output: 41 is Proth Prime

Input: 19
Output: 19 is not a Proth Prime

 

Approach: 
The idea is to find primes upto N using Sieve of Eratosthenes. Then check whether the given number is Proth Number or not. If number is a Proth Number and is also a prime number, then given number is Proth Prime.

Below is the implementation of the above algorithm:

C++




// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
int prime[1000000];
 
// Calculate all primes upto n.
void SieveOfEratosthenes(int n)
{
    // Initialize all entries it as true.
    // A value in prime[i] will finally
    // false if i is Not a prime, else true.
    for (int i = 1; i <= n + 1; i++)
        prime[i] = true;
 
    prime[1] = false;
 
    for (int p = 2; p * p <= n; p++) {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true) {
 
            // Update all multiples of p
            // greater than or equal to
            // the square of it numbers
            // which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
 
// Utility function to check power of two
bool isPowerOfTwo(int n)
{
    return (n && !(n & (n - 1)));
}
 
// Function to check if the Given
// number is Proth number or not
bool isProthNumber(int n)
{
 
    int k = 1;
    while (k < (n / k)) {
 
        // check if k divides n or not
        if (n % k == 0) {
 
            // Check if n/k is power of 2 or not
            if (isPowerOfTwo(n / k))
                return true;
        }
 
        // update k to next odd number
        k = k + 2;
    }
 
    // If we reach here means there
    // exists no value of K such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
 
// Function to check whether the given
// number is Proth Prime or Not.
bool isProthPrime(int n)
{
    // Check n for Proth Number
    if (isProthNumber(n - 1)) {
 
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
 
// Driver Code
int main()
{
    int n = 41;
 
    // if number is proth number,
    // calculate primes upto n
    SieveOfEratosthenes(n);
 
    for (int i = 1; i <= n; i++)
        // Check n for Proth Prime
        if (isProthPrime(i))
            cout << i << endl;
 
    return 0;
}


Java




// Java implementation of the above approach
import java.util.*;
 
class GFG
{
 
static boolean[] prime = new boolean[1000000];
 
// Calculate all primes upto n.
static void SieveOfEratosthenes(int n)
{
    // Initialize all entries it as true.
    // A value in prime[i] will finally
    // false if i is Not a prime, else true.
    for (int i = 1; i <= n + 1; i++)
        prime[i] = true;
 
    prime[1] = false;
 
    for (int p = 2; p * p <= n; p++)
    {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true)
        {
 
            // Update all multiples of p
            // greater than or equal to
            // the square of it numbers
            // which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
 
// Utility function to check power of two
static boolean isPowerOfTwo(int n)
{
    return (n > 0 && (n & (n - 1)) == 0);
}
 
// Function to check if the Given
// number is Proth number or not
static boolean isProthNumber(int n)
{
 
    int k = 1;
    while (k < (int)(n / k))
    {
 
        // check if k divides n or not
        if (n % k == 0)
        {
 
            // Check if n/k is power of 2 or not
            if (isPowerOfTwo((int)(n / k)))
                return true;
        }
 
        // update k to next odd number
        k = k + 2;
    }
 
    // If we reach here means there
    // exists no value of K such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
 
// Function to check whether the given
// number is Proth Prime or Not.
static boolean isProthPrime(int n)
{
    // Check n for Proth Number
    if (isProthNumber(n - 1))
    {
 
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 41;
 
    // if number is proth number,
    // calculate primes upto n
    SieveOfEratosthenes(n);
 
    for (int i = 1; i <= n; i++)
        // Check n for Proth Prime
        if (isProthPrime(i))
            System.out.println(i);
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3




# Python3 implementation of the
# above approach
import math as mt
 
prime = [0 for i in range(1000000)]
 
# Calculate all primes upto n.
def SieveOfEratosthenes(n):
     
    # Initialize all entries it as true.
    # A value in prime[i] will finally
    # false if i is Not a prime, else true.
    for i in range(1, n + 2):
        prime[i] = True
 
    prime[1] = False
 
    for p in range(2, mt.ceil(n**(0.5))):
 
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p] == True):
 
            # Update all multiples of p
            # greater than or equal to
            # the square of it numbers
            # which are multiple of p and are
            # less than p^2 are already been marked.
            for i in range(p * p, n + 1, p):
                prime[i] = False
 
# Utility function to check power of two
def isPowerOfTwo(n):
    return (n and (n & (n - 1)) == False)
 
# Function to check if the Given
# number is Proth number or not
def isProthNumber(n):
     
    k = 1
    while (k < (n // k)):
 
        # check if k divides n or not
        if (n % k == 0):
 
            # Check if n/k is power of 2 or not
            if (isPowerOfTwo(n // k)):
                return True
         
        # update k to next odd number
        k = k + 2
     
    # If we reach here means there
    # exists no value of K such
    # that k is odd number and n/k
    # is a power of 2 greater than k
    return False
 
# Function to check whether the given
# number is Proth Prime or Not.
def isProthPrime(n):
 
    # Check n for Proth Number
    if (isProthNumber(n - 1)):
 
        # if number is prime, return true
        if (prime[n]):
            return True
        else:
            return False
     
    else:
        return False
 
# Driver Code
n = 41
 
# if number is proth number,
# calculate primes upto n
SieveOfEratosthenes(n)
 
for i in range(1, n + 1):
     
    # Check n for Proth Prime
    if isProthPrime(i) == True:
        print(i)
         
# This code is contributed by
# Mohit kumar 29


C#




// C# implementation of the above approach
using System;
 
class GFG
{
 
static Boolean[] prime = new Boolean[1000000];
 
// Calculate all primes upto n.
static void SieveOfEratosthenes(int n)
{
    // Initialize all entries it as true.
    // A value in prime[i] will finally
    // false if i is Not a prime, else true.
    for (int i = 1; i <= n + 1; i++)
        prime[i] = true;
 
    prime[1] = false;
 
    for (int p = 2; p * p <= n; p++)
    {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true)
        {
 
            // Update all multiples of p
            // greater than or equal to
            // the square of it numbers
            // which are multiple of p and are
            // less than p^2 are already been marked.
            for (int i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
 
// Utility function to check power of two
static Boolean isPowerOfTwo(int n)
{
    return (n > 0 && (n & (n - 1)) == 0);
}
 
// Function to check if the Given
// number is Proth number or not
static Boolean isProthNumber(int n)
{
 
    int k = 1;
    while (k < (int)(n / k))
    {
 
        // check if k divides n or not
        if (n % k == 0)
        {
 
            // Check if n/k is power of 2 or not
            if (isPowerOfTwo((int)(n / k)))
                return true;
        }
 
        // update k to next odd number
        k = k + 2;
    }
 
    // If we reach here means there
    // exists no value of K such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
 
// Function to check whether the given
// number is Proth Prime or Not.
static Boolean isProthPrime(int n)
{
    // Check n for Proth Number
    if (isProthNumber(n - 1))
    {
 
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
 
// Driver Code
static public void Main(String []args)
{
    int n = 41;
 
    // if number is proth number,
    // calculate primes upto n
    SieveOfEratosthenes(n);
 
    for (int i = 1; i <= n; i++)
     
        // Check n for Proth Prime
        if (isProthPrime(i))
            Console.WriteLine(i);
}
}
 
// This code is contributed by Arnab Kundu


PHP




<?php
// PHP implementation of the above approach
$GLOBALS['prime'] = array();
 
// Calculate all primes upto n.
function SieveOfEratosthenes($n)
{
    // Initialize all entries it as true.
    // A value in prime[i] will finally
    // false if i is Not a prime, else true.
    for ($i = 1; $i <= $n + 1; $i++)
        $GLOBALS['prime'][$i] = true;
 
    $GLOBALS['prime'][1] = false;
 
    for ($p = 2; $p * $p <= $n; $p++)
    {
 
        // If prime[p] is not changed,
        // then it is a prime
        if ($GLOBALS['prime'][$p] == true)
        {
 
            // Update all multiples of p greater 
            // than or equal to the square of it 
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for ($i = $p * $p; $i <= $n; $i += $p)
                $GLOBALS['prime'][$i] = false;
        }
    }
}
 
// Utility function to check power of two
function isPowerOfTwo($n)
{
    return ($n && !($n & ($n - 1)));
}
 
// Function to check if the Given
// number is Proth number or not
function isProthNumber($n)
{
    $k = 1;
    while ($k < ($n / $k))
    {
 
        // check if k divides n or not
        if ($n % $k == 0)
        {
 
            // Check if n/k is power of 2 or not
            if (isPowerOfTwo($n / $k))
                return true;
        }
 
        // update k to next odd number
        $k = $k + 2;
    }
 
    // If we reach here means there
    // exists no value of K such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
 
// Function to check whether the given
// number is Proth Prime or Not.
function isProthPrime($n)
{
    // Check n for Proth Number
    if (isProthNumber($n - 1))
    {
 
        // if number is prime, return true
        if ($GLOBALS['prime'][$n])
            return true;
        else
            return false;
    }
    else
        return false;
}
 
// Driver Code
$n = 41;
 
// if number is proth number,
// calculate primes upto n
SieveOfEratosthenes($n);
 
for ($i = 1; $i <= $n; $i++)
 
    // Check n for Proth Prime
    if (isProthPrime($i) == true)
        echo $i, "\n";
 
// This code is contributed by Ryuga
?>


Javascript




<script>
// Javascript implementation of the above approach
let prime = new Array();
 
// Calculate all primes upto n.
function SieveOfEratosthenes(n)
{
    // Initialize all entries it as true.
    // A value in prime[i] will finally
    // false if i is Not a prime, else true.
    for (let i = 1; i <= n + 1; i++)
        prime[i] = true;
 
    prime[1] = false;
 
    for (let p = 2; p * p <= n; p++)
    {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p] == true)
        {
 
            // Update all multiples of p greater
            // than or equal to the square of it
            // numbers which are multiple of p and are
            // less than p^2 are already been marked.
            for (let i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
}
 
// Utility function to check power of two
function isPowerOfTwo(n)
{
    return (n && !(n & (n - 1)));
}
 
// Function to check if the Given
// number is Proth number or not
function isProthNumber(n)
{
    let k = 1;
    while (k < (n / k))
    {
 
        // check if k divides n or not
        if (n % k == 0)
        {
 
            // Check if n/k is power of 2 or not
            if (isPowerOfTwo(n / k))
                return true;
        }
 
        // update k to next odd number
        k = k + 2;
    }
 
    // If we reach here means there
    // exists no value of K such
    // that k is odd number and n/k
    // is a power of 2 greater than k
    return false;
}
 
// Function to check whether the given
// number is Proth Prime or Not.
function isProthPrime(n)
{
    // Check n for Proth Number
    if (isProthNumber(n - 1))
    {
 
        // if number is prime, return true
        if (prime[n])
            return true;
        else
            return false;
    }
    else
        return false;
}
 
// Driver Code
let n = 41;
 
// if number is proth number,
// calculate primes upto n
SieveOfEratosthenes(n);
 
for (let i = 1; i <= n; i++)
 
    // Check n for Proth Prime
    if (isProthPrime(i) == true)
        document.write(i + "<br>");
 
// This code is contributed by gfgking
</script>


Output: 

3
5
13
17
41

 

Time Complexity: O(n*log(log(n)))
Auxiliary Space: O(1), constant extra space is required as the size of the prime array is constant.
References: 



Similar Reads

Program to check whether a number is Proth number or not
Given a positive integer N, the task is to check if it is a Proth number. If the given number is a Proth number then print 'YES' otherwise print 'NO'. Proth Number: In mathematics, a Proth number is a positive integer of the form n = k * 2n + 1 where k is an odd positive integer and n is a positive integer such that 2n &gt; k . The first few Proth
11 min read
Length of largest sub-array having primes strictly greater than non-primes
Given an array 'arr' of length 'n'. The task is to find the largest contiguous sub-array having the count of prime numbers strictly greater than the count of non-prime numbers. Examples: Input: arr[] = {4, 7, 4, 7, 11, 5, 4, 4, 4, 5} Output: 9 Input: arr[] = { 1, 9, 3, 4, 5, 6, 7, 8 } Output: 5 Approach: To find the largest subarray in which count
8 min read
Count primes that can be expressed as sum of two consecutive primes and 1
Given a number N. The task is to count the number of prime numbers from 2 to N that can be expressed as a sum of two consecutive primes and 1.Examples: Input: N = 27 Output: 2 13 = 5 + 7 + 1 and 19 = 7 + 11 + 1 are the required prime numbers.Input: N = 34 Output: 3 13 = 5 + 7 + 1, 19 = 7 + 11 + 1 and 31 = 13 + 17 + 1. Approach: An efficient approac
9 min read
Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
Given two integers X and Y, the task is to perform the following operations: Find all prime numbers in the range [X, Y].Generate all numbers possible by combining every pair of primes in the given range.Find the prime numbers among all the possible numbers generated above. Calculate the count of primes among them, say N.Print the Nth term of a Fibo
13 min read
Count of primes below N which can be expressed as the sum of two primes
Given an integer N, the task is to find the count of all the primes below N which can be expressed as the sum of two primes.Examples: Input: N = 6 Output: 1 5 is the only such prime below 6. 2 + 3 = 5.Input: N = 11 Output: 2 Recommended: Please try your approach on {IDE} first, before moving on to the solution.Approach: Create an array prime[] wher
11 min read
Print all safe primes below N
Given an integer N, the task is to print all safe primes below N safe primes. A safe prime is a prime number of the form (2 * p) + 1 where p is also a prime. The first few safe primes are 5, 7, 11, 23, 47, ... Examples: Input: N = 13 Output: 5 7 11 5 = 2 * 2 + 1 7 = 2 * 3 + 1 11 = 2 * 5 + 1 Input: N = 6 Output: 5 7 Approach: First pre-compute all t
8 min read
Print all multiplicative primes &lt;= N
Given an integer N, the task is to print all the multiplicative primes ? N. Multiplicative Primes are the primes such that the product of their digits is also a prime. For example; 2, 3, 7, 13, 17, ... Examples: Input: N = 10 Output: 2 3 5 7Input: N = 3 Output: 2 3 Approach: Using Sieve of Eratosthenes check for all the primes ? N whether they are
8 min read
Sieve of Sundaram to print all primes smaller than n
Given a number n, print all primes smaller than or equal to n. Examples: Input: n = 10 Output: 2, 3, 5, 7 Input: n = 20 Output: 2, 3, 5, 7, 11, 13, 17, 19 We have discussed Sieve of Eratosthenes algorithm for the above task. Below is Sieve of Sundaram algorithm. printPrimes(n) [Prints all prime numbers smaller than n] 1) In general Sieve of Sundara
10 min read
Segmented Sieve (Print Primes in a Range)
Given a range [low, high], print all primes in this range? For example, if the given range is [10, 20], then output is 11, 13, 17, 19. A Naive approach is to run a loop from low to high and check each number for primeness. A Better Approach is to precalculate primes up to the maximum limit using Sieve of Eratosthenes, then print all prime numbers i
15 min read
Product of all primes in the range from L to R
Given a range [L, R]. The task is to find the product of all the prime numbers in the given range from L to R both inclusive modulo 10^9 + 7.Examples: Input: L = 10, R = 20 Output: 46189 Prime numbers between [10, 20] are: 11, 13, 17, 19 Therefore, product = 11 * 13 * 17 * 19 = 46189 Input: L = 15, R = 25 Output: 7429 A Simple Solution is to traver
12 min read