Thabit number
Last Updated :
26 Aug, 2022
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 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
Explanation: No such value of n exist for which 65 can be expressed in the form of 3.2n – 1
Approach :
- Add 1 to the given number, Now the number must be of the form 3*2n
- Divide the number by 3, By now the number must be of the form 2n
- Check if the number is a power of 2 or not, To check if the number is power of two or not refer this .
- If the number is power of 2 then Print ‘YES’ otherwise ‘NO’.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfTwo( int n)
{
return (n && !(n & (n - 1)));
}
bool isThabitNumber( int n)
{
n = n + 1;
if (n % 3 == 0)
n = n / 3;
else
return false ;
if (isPowerOfTwo(n))
return true ;
else
return false ;
}
int main()
{
int n = 47;
if (isThabitNumber(n))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG {
static boolean isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1 )) == 0 );
}
static boolean isThabitNumber( int n)
{
n = n + 1 ;
if (n % 3 == 0 )
n = n / 3 ;
else
return false ;
if (isPowerOfTwo(n))
return true ;
else
return false ;
}
public static void main(String[] args)
{
int n = 47 ;
if (isThabitNumber(n)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def isPowerOfTwo(n):
return (n and ( not (n & (n - 1 ))))
def isThabitNumber( n ):
n = n + 1 ;
if (n % 3 = = 0 ):
n = n / / 3 ;
else :
return False
if (isPowerOfTwo(n)):
return True
else :
return False
n = 47
if (isThabitNumber(n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG {
static bool isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1)) == 0);
}
static bool isThabitNumber( int n)
{
n = n + 1;
if (n % 3 == 0)
n = n / 3;
else
return false ;
if (isPowerOfTwo(n))
return true ;
else
return false ;
}
public static void Main()
{
int n = 47;
if (isThabitNumber(n)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
PHP
<?php
function isPowerOfTwo( $n )
{
return ( $n && !( $n & ( $n - 1)));
}
function isThabitNumber( $n )
{
$n = $n + 1;
if ( $n % 3 == 0)
$n = $n / 3;
else
return false;
if (isPowerOfTwo( $n ))
return true;
else
return false;
}
$n = 47;
if (isThabitNumber( $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function isPowerOfTwo(n)
{
return (n && !(n & (n - 1)));
}
function isThabitNumber(n)
{
n = n + 1;
if (n % 3 == 0)
n = n / 3;
else
return false ;
if (isPowerOfTwo(n))
return true ;
else
return false ;
}
var n = 47;
if (isThabitNumber(n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(1), as only constant operations are performed.
Auxiliary Space: O(1) as no extra space is used.
Share your thoughts in the comments
Please Login to comment...