Woodall Primes
Last Updated :
22 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool isWoodall( int x)
{
if (x % 2 == 0)
return false ;
if (x == 1)
return true ;
x = x + 1;
int p = 0;
while (x % 2 == 0) {
x = x / 2;
p = p + 1;
if (p == x)
return true ;
}
return false ;
}
void printWoodallPrimesLessThanN( int n)
{
vector< bool > prime(n + 1, true );
int p = 2;
while (p * p <= n) {
if (prime[p])
for ( int i = p * 2; i <= n; i += p)
prime[i] = false ;
p += 1;
}
for (p = 2; p <= n; p++) {
if (prime[p] && isWoodall(p))
cout << p << " " ;
}
}
int main()
{
int n = 1000;
printWoodallPrimesLessThanN(n);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static Boolean isWoodall( int x)
{
if (x % 2 == 0 )
return false ;
if (x == 1 )
return true ;
x = x + 1 ;
int p = 0 ;
while (x % 2 == 0 )
{
x = x / 2 ;
p = p + 1 ;
if (p == x)
return true ;
}
return false ;
}
static void printWoodallPrimesLessThanN( int n)
{
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.get(p))
for ( int i = p * 2 ; i <= n; i += p)
prime.set(i, false );
p += 1 ;
}
for (p = 2 ; p <= n; p++) {
if (prime.get(p) && isWoodall(p))
System.out.print(p + " " );
}
}
public static void main (String []args)
{
int n = 1000 ;
printWoodallPrimesLessThanN(n);
}
}
|
Python3
def isWoodall(x) :
if (x % 2 = = 0 ) :
return False
if (x = = 1 ) :
return True
x = x + 1
p = 0
while (x % 2 = = 0 ) :
x = x / 2
p = p + 1
if (p = = x) :
return True
return False
def printWoodallPrimesLessThanN(n):
prime = [ True ] * (n + 1 );
p = 2 ;
while (p * p < = n):
if (prime[p]):
for i in range (p * 2 , n + 1 , p):
prime[i] = False ;
p + = 1 ;
for p in range ( 2 , n + 1 ):
if (prime[p] and isWoodall(p)):
print (p, end = " " );
n = 1000 ;
printWoodallPrimesLessThanN(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isWoodall( int x)
{
if (x % 2 == 0)
return false ;
if (x == 1)
return true ;
x = x + 1;
int p = 0;
while (x % 2 == 0)
{
x = x / 2;
p = p + 1;
if (p == x)
return true ;
}
return false ;
}
static void printWoodallPrimesLessThanN( int n)
{
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])
for ( int i = p * 2; i <= n; i += p)
prime[i] = false ;
p += 1;
}
for (p = 2; p <= n; p++) {
if (prime[p] && isWoodall(p))
Console.Write(p + " " );
}
}
public static void Main( string [] args)
{
int n = 1000;
printWoodallPrimesLessThanN(n);
}
}
|
Javascript
function isWoodall(x)
{
if (x % 2 == 0)
return false
if (x == 1)
return true
x = x + 1
let p = 0
while (x % 2 == 0)
{
x = x / 2
p = p + 1
if (p == x)
return true
}
return false
}
function printWoodallPrimesLessThanN(n)
{
let prime = new Array(n + 1).fill( true )
let p = 2;
while (p * p <= n)
{
if (prime[p])
for ( var i = p * 2; i <= n; i += p)
prime[i] = false ;
p += 1;
}
for (p = 2; p <= n; p ++)
{
if (prime[p] && isWoodall(p))
process.stdout.write(p + " " );
}
}
let n = 1000;
printWoodallPrimesLessThanN(n)
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...