Woodall Number in C++


In this problem, we are given a number and our task is to check if the number is Woodall number or not.

Woodall number is a special type of number which is of the form,

Wn = n.2n -1

First 5 Woodall numbers are 1, 7, 23, 63, 159

Let’s take an example to understand the problem,

Input

X = 159

Output

Yes

To solve this problem, we will observe the number, if the number is even then it cannot be Woodall and then check for the number. To check, add the number by 1 and recursively divide the number by 2. after each division count the number of times it is divisible. And at each point check if the count is equal to the number.

Program to show the implementation of our solution,

Example

 Live Demo

#include <iostream>
using namespace std;
bool isWoodallNumber(int x){
   if (x % 2 == 0)
      return false;
   if (x == 1)
      return true;
   x+= 1;
   int p = 0;
   while(x%2 == 0){
      x = x/2;
      p++;
      if (p == x)
         return true;
   }
   return false;
}
int main() {
   int x = 1424;
   cout<<x;
   (isWoodallNumber(x))?cout<<" is a Woodal Number":cout<<" is not a Woodal Number";
   return 0;
}

Output

1424 is not a Woodal Number

Updated on: 17-Jul-2020

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements