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>
// Utility function to check if a number is a power of two
bool isPowerOfTwo(unsigned long long int n) {
return n && !(n & (n - 1));
}
// Function to check if a number is a Proth number using brute-force
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;
}
// Nikunj Sonigara
// Driver code
int main() {
// Get n
unsigned long long int n = 25;
// Check n for Proth Number
if (isProthNumber(n - 1))
std::cout << "YES" << std::endl;
else
std::cout << "NO" << std::endl;
return 0;
}
Java
public class GFG {
// Utility function to check if a number is a power of two
public static boolean isPowerOfTwo(long n) {
return n != 0 && (n & (n - 1)) == 0;
}
// Function to check if a number is a Proth number using brute-force
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;
}
// Nikunj Sonigara
public static void main(String[] args) {
// Get n
long n = 25;
// Check n for Proth Number
if (isProthNumber(n - 1))
System.out.println("YES");
else
System.out.println("NO");
}
}
Python3
# Utility function to check if a number is a power of two
def isPowerOfTwo(n):
return n != 0 and (n & (n - 1)) == 0
# Function to check if a number is a Proth number using brute-force
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
# Nikunj Sonigara
# Driver code
n = 25
# Check n for Proth Number
if isProthNumber(n - 1):
print("YES")
else:
print("NO")
C#
using System;
class Program
{
// Utility function to check if a number is a power of two
static bool IsPowerOfTwo(int n)
{
return n != 0 && (n & (n - 1)) == 0;
}
// Function to check if a number is a Proth number using brute-force
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;
// Check n for Proth Number
if (IsProthNumber(n - 1))
{
Console.WriteLine("YES");
}
else
{
Console.WriteLine("NO");
}
}
}
JavaScript
// Utility function to check if a number is a power of two
function isPowerOfTwo(n) {
return n !== 0 && (n & (n - 1)) === 0;
}
// Function to check if a number is a Proth number using brute-force
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;
}
// Driver code
const n = 25;
// Check n for Proth Number
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++
// 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 function 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");
}
}
JavaScript
<script>
// Javascript 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)
{
let k = 1;
while (k < parseInt(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(parseInt(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
let n = 25;
// Check n for Proth Number
if (isProthNumber(n - 1))
document.write("YES");
else
document.write("NO");
// This code is contributed by souravmahato348
</script>
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
?>
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Similar Reads
Program to check whether the given number is Buzz Number or not
A number is said to be Buzz Number if it ends with 7 OR is divisible by 7. The task is to check whether the given number is buzz number or not.Examples: Input : 63 Output : Buzz Number Explanation: 63 is divisible by 7, one of the condition is satisfied. Input : 72 Output : Not a Buzz Number Explana
4 min read
Check whether a number is Tech Number or not
Given a number, the task is to check if it is a Tech number or not. Note: A Tech number is a number that has an even number of digits and if the number is split into two equal halves(From the middle), then the square of the sum of these halves is equal to the number itself. Examples:Input: n = 81 Ou
6 min read
Prime Number Program | Code to Check whether a number is Prime or not
Given a positive integer n, write a code to check whether the number is prime. What is Prime Number?A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of the first few prime numbers are {2, 3, 5, ...}Examples:Input: n = 11Output: trueInput: n =
12 min read
Check whether a number is Sublime number or not
Given a number N, the task is to check whether the number is a Sublime number or not. Sublime numbers are defined as positive numbers whose sum of positive divisors and count of positive divisors are both perfect numbers. Examples: Input: N = 12Output: TrueExplanation: 12 is a sublime number because
6 min read
Check Whether a number is Duck Number or not
A Duck number is a positive number which has zeroes present in it, For example 3210, 8050896, 70709 are all Duck numbers. Please note that a numbers with only leading 0s is not considered as Duck Number. For example, numbers like 035 or 0012 are not considered as Duck Numbers. A number like 01203 is
8 min read
Check whether a given number is an ugly number or not
Given an integer N, the task is to find out whether the given number is an Ugly number or not . Ugly numbers are numbers whose only prime factors are 2, 3 or 5. Examples: Input: N = 14 Output: No Explanation: 14 is not ugly since it includes another prime factor 7. Input: N = 6 Output: Yes Explanati
9 min read
Check whether a number is semiprime or not
Given a positive integer n. Find whether a number is a semiprime or not. Print True if number is semiprime else False. A semiprime is a natural number that is a product of two prime numbers.Examples : Input: 6Output: TrueExplanation6 is a semiprime number as it is aproduct of two prime numbers 2 and
11 min read
Check whether the given number is Euclid Number or not
Given a positive integer n, the task is to check if it is Euclid Number or not. Print âYESâ if the given number is Euclid Number otherwise print âNO'. Euclid number : In Mathematics, Euclid numbers are integers of the form - E_{n} = p_{n}\# + 1 where p_{n}\# is product of first n prime numbers.The f
15 min read
Check whether a number is Good prime or not
Given a positive integer N, the task is to check whether the given number is good prime or not. If the given number is good prime print âYESâ Otherwise Print âNOâ. Good Prime: In Mathematics, a good prime is a prime number whose square is greater than the product of any two primes at the same number
8 min read
Check whether a given number N is a Nude Number or not
Given an integer N, the task is to check whether N is a Nude number or not. A Nude number is a number that is divisible by all of its digits (which should be nonzero). Example: Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output
4 min read