Woodall Number
Last Updated :
07 Aug, 2022
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
- We can observe that all Woodall numbers are odd. So, first of all we check if given number is odd or not.
- Now to check if number is woodall or not, increment given number by 1 and now divide number by 2 until it is even and count number of times it is divisible. And at each point check if count is equal to number or not.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isWoodall( int x)
{
if (x % 2 == 0)
return false ;
if (x == 1)
return true ;
x++;
int p = 0;
while (x % 2 == 0) {
x = x/2;
p++;
if (p == x)
return true ;
}
return false ;
}
int main()
{
int x = 383;
(isWoodall(x)) ? (cout << "Yes" << endl) :
(cout << "No" << endl);
return 0;
}
|
Java
class GFG {
static boolean isWoodall( int x)
{
if (x % 2 == 0 )
return false ;
if (x == 1 )
return true ;
x++;
int p = 0 ;
while (x % 2 == 0 ) {
x = x / 2 ;
p++;
if (p == x)
return true ;
}
return false ;
}
public static void main(String args[])
{
int x = 383 ;
if (isWoodall(x))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python
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
x = 383
if (isWoodall(x)) :
print "Yes"
else :
print "No"
|
C#
using System;
class GFG {
static bool isWoodall( int x)
{
if (x % 2 == 0)
return false ;
if (x == 1)
return true ;
x++;
int p = 0;
while (x % 2 == 0) {
x = x / 2;
p++;
if (p == x)
return true ;
}
return false ;
}
public static void Main()
{
int x = 383;
if (isWoodall(x))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isWoodall( $x )
{
if ( $x % 2 == 0)
return false;
if ( $x == 1)
return true;
$x ++;
$p = 0;
while ( $x % 2 == 0)
{
$x = $x /2;
$p ++;
if ( $p == $x )
return true;
}
return false;
}
$x = 383;
if (isWoodall( $x ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isWoodall(x)
{
if (x % 2 == 0)
return false ;
if (x == 1)
return true ;
x++;
let p = 0;
while (x % 2 == 0)
{
x = x/2;
p++;
if (p == x)
return true ;
}
return false ;
}
let x = 383;
if (isWoodall(x))
document.write( "Yes" ) ;
else
document.write( "No" ) ;
</script>
|
Output:
Yes
Time complexity: O(log n) since while loop will run for log n times
Auxiliary space: O(1) as using constant space
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...