Open In App

Woodall Number

Last Updated : 07 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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 Practice

 

  1. We can observe that all Woodall numbers are odd. So, first of all we check if given number is odd or not.
  2. 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++




// CPP program to check if a number is
// Woodall or not.
#include <bits/stdc++.h>
using namespace std;
 
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++;  // 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++;
 
        // If at any point power and
        // x became equal, return true.
        if (p == x)
            return true;
    }
 
    return false;
}
 
// Driven Program
int main()
{
    int x = 383;
 
    (isWoodall(x)) ? (cout << "Yes" << endl) :
                     (cout << "No" << endl);
    return 0;
}


Java




// JAVA program to check if a number
// is Woodall or not.
class GFG {
     
    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++;  // 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++;
      
            // If at any point power and
            // x became equal, return true.
            if (p == x)
                return true;
        }
      
        return false;
    }
      
    // Driven Program
    public static void main(String args[])
    {
        int x = 383;
      
        if(isWoodall(x))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python




# Python program to check if a number
# is Woodall or not.
 
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
     
  
# Driven Program
x = 383
if(isWoodall(x)) :
    print "Yes"
else  :
    print "No"
     
# This code is contributed by Nikita Tiwari.


C#




// C# program to check if a number
// is Woodall or not.
using System;
 
class GFG {
     
    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++; // 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++;
     
            // If at any point power and
            // x became equal, return true.
            if (p == x)
                return true;
        }
     
        return false;
    }
     
    // Driver Code
    public static void Main()
    {
        int x = 383;
     
        if(isWoodall(x))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by Nikita Tiwari.


PHP




<?php
// PHP program to check
// if a number is
// Woodall or not.
 
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;
         
    // Add 1 to
    // make x even
    $x++;
 
    // While x is
    // divisible by 2
    $p = 0;
    while ($x % 2 == 0)
    {
 
        // Divide x by 2
        $x = $x/2;
 
        // Count the power
        $p++;
 
        // If at any point power and
        // x became equal, return true.
        if ($p == $x)
            return true;
    }
 
    return false;
}
 
    // Driver Code
    $x = 383;
 
    if(isWoodall($x))
    echo "Yes" ;
    else
    echo "No" ;
     
// This code is contributed by anuj_67.
?>


Javascript




<script>
// Javascript program to check
// if a number is
// Woodall or not.
 
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;
         
    // Add 1 to
    // make x even
    x++;
 
    // While x is
    // divisible by 2
    let p = 0;
    while (x % 2 == 0)
    {
 
        // Divide x by 2
        x = x/2;
 
        // Count the power
        p++;
 
        // If at any point power and
        // x became equal, return true.
        if (p == x)
            return true;
    }
 
    return false;
}
 
    // Driver Code
    let x = 383;
 
    if(isWoodall(x))
    document.write("Yes") ;
    else
    document.write("No") ;
     
// This code is contributed by _saurabh_jaiswal
</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
Previous
Next
Share your thoughts in the comments

Similar Reads