📜  Woodall号码

📅  最后修改于: 2021-05-04 14:36:52             🧑  作者: Mango

Woodall号的格式为:

Woodall的前几个数字是:1、7、23、63、159、383、895……
给定数字X。任务是检查X是否为woodall数字。
例子:

Input : X = 383
Output : Yes
For n = 6, Wn = n.2n - 1 = 383.

Input : X = 200
Output : No
  1. 我们可以观察到所有伍德洛数都是奇数。因此,首先我们检查给定的数字是否为奇数。
  2. 现在要检查数字是否为woodall,将给定数字加1,然后将数字除以2,直到它为偶数并计数可除的次数。并在每个点检查计数是否等于数字。

以下是此方法的实现:

C++
// CPP program to check if a number is
// Woodball or not.
#include 
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 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.


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


Javascript


输出:

Yes