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

  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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to check if a number is
// Woodball 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.*/

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check if a number
# is Woodball 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.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check
// if a number is
// Woodball 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.
?>

chevron_right



Output:

Yes

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.