Sunday, November 17, 2024
Google search engine

D Numbers

D Number is a number N > 3 such that N divides k^(n-2)-k for all k with gcd(k, n) = 1, 1<k<n.
 

9, 15, 21, 33, 39, 51, 57, 63, 69, 87, 93…. 
 

 

Check if a number is a D-Number

Given a number N, the task is to check if N is an D Number or not. If N is an D Number then print “Yes” else print “No”.
Examples: 
 

Input: N = 9 
Output: Yes 
Explanation: 
9 is a D-number since it divides all the numbers 
2^7-2, 4^7-4, 5^7-5, 7^7-7 and 8^7-8, and 
2, 4, 5, 7, 8 are relatively prime to n.
Input: N = 16 
Output: No 
 

 

Approach: Since D Number is a number N > 3 such that N divides k^(n-2)-k for all k with gcd(k, n) = 1, 1<k<n. So in a loop of k from 2 to n-1, we will check if gcd of N and k is 1 or not.If it’s 1 then we will check if k^(n-2)-k is divisible by N or not.If not divisible we will return false.At last we will return true.
Below is the implementation of the above approach: 
 

C++




// C++ implementation
// for the above approach
#include<bits/stdc++.h>
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




<script>
// Javascript implementation for the
// above approach
 
 
    // Function to find the N-th
    // icosikaipentagon number
    function isDNum( n) {
 
        // Number should be
        // greater than 3
        if (n < 4)
            return false;
 
        let numerator = 0, hcf = 0;
 
        // Check every k in range 2 to n-1
        for ( k = 2; k <= n; k++) {
            numerator = parseInt( (Math.pow(k, n - 2) - k));
            hcf = __gcd(n, k);
        }
 
        // Condition for D-Number
        if (hcf == 1 && (numerator % n) != 0)
            return false;
 
        return true;
    }
 
    function __gcd( a, b) {
        return b == 0 ? a : __gcd(b, a % b);
    }
 
    // Driver Code
      
        let n = 15;
        let a = isDNum(n);
 
        if (a)
            document.write("Yes");
        else
            document.write("No");
 
 
// This code contributed by Rajput-Ji
 
</script>


Output: 

Yes

 

Time Complexity: O(1) 
Reference: OEIS
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
23 Mar, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments