Open In App

Woodall Primes

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Woodall Primes are prime numbers that are also Woodall number.
 

Find the Woodall prime numbers less than N

Given a number N, print all Woodall primes smaller than or equal to N. 
Examples: 
 

Input: N = 10 
Output: 7
Input: N = 500 
Output: 7, 23, 383 
 

 

Approach: The idea is to use Sieve of Eratosthenes to check that a number is prime or not efficiently. Then, Iterate over integers from 1 to N, and for every number check that if it is prime or not and it is Woodall number or not. If a number is prime also a Woodall number, Then it a Woodall prime.
 

Below is the implementation of above algorithm:

 

C++




// C++ implementation to print all Woodall
// primes smaller than or equal to n.
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number
// N is Woodall
bool isWoodall(int x)
{
  // If number is even, return false.
  if (x % 2 == 0)
    return false;
 
  // If x is 1, return true.
  if (x == 1)
    return true;
 
  x = x + 1; // Add 1 to make x even
 
  // While x is divisible by 2
  int p = 0;
  while (x % 2 == 0) {
    // Divide x by 2
    x = x / 2;
 
    // Count the power
    p = p + 1;
 
    // If at any point power and
    // x became equal, return true.
    if (p == x)
      return true;
  }
 
  return false;
}
 
// Function to generate all primes and checking
// whether number is Woodall or not
void printWoodallPrimesLessThanN(int n)
{
  // Create a boolean array "prime[0..n]" and
  // initialize all entries it as true. A value
  // in prime[i] will finally be false if i is
  // Not a prime, else true.
  vector<bool> prime(n + 1, true);
 
  int p = 2;
  while (p * p <= n) {
    // If prime[p] is not changed,
    // then it is a prime
    if (prime[p])
 
      // Update all multiples of p
      for (int i = p * 2; i <= n; i += p)
        prime[i] = false;
    p += 1;
  }
 
  // Print all Woodall prime numbers
  for (p = 2; p <= n; p++) {
 
    // checking whether the given number
    // is prime Woodall or not
    if (prime[p] && isWoodall(p))
      cout << p << " ";
  }
}
 
// Driver Code
int main()
{
  int n = 1000;
  printWoodallPrimesLessThanN(n);
}
 
// This code is contributed by phasing17


Java




// Java implementation to print all Woodall
// primes smaller than or equal to n.
import java.io.*;
import java.util.*;
 
class GFG
{
   
  // Function to check if a number
  // N is Woodall
  static Boolean isWoodall(int x)
  {
 
    // If number is even, return false.
    if (x % 2 == 0)
      return false;
 
    // If x is 1, return true.
    if (x == 1)
      return true;
 
    x = x + 1; // Add 1 to make x even
 
    // While x is divisible by 2
    int p = 0;
    while (x % 2 == 0)
    {
 
      // Divide x by 2
      x = x / 2;
 
      // Count the power
      p = p + 1;
 
      // If at any point power and
      // x became equal, return true.
      if (p == x)
        return true;
    }
 
    return false;
  }
 
  // Function to generate all primes and checking
  // whether number is Woodall or not
  static void printWoodallPrimesLessThanN(int n)
  {
 
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    ArrayList<Boolean> prime = new ArrayList<Boolean>();
 
    for (int i = 0; i <= n; i++)
      prime.add(true);
 
    int p = 2;
    while (p * p <= n)
    {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime.get(p))
 
        // Update all multiples of p
        for (int i = p * 2; i <= n; i += p)
          prime.set(i,false);
      p += 1;
    }
 
    // Print all Woodall prime numbers
    for (p = 2; p <= n; p++) {
 
      // checking whether the given number
      // is prime Woodall or not
      if (prime.get(p) && isWoodall(p))
        System.out.print(p + " ");
    }
  }
 
  // Driver Code
  public static void main (String []args)
  {
    int n = 1000;
    printWoodallPrimesLessThanN(n);
  }
}
 
// This code is contributed by Pushpesh Raj


Python3




# Python3 implementation to print all Woodall 
# primes smaller than or equal to n. 
    
# Function to check if a number
# N is Woodall  
def isWoodall(x) :
       
    # If number is even, return false.
    if (x % 2 == 0) :
        return False
    
    # If x is 1, return true.
    if (x == 1) :
        return True
        
    x = x + 1  # Add 1 to make x even
    
    # While x is divisible by 2
    p = 0
    while (x % 2 == 0) :
           
        # Divide x by 2
        x = x / 2
    
        # Count the power
        p = p + 1
    
        # If at any point power and 
        # x became equal, return true.
        if (p == x) :
            return True
           
    return False
       
# Function to generate all primes and checking 
# whether number is Woodall or not 
def printWoodallPrimesLessThanN(n):
       
    # Create a boolean array "prime[0..n]" and 
    # initialize all entries it as true. A value 
    # in prime[i] will finally be false if i is 
    # Not a prime, else true. 
    prime = [True] * (n + 1); 
    p = 2;
    while (p * p <= n):
           
        # If prime[p] is not changed, 
        # then it is a prime 
        if (prime[p]): 
               
            # Update all multiples of p 
            for i in range(p * 2, n + 1, p): 
                prime[i] = False;
        p += 1;
           
    # Print all Woodall prime numbers 
    for p in range(2, n + 1): 
           
        # checking whether the given number 
        # is prime Woodall or not 
        if (prime[p] and isWoodall(p)): 
            print(p, end = " "); 
       
# Driver Code 
n = 1000;
printWoodallPrimesLessThanN(n)


C#




// C# implementation to print all Woodall
// primes smaller than or equal to n.
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to check if a number
  // N is Woodall
  static bool isWoodall(int x)
  {
 
    // If number is even, return false.
    if (x % 2 == 0)
      return false;
 
    // If x is 1, return true.
    if (x == 1)
      return true;
 
    x = x + 1; // Add 1 to make x even
 
    // While x is divisible by 2
    int p = 0;
    while (x % 2 == 0)
    {
 
      // Divide x by 2
      x = x / 2;
 
      // Count the power
      p = p + 1;
 
      // If at any point power and
      // x became equal, return true.
      if (p == x)
        return true;
    }
 
    return false;
  }
 
  // Function to generate all primes and checking
  // whether number is Woodall or not
  static void printWoodallPrimesLessThanN(int n)
  {
 
    // Create a boolean array "prime[0..n]" and
    // initialize all entries it as true. A value
    // in prime[i] will finally be false if i is
    // Not a prime, else true.
    List<bool> prime = new List<bool>();
    for (int i = 0; i <= n; i++)
      prime.Add(true);
 
 
    int p = 2;
    while (p * p <= n)
    {
 
      // If prime[p] is not changed,
      // then it is a prime
      if (prime[p])
 
        // Update all multiples of p
        for (int i = p * 2; i <= n; i += p)
          prime[i] = false;
      p += 1;
    }
 
    // Print all Woodall prime numbers
    for (p = 2; p <= n; p++) {
 
      // checking whether the given number
      // is prime Woodall or not
      if (prime[p] && isWoodall(p))
        Console.Write(p + " ");
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int n = 1000;
    printWoodallPrimesLessThanN(n);
  }
}
 
// This code is contributed by phasing17


Javascript




// Python3 implementation to print all Woodall 
// primes smaller than or equal to n. 
    
// Function to check if a number
// N is Woodall  
function isWoodall(x) 
{
    // If number is even, return false.
    if (x % 2 == 0) 
        return false
    
    // If x is 1, return true.
    if (x == 1) 
        return true
        
    x = x + 1  // Add 1 to make x even
    
    // While x is divisible by 2
    let p = 0
    while (x % 2 == 0) 
    {     
        // Divide x by 2
        x = x / 2
    
        // Count the power
        p = p + 1
    
        // If at any point power and 
        // x became equal, return true.
        if (p == x) 
            return true
    }
           
    return false
}
 
// Function to generate all primes and checking 
// whether number is Woodall or not 
function printWoodallPrimesLessThanN(n)
{
    // Create a boolean array "prime[0..n]" and 
    // initialize all entries it as true. A value 
    // in prime[i] will finally be false if i is 
    // Not a prime, else true.
    let prime = new Array(n + 1).fill(true)
     
    let p = 2;
    while (p * p <= n)
    {
        // If prime[p] is not changed, 
        // then it is a prime 
        if (prime[p]) 
               
            // Update all multiples of p 
            for (var i = p * 2; i <= n; i += p)
                prime[i] = false;
        p += 1;
    }
           
    // Print all Woodall prime numbers
    for (p = 2; p <= n; p ++)
    {
           
        // checking whether the given number 
        // is prime Woodall or not 
        if (prime[p] && isWoodall(p)) 
            process.stdout.write(p + " ");
    }
}
       
// Driver Code 
let n = 1000;
printWoodallPrimesLessThanN(n)
 
 
 
// This code is contributed by phasing17


Output: 

7 23 383

 

Time Complexity: O(n*log(n))
Auxiliary Space: O(n)



Similar Reads

Woodall Number
A Woodall number is of the form: Wn = n.2n - 1 The first few Woodall numbers are: 1, 7, 23, 63, 159, 383, 895......Given a number X. The task is to check if X is woodall number or not.Examples: Input : X = 383 Output : Yes For n = 6, Wn = n.2n - 1 = 383. Input : X = 200 Output : No Recommended PracticeWoodall NumberTry It! We can observe that all W
5 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
A Kobayashi Maru Approach to Finding Primes
It has been said that "Great engineering strikes a balance between competing objectives." I believe this to be true, in both mechanical and software engineering. In the latter, the competing objectives are typically clarity, size, and speed. So I thought it a great opportunity to apply this principle when a computer scientist in South America chall
5 min read
Number of primes in a subarray (with updates)
Given an array of N integers, the task is to perform the following two queries: query(start, end) : Print the number of prime numbers in the subarray from start to end update(i, x) : update the value at index i to x, i.e arr[i] = x Examples: Input : arr = {1, 2, 3, 5, 7, 9} Query 1: query(start = 0, end = 4) Query 2: update(i = 3, x = 6) Query 3: q
31 min read
Minimum number of single digit primes required whose sum is equal to N
Find the minimum number of single-digit prime numbers required whose sum will be equal to N. Examples: Input: 11 Output: 3 Explanation: 5 + 3 + 3. Another possibility is 3 + 3 + 3 + 2, but it is not the minimal Input: 12 Output: 2 Explanation: 7 + 5 Approach: Dynamic Programming can be used to solve the above problem. The observations are: There ar
9 min read
Alternate Primes till N
We have to print alternate prime numbers till N.Examples: Input : N = 10 Output : 2 5 Input : N = 15 Output : 2 5 11 Naive Approach:We can just simply iterate over N and check whether the number is prime or not and print the alternate number just by keeping a simple altering flag variable. C/C++ Code /* C++ program to print all primes smaller than
14 min read
Count numbers which can be represented as sum of same parity primes
Given an arr[] of positive integers you have to count how many numbers can be represented as sum of same parity prime numbers(can be same) Examples: Input : arr[] = {1, 3, 4, 6}Output : 2Explanation: 4 = 2+2, 6 = 3+3 Input : arr[] = {4, 98, 0, 36, 51}Output : 3 1. If two numbers of same parity are added then they would be always even, so all odd nu
4 min read