# Thabit number

Given a positive integer n, the task is to check if it is a Thabit number. If the given number is a Thabit number then print ‘YES’ otherwise print ‘NO’.

Thabit number: In mathematics, a Thabit Number is a positive integer of the form 3* 2n – 1, where n is a non negative integer.

The first few Thabit numbers are – ``` 2, 5, 11, 23, 47, 95, 191, 383, 767, 1535, 3071, 6143, 12287, 24575, 49151, 98303, 196607, 393215, ```

Examples:

Input : 47
Output : YES
Explanation : for n=4, 47 can be expressed in the form of 3.2n -1 as 3.24 -1.

Input: 65
Output : NO
No such value of n exist for which 65 can be expressed in the form of 3.2n – 1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach :

1. Add 1 to the given number, Now the number must be of the form 3*2n
2. Divide the number by 3, By now the number must be of the form 2n
3. Check if the number is a power of 2 or not, To check if the number is power of two or not refer this .
4. If the number is power of 2 then Print ‘YES’ otherwise ‘NO’.

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

 ` `

Output:

```YES
```

My Personal Notes arrow_drop_up self motivated and passionate programmer

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Shashank12, shubham_singh

Article Tags :

Be the First to upvote.

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