📜  柯尔松数

📅  最后修改于: 2021-04-21 23:48:30             🧑  作者: Mango

给定一个整数N ,检查给定的数字是否为Curzon数

例子:

方法:方法是计算并检查2 N + 1是否可被2 * N + 1整除。

  • 首先找到2 * N +1的值
  • 然后找到2 N +1的值
  • 检查第二个值是否可以被第一个值整除,然后是一个Curzon Number ,否则不可以。

下面是上述方法的实现:

C++
// C++ implementation of the approach
 
#include 
using namespace std;
 
// Function to check if a number
// is a Curzon number or not
void checkIfCurzonNumber(int N)
{
 
    long int powerTerm, productTerm;
 
    // Find 2^N + 1
    powerTerm = pow(2, N) + 1;
 
    // Find 2*N + 1
    productTerm = 2 * N + 1;
 
    // Check for divisibility
    if (powerTerm % productTerm == 0)
        cout << "Yes\n";
    else
        cout << "No\n";
}
 
// Driver code
int main()
{
    long int N = 5;
    checkIfCurzonNumber(N);
 
    N = 10;
    checkIfCurzonNumber(N);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to check if a number
// is a Curzon number or not
static void checkIfCurzonNumber(long N)
{
    double powerTerm, productTerm;
 
    // Find 2^N + 1
    powerTerm = Math.pow(2, N) + 1;
 
    // Find 2*N + 1
    productTerm = 2 * N + 1;
 
    // Check for divisibility
    if (powerTerm % productTerm == 0)
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver code
public static void main(String[] args)
{
    long N = 5;
    checkIfCurzonNumber(N);
     
    N = 10;
    checkIfCurzonNumber(N);
}
}
 
// This code is contributed by coder001


Python3
# Python3 implementation of the approach
 
# Function to check if a number
# is a Curzon number or not
def checkIfCurzonNumber(N):
 
    powerTerm, productTerm = 0, 0
 
    # Find 2^N + 1
    powerTerm = pow(2, N) + 1
 
    # Find 2*N + 1
    productTerm = 2 * N + 1
 
    # Check for divisibility
    if (powerTerm % productTerm == 0):
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__ == '__main__':
     
    N = 5
    checkIfCurzonNumber(N)
 
    N = 10
    checkIfCurzonNumber(N)
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG{
     
// Function to check if a number
// is a curzon number or not
static void checkIfCurzonNumber(long N)
{
    double powerTerm, productTerm;
 
    // Find 2^N + 1
    powerTerm = Math.Pow(2, N) + 1;
 
    // Find 2*N + 1
    productTerm = 2 * N + 1;
 
    // Check for divisibility
    if (powerTerm % productTerm == 0)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver code
static public void Main ()
{
    long N = 5;
    checkIfCurzonNumber(N);
     
    N = 10;
    checkIfCurzonNumber(N);
}
}
 
// This code is contributed by shubhamsingh10


Javascript


输出:
Yes
No

时间复杂度: O(log N)