📜  习惯编号

📅  最后修改于: 2021-05-06 17:58:18             🧑  作者: Mango

给定一个正整数n,任务是检查它是否为Thabit编号。如果给定的号码是惯性号码,则打印“是”,否则打印“否”。

惯性数:在数学中,惯性数是3 * 2 n – 1形式的正整数,其中n是非负整数。

头几个Thabit号码是–
2, 5, 11, 23, 47, 95, 191, 383, 767, 1535, 3071, 6143, 12287, 24575, 49151, 98303, 196607, 393215,

例子:

方法 :

  1. 给定数字加1,现在数字必须为3 * 2 n形式
  2. 将数字除以3,到现在为止,数字的形式必须为2 n
  3. 检查数字是否为2的幂,要检查数字是否为2的幂,请参阅此。
  4. 如果数字是2的幂,则打印“是”,否则打印“否”。
C++
// CPP program to check if a given
// number is Thabit number or not.
#include 
using namespace std;
  
// Utility function to check power of two
bool isPowerOfTwo(int n)
{
    return (n && !(n & (n - 1)));
}
  
// function to check if the given number
// is Thabit Number
bool isThabitNumber(int n)
{
    // Add 1 to the number
    n = n + 1;
  
    // Divide the number by 3
    if (n % 3 == 0)
        n = n / 3;
    else
        return false;
  
    // Check if the given number is power of 2
    if (isPowerOfTwo(n))
        return true;
  
    else
        return false;
}
  
// Driver Program
int main()
{
    int n = 47;
    if (isThabitNumber(n)) 
        cout << "YES";    
    else 
        cout << "NO"; 
  
    return 0;
}


Java
// Java code to check
// for thabit number
  
class GFG {
  
    // Utility function to check power of two
    static boolean isPowerOfTwo(int n)
    {
        return n != 0 && ((n & (n - 1)) == 0);
    }
  
    // function to check if the given number
    // is Thabit Number
    static boolean isThabitNumber(int n)
    {
        // Add 1 to the number
        n = n + 1;
  
        // Divide the number by 3
        if (n % 3 == 0)
            n = n / 3;
        else
            return false;
  
        // Check if the given number is power of 2
        if (isPowerOfTwo(n))
            return true;
  
        else
            return false;
    }
  
    // Driver Program
    public static void main(String[] args)
    {
        int n = 47;
  
        // Check if number is
        // thabit number
  
        if (isThabitNumber(n)) {
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}


Python3
# Python code to check
# thabit number
  
# Utility function to Check 
# power of two 
    
def isPowerOfTwo(n): 
        
    return (n and (not(n & (n - 1)))) 
      
      
# function to check if the given number
# is Thabit Number
def isThabitNumber( n ):
  
    # Add 1 to the number
    n = n + 1;
       
    # Divide the number by 3
    if (n % 3 == 0):
        n = n//3;
    else:
       return False
         
        
    # Check if the given number 
    # is power of 2  
    if (isPowerOfTwo(n)):
        return True
       
    else:
         return False    
       
          
# Driver Program 
n = 47 
  
# Check if number is 
# thabit number 
if (isThabitNumber(n)):
    print("YES")
else:  
    print("NO")


C#
// C# code to check
// thabit number
  
using System;
class GFG {
  
    // Utility function to check power of two
    static bool isPowerOfTwo(int n)
    {
        return n != 0 && ((n & (n - 1)) == 0);
    }
  
    // function to check if the given number
    // is Thabit Number
    static bool isThabitNumber(int n)
    {
        // Add 1 to the number
        n = n + 1;
  
        // Divide the number by 3
        if (n % 3 == 0)
            n = n / 3;
        else
            return false;
  
        // Check if the given number is power of 2
        if (isPowerOfTwo(n))
            return true;
  
        else
            return false;
    }
    // Driver Program
    public static void Main()
    {
        int n = 47;
  
        // Check if number is
        // thabit number
  
        if (isThabitNumber(n)) {
            Console.WriteLine("YES");
        }
        else {
            Console.WriteLine("NO");
        }
    }
}


PHP


输出:
YES