Program to check whether a number is Proth number or not
Last Updated :
21 Sep, 2023
Given a positive integer N, the task is to check if it is a Proth number. If the given number is a Proth number then print ‘YES’ otherwise print ‘NO’.
Proth Number: In mathematics, a Proth number is a positive integer of the form
n = k * 2n + 1
where k is an odd positive integer and n is a positive integer such that 2n > k .
The first few Proth numbers are –
3, 5, 9, 13, 17, 25, 33, 41, 49, ……
Examples:
Input: 25
Output: YES
Taking k= 3 and n= 3,
25 can be expressed in the form of
(k.2n + 1) as (3.23 + 1)
Input: 73
Output: NO
Taking k=9 and n=3
73 can be expressed in the form of
(k.2n + 1 ) as (9.23 + 1)
But 23 is less than 9
(it should be greater than k to be Proth Number)
Approach 1:
- Iterate through values of k from 1 to n.
- For each k, iterate through exponent from 0 to n.
- Calculate prothNumber using the formula k * (1ULL << exponent) + 1.
- If prothNumber is equal to the given number n, return true.
- If prothNumber is greater than n, break the inner loop and continue with the next k.
- If no Proth number is found, return false.
Note: This brute-force approach has high time complexity and is not efficient for large values of n.
C++
#include <iostream>
#include <cmath>
bool isPowerOfTwo(unsigned long long int n) {
return n && !(n & (n - 1));
}
bool isProthNumber(unsigned long long int n) {
if (n < 3)
return false ;
for (unsigned long long int k = 1; k <= n; ++k) {
for (unsigned long long int exponent = 0; exponent <= n; ++exponent) {
unsigned long long int prothNumber = k * (1ULL << exponent) + 1;
if (prothNumber == n)
return true ;
if (prothNumber > n)
break ;
}
}
return false ;
}
int main() {
unsigned long long int n = 25;
if (isProthNumber(n - 1))
std::cout << "YES" << std::endl;
else
std::cout << "NO" << std::endl;
return 0;
}
|
Java
public class GFG {
public static boolean isPowerOfTwo( long n) {
return n != 0 && (n & (n - 1 )) == 0 ;
}
public static boolean isProthNumber( long n) {
if (n < 3 )
return false ;
for ( long k = 1 ; k <= n; ++k) {
for ( long exponent = 0 ; exponent <= n; ++exponent) {
long prothNumber = k * (1L << exponent) + 1 ;
if (prothNumber == n)
return true ;
if (prothNumber > n)
break ;
}
}
return false ;
}
public static void main(String[] args) {
long n = 25 ;
if (isProthNumber(n - 1 ))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def isPowerOfTwo(n):
return n ! = 0 and (n & (n - 1 )) = = 0
def isProthNumber(n):
if n < 3 :
return False
for k in range ( 1 , n + 1 ):
for exponent in range ( 0 , n + 1 ):
prothNumber = k * ( 1 << exponent) + 1
if prothNumber = = n:
return True
if prothNumber > n:
break
return False
n = 25
if isProthNumber(n - 1 ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class Program
{
static bool IsPowerOfTwo( int n)
{
return n != 0 && (n & (n - 1)) == 0;
}
static bool IsProthNumber( int n)
{
if (n < 3)
return false ;
for ( int k = 1; k <= n; k++)
{
for ( int exponent = 0; exponent <= n; exponent++)
{
int prothNumber = k * (1 << exponent) + 1;
if (prothNumber == n)
return true ;
if (prothNumber > n)
break ;
}
}
return false ;
}
static void Main( string [] args)
{
int n = 25;
if (IsProthNumber(n - 1))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function isPowerOfTwo(n) {
return n !== 0 && (n & (n - 1)) === 0;
}
function isProthNumber(n) {
if (n < 3)
return false ;
for (let k = 1; k <= n; ++k) {
for (let exponent = 0; exponent <= n; ++exponent) {
const prothNumber = k * (1 << exponent) + 1;
if (prothNumber === n)
return true ;
if (prothNumber > n)
break ;
}
}
return false ;
}
const n = 25;
if (isProthNumber(n - 1)) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Approach 2:
- Deduct 1 from the number. This would give a number in the form k*2n, if the given number is a proth number.
- Now, loop through all odd numbers starting form k=1 to n/k and check if k can divide n in such a way that ( n/k ) is a power of 2 or not.
- If found, print ‘YES’
- If no such value of k is found then Print ‘NO’
Below is the implementation of the above idea
C++
#include <bits/stdc++.h>
using namespace std;
bool isPowerOfTwo( int n)
{
return (n && !(n & (n - 1)));
}
bool isProthNumber( int n)
{
int k = 1;
while (k < (n / k)) {
if (n % k == 0) {
if (isPowerOfTwo(n / k))
return true ;
}
k = k + 2;
}
return false ;
}
int main()
{
int n = 25;
if (isProthNumber(n - 1))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG {
static boolean isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1 )) == 0 );
}
static boolean isProthNumber( int n)
{
int k = 1 ;
while (k < (n / k)) {
if (n % k == 0 ) {
if (isPowerOfTwo(n / k))
return true ;
}
k = k + 2 ;
}
return false ;
}
public static void main(String[] args)
{
int n = 25 ;
if (isProthNumber(n - 1 ))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def isPowerOfTwo(n):
return (n and ( not (n & (n - 1 ))))
def isProthNumber( n):
k = 1
while (k < (n / / k)):
if (n % k = = 0 ):
if (isPowerOfTwo(n / / k)):
return True
k = k + 2
return False
int n = 25 ;
if (isProthNumber(n - 1 )):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG {
static bool isPowerOfTwo( int n)
{
return n != 0 && ((n & (n - 1)) == 0);
}
static bool isProthNumber( int n)
{
int k = 1;
while (k < (n / k)) {
if (n % k == 0) {
if (isPowerOfTwo(n / k))
return true ;
}
k = k + 2;
}
return false ;
}
public static void Main()
{
int n = 25;
if (isProthNumber(n - 1))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function isPowerOfTwo(n)
{
return (n && !(n & (n - 1)));
}
function isProthNumber(n)
{
let k = 1;
while (k < parseInt(n / k))
{
if (n % k == 0)
{
if (isPowerOfTwo(parseInt(n / k)))
return true ;
}
k = k + 2;
}
return false ;
}
let n = 25;
if (isProthNumber(n - 1))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
PHP
<?php
function isPowerOfTwo( $n )
{
return ( $n && !( $n & ( $n - 1)));
}
function isProthNumber( $n )
{
$k = 1;
while ( $k < ( $n / $k ))
{
if ( $n % $k == 0)
{
if (isPowerOfTwo( $n / $k ))
return true;
}
$k = $k + 2;
}
return false;
}
$n = 25;
if (isProthNumber( $n - 1))
echo "YES" ;
else
echo "NO" ;
?>
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...