📜  D号码

📅  最后修改于: 2021-04-22 08:21:26             🧑  作者: Mango

D Number是一个N > 3的数字,这样对于gcd(k,n)= 1,1

检查号码是否为D号码

给定数字N ,任务是检查N是否为D数字。如果N是D号,则打印“是”,否则打印“否”
例子:

方法:由于D Number是一个N > 3的数字,因此对于gcd(k,n)= 1,1 下面是上述方法的实现:

C++
// C++ implementation
// for the above approach
#include
using namespace std;
 
// Function to find the N-th
// icosikaipentagon number
int isDNum(int n)
{
    // number should be
    // greater than 3
    if (n < 4)
        return false;
     
    int numerator, hcf;
     
    // Check every k in range 2 to n-1
    for (int k = 2; k <= n; k++)
    {
        numerator = pow(k, n - 2) - k;
        hcf = __gcd(n, k);
    }
     
    // condition for D-Number
    if (hcf == 1 && (numerator % n) != 0)
        return false;
     
    return true;
}
 
// Driver Code
int main()
{
    int n = 15;
    int a = isDNum(n);
    if (a)
        cout << "Yes";
    else
        cout << "No";
}
 
// This code is contributed by Ritik Bansal


Java
// Java implementation for the
// above approach
import java.util.*;
 
class GFG{
 
// Function to find the N-th
// icosikaipentagon number
static boolean isDNum(int n)
{
     
    // Number should be
    // greater than 3
    if (n < 4)
        return false;
     
    int numerator = 0, hcf = 0;
     
    // Check every k in range 2 to n-1
    for(int k = 2; k <= n; k++)
    {
       numerator = (int)(Math.pow(k, n - 2) - k);
       hcf = __gcd(n, k);
    }
     
    // Condition for D-Number
    if (hcf == 1 && (numerator % n) != 0)
        return false;
     
    return true;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 15;
    boolean a = isDNum(n);
     
    if (a)
        System.out.print("Yes");
    else
        System.out.print("No");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 implementation
# for the above approach
 
import math
   
# Function to find the N-th
# icosikaipentagon number
def isDNum(n):
    # number should be
        # greater than 3
    if n < 4:
        return False
 
    # Check every k in range 2 to n-1
    for k in range(2, n):
        numerator = pow(k, n - 2) - k
        hcf = math.gcd(n, k)
 
        # condition for D-Number
        if(hcf ==1 and (numerator % n) != 0):
            return False
    return True
 
# Driver code
n = 15
if isDNum(n):
    print("Yes")
else:
    print("No")


C#
// C# implementation for the
// above approach
using System;
class GFG{
 
// Function to find the N-th
// icosikaipentagon number
static bool isDNum(int n)
{
     
    // Number should be
    // greater than 3
    if (n < 4)
        return false;
     
    int numerator = 0, hcf = 0;
     
    // Check every k in range 2 to n-1
    for(int k = 2; k <= n; k++)
    {
        numerator = (int)(Math.Pow(k, n - 2) - k);
        hcf = __gcd(n, k);
    }
     
    // Condition for D-Number
    if (hcf == 1 && (numerator % n) != 0)
        return false;
     
    return true;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 15;
    bool a = isDNum(n);
     
    if (a)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code contributed by Princi Singh


Javascript


输出:
Yes

时间复杂度: O(1)
参考:OEIS