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
- 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 number 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 above idea
C++
// CPP program to check Proth number #include <bits/stdc++.h> 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 Proth number or not bool isProthNumber(int n) { int k = 1; while (k < (n / k)) { // check if k divides n or not if (n % k == 0) { // Check if n/k is power of 2 or not if (isPowerOfTwo(n / k)) return true; } // update k to next odd number k = k + 2; } // If we reach here means // there exists no value of K // Such that k is odd number // and n/k is a power of 2 greater than k return false; } // Driver code int main() { // Get n int n = 25; // Check n for Proth Number if (isProthNumber(n - 1)) cout << "YES"; else cout << "NO"; return 0; } |
Java
// Java program to check for Proth 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 Proth number or not static boolean isProthNumber(int n) { int k = 1; while (k < (n / k)) { // check if k divides n or not if (n % k == 0) { // Check if n/k is power of 2 or not if (isPowerOfTwo(n / k)) return true; } // update k to next odd number k = k + 2; } // If we reach here means // there exists no value of K // Such that k is odd number // and n/k is a power of 2 greater than k return false; } // Driver code public static void main(String[] args) { // Get n int n = 25; // Check n for Proth Number if (isProthNumber(n - 1)) System.out.println("YES"); else System.out.println("NO"); } } |
Python3
# Python3 program to check for Proth number # Utility funtion to Check # power of two def isPowerOfTwo(n): return (n and (not(n & (n - 1)))) # Function to check if the # Given number is Proth number or not def isProthNumber( n): k = 1 while(k < (n//k)): # check if k divides n or not if(n % k == 0): # Check if n / k is power of 2 or not if(isPowerOfTwo(n//k)): return True # update k to next odd number k = k + 2 # If we reach here means # there exists no value of K # Such that k is odd number # and n / k is a power of 2 greater than k return False # Driver code # Get n int n = 25; # Check n for Proth Number if(isProthNumber(n-1)): print("YES"); else: print("NO"); |
C#
// C# program to check Proth 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 Proth number or not static bool isProthNumber(int n) { int k = 1; while (k < (n / k)) { // check if k divides n or not if (n % k == 0) { // Check if n/k is power of 2 or not if (isPowerOfTwo(n / k)) return true; } // update k to next odd number k = k + 2; } // If we reach here means // there exists no value of K // Such that k is odd number // and n/k is a power of 2 greater than k return false; } // Driver code public static void Main() { // Get n int n = 25; // Check n for Proth Number if (isProthNumber(n - 1)) Console.WriteLine("YES"); else Console.WriteLine("NO"); } } |
PHP
<?php // PHP program to check Proth number // Utility function to check // power of two function isPowerOfTwo($n) { return ($n && !($n & ($n - 1))); } // Function to check if the // Given number is Proth // number or not function isProthNumber($n) { $k = 1; while ($k < ($n / $k)) { // check if k divides n or not if ($n % $k == 0) { // Check if n/k is power // of 2 or not if (isPowerOfTwo($n / $k)) return true; } // update k to next odd number $k = $k + 2; } // If we reach here means // there exists no value of K // Such that k is odd number // and n/k is a power of 2 // greater than k return false; } // Driver code // Get n $n = 25; // Check n for Proth Number if (isProthNumber($n - 1)) echo "YES"; else echo "NO"; // This code is contributed // by inder_verma ?> |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C Program to check whether a number is a Perfect Cube or not
- Check Whether a number is Duck Number or not
- Check whether the given number is Euclid Number or not
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check whether given number N is a Moran Number or not
- Check whether a given number N is a Nude Number or not
- Check whether a given number is an ugly number or not
- Check whether a number is circular prime or not
- Check whether a number has exactly three distinct factors or not
- Check whether a number is semiprime or not
- Check whether a number is Emirpimes or not
- Check whether a given number is Polydivisible or Not
- Check whether a given Number is Power-Isolated or not
- Check whether the given number is Wagstaff prime or not
- Check whether a number has consecutive 0's in the given base or not
- Check whether N is a Dihedral Prime Number or not
- Check whether the binary equivalent of a number ends with "001" or not
- Check whether a number is Good prime or not
- Check whether the binary equivalent of a number ends with given string or not
- Check whether a large number is divisible by 53 or not
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 : inderDuMCA

