Curzon Numbers
Last Updated :
05 Nov, 2021
Given an integer N, check whether the given number is a Curzon Number or not.
A number N is said to be a Curzon Number if 2N + 1 is divisible by 2*N + 1.
Example:
Input: N = 5
Output: Yes
Explanation:
2^5 + 1 = 33 and 2*5 + 1 = 11
Since 11 divides 33, so 5 is a curzon number.
Input: N = 10
Output: No
Explanation:
2^10 + 1 = 1025 and 2*10 + 1 = 21
1025 is not divisible by 21, so 10 is not a curzon number.
Approach: The approach is to compute and check if 2N + 1 is divisible by 2*N + 1 or not.
- First find the value of 2*N + 1
- Then find the value of 2N + 1
- Check if the second value is divisible by the first value, then it is a Curzon Number, else not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkIfCurzonNumber( int N)
{
long int powerTerm, productTerm;
powerTerm = pow (2, N) + 1;
productTerm = 2 * N + 1;
if (powerTerm % productTerm == 0)
cout << "Yes\n" ;
else
cout << "No\n" ;
}
int main()
{
long int N = 5;
checkIfCurzonNumber(N);
N = 10;
checkIfCurzonNumber(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void checkIfCurzonNumber( long N)
{
double powerTerm, productTerm;
powerTerm = Math.pow( 2 , N) + 1 ;
productTerm = 2 * N + 1 ;
if (powerTerm % productTerm == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
long N = 5 ;
checkIfCurzonNumber(N);
N = 10 ;
checkIfCurzonNumber(N);
}
}
|
Python3
def checkIfCurzonNumber(N):
powerTerm, productTerm = 0 , 0
powerTerm = pow ( 2 , N) + 1
productTerm = 2 * N + 1
if (powerTerm % productTerm = = 0 ):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
N = 5
checkIfCurzonNumber(N)
N = 10
checkIfCurzonNumber(N)
|
C#
using System;
class GFG{
static void checkIfCurzonNumber( long N)
{
double powerTerm, productTerm;
powerTerm = Math.Pow(2, N) + 1;
productTerm = 2 * N + 1;
if (powerTerm % productTerm == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static public void Main ()
{
long N = 5;
checkIfCurzonNumber(N);
N = 10;
checkIfCurzonNumber(N);
}
}
|
Javascript
<script>
function checkIfCurzonNumber(N)
{
var powerTerm, productTerm;
powerTerm = Math.pow(2, N) + 1;
productTerm = 2 * N + 1;
if (powerTerm % productTerm == 0)
{
document.write( "Yes" + "</br>" );
}
else
{
document.write( "No" );
}
}
var N = 5;
checkIfCurzonNumber(N);
N = 10;
checkIfCurzonNumber(N);
</script>
|
Time complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...