Skip to content
Related Articles

Related Articles

Improve Article

Woodall Primes

  • Difficulty Level : Basic
  • Last Updated : 26 Jun, 2020

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.

Examaples:

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:

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)
Output:
7 23 383

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :