📜  使用给定整数的数字根的整数平方的数字根(重复的数字和)

📅  最后修改于: 2021-05-04 22:00:07             🧑  作者: Mango

给定一个整数N,任务是找到数字根N 2使用N的数字根。

例子:

方法:这个想法是找到N的数字根。现在我们可以使用N的数字根通过观察以下点找到N 2的数字根:

  • 如果N的数字根为1或8,则N 2的数字根始终为1;否则, N 2的数字根始终为1。
  • 如果N的数字根为2或7,则N 2的数字根始终为4;否则, N 2的数字根始终为4。
  • 如果N的数字根为3或6或9,则N 2的数字根始终为9;否则, N 2的数字根始终为9。
  • 如果N的数字根为4或5,则N 2的数字根始终为7;否则, N 2的数字根始终为7。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
#include 
using namespace std;
 
// Function to find the digital
// root of the number
int digitalRootofN(string num)
{
    // If num is 0
    if (num.compare("0") == 0)
        return 0;
 
    // Count sum of digits under mod 9
    int ans = 0;
    for (int i = 0; i < num.length(); i++)
        ans = (ans + num[i] - '0') % 9;
 
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
 
// Returns digital root of N * N
int digitalRootofNSquare(string N)
{
    // finding digital root of N
    int NDigRoot = digitalRootofN(N);
 
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
 
    if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
 
    if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
 
    if (NDigRoot == 4 || NDigRoot == 5)
        return 7;
}
 
// Driver Code
int main()
{
    string num = "15";
    cout << digitalRootofNSquare(num);
 
    return 0;
}


Java
// Java implementation of the
// above approach
import java.io.*;
 
class GFG{
  
// Function to find the digital
// root of the number
static int digitalRootofN(String num)
{
     
    // If num is 0
    if (num.compareTo("0") == 0)
        return 0;
  
    // Count sum of digits under mod 9
    int ans = 0;
    for(int i = 0; i < num.length(); i++)
        ans = (ans + num.charAt(i) - '0') % 9;
  
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
  
// Returns digital root of N * N
static int digitalRootofNSquare(String N)
{
     
    // Finding digital root of N
    int NDigRoot = digitalRootofN(N);
  
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
  
    else if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
  
    else if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
    else
        return 7;
}
 
// Driver Code
public static void main (String[] args)
{
    String num = "15";
     
    // Function call
    System.out.print(digitalRootofNSquare(num));
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 implementation of the
# above approach
 
# Function to find the digital
# root of the number
def digitalRootofN(num):
 
    # If num is 0
    if (num == ("0")):
        return 0;
 
    # Count sum of digits
    # under mod 9
    ans = 0;
    for i in range(0, len(num)):
        ans = (ans + ord(num[i]) - ord('0')) % 9;
 
    # If digit sum is multiple of 9,
    # 9, else remainder with 9.
    return 9 if (ans == 0) else ans % 9;
 
# Returns digital root of N * N
def digitalRootofNSquare(N):
 
    # Finding digital root of N
    NDigRoot = digitalRootofN(N);
    if (NDigRoot == 1 or NDigRoot == 8):
        return 1;
    elif(NDigRoot == 2 or NDigRoot == 7):
        return 4;
    elif(NDigRoot == 3 or NDigRoot == 6):
        return 9;
    else:
        return 7;
 
# Driver Code
if __name__ == '__main__':
   
    num = "15";
 
    # Function call
    print(digitalRootofNSquare(num));
 
# This code is contributed by shikhasingrajput


C#
// C# implementation of the
// above approach
using System;
 
class GFG{
  
// Function to find the digital
// root of the number
static int digitalRootofN(string num)
{
     
    // If num is 0
    if (num.CompareTo("0") == 0)
        return 0;
  
    // Count sum of digits under mod 9
    int ans = 0;
    for(int i = 0; i < num.Length; i++)
        ans = (ans + num[i] - '0') % 9;
  
    // If digit sum is multiple of 9,
    // 9, else remainder with 9.
    return (ans == 0) ? 9 : ans % 9;
}
  
// Returns digital root of N * N
static int digitalRootofNSquare(string N)
{
     
    // Finding digital root of N
    int NDigRoot = digitalRootofN(N);
  
    if (NDigRoot == 1 || NDigRoot == 8)
        return 1;
  
    else if (NDigRoot == 2 || NDigRoot == 7)
        return 4;
  
    else if (NDigRoot == 3 || NDigRoot == 6)
        return 9;
  
    else
        return 7;
}
 
// Driver Code
public static void Main ()
{
    string num = "15";
     
    // Function call
    Console.Write(digitalRootofNSquare(num));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
9

时间复杂度: O(N)
辅助空间: O(1)