给定数字N,您需要找到一个N的除数,使得该除数的数字根在N的所有其他除数中最大。如果一个以上的除数给出相同的最大数字根,则输出最大的除数。
非负数的数字根可以通过重复累加数字的位数来获得,直到我们达到一位数为止。示例:DigitalRoot(98)= 9 + 8 => 17 => 1 + 7 => 8(一位数字!)。任务是打印具有最大数字根的最大除数,然后打印该除数的空格和数字根。
例子:
Input: N = 10
Output: 5 5
The divisors of 10 are: 1, 2, 5, 10. The Digital Roots of these divisors are as follows:
1=>1, 2=>2, 5=>5, 10=>1. The greatest Digital Root is 5 which is produced by divisor 5, so answer is 5 5
Input: N = 18
Output: 18 9
The divisors of 18 are: 1, 2, 3, 6, 9, 18. The Digital Roots of these divisors are as follows:
1=>1, 2=>2, 3=>3, 6=>6, 9=>9, 18=>9. As we can see that 9 and 18 both have the greatest Digital Root of 9. So we select the maximum of those divisors, that is Max(9, 18)=18. So answer is 18 9
天真的方法是迭代到N并找到所有因子及其数字和。存储其中最大的并打印。
时间复杂度: O(N)
一种有效的方法是循环直到sqrt(N) ,然后因子为i和n / i。检查其中最大的数字总和,如果数字总和相似,则存储较大的因数。迭代完成后,打印它们。
下面是上述方法的实现。
C++
// C++ program to print the
// digital roots of a number
#include
using namespace std;
// Function to return
// dig-sum
int summ(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Function to print the Digital Roots
void printDigitalRoot(int n)
{
// store the largest digital roots
int maxi = 1;
int dig = 1;
// Iterate till sqrt(n)
for (int i = 1; i <= sqrt(n); i++) {
// if i is a factor
if (n % i == 0) {
// get the digit sum of both
// factors i and n/i
int d1 = summ(n / i);
int d2 = summ(i);
// if digit sum is greater
// then previous maximum
if (d1 > maxi) {
dig = n / i;
maxi = d1;
}
// if digit sum is greater
// then previous maximum
if (d2 > maxi) {
dig = i;
maxi = d2;
}
// if digit sum is same as
// then previous maximum, then
// check for larger divisor
if (d1 == maxi) {
if (dig < (n / i)) {
dig = n / i;
maxi = d1;
}
}
// if digit sum is same as
// then previous maximum, then
// check for larger divisor
if (d2 == maxi) {
if (dig < i) {
dig = i;
maxi = d2;
}
}
}
}
// Print the digital roots
cout << dig << " " << maxi << endl;
}
// Driver Code
int main()
{
int n = 10;
// Function call to print digital roots
printDigitalRoot(n);
return 0;
}
Java
// Java program to print the digital
// roots of a number
class GFG
{
// Function to return dig-sum
static int summ(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Function to print the Digital Roots
static void printDigitalRoot(int n)
{
// store the largest digital roots
int maxi = 1;
int dig = 1;
// Iterate till sqrt(n)
for (int i = 1; i <= Math.sqrt(n); i++)
{
// if i is a factor
if (n % i == 0)
{
// get the digit sum of both
// factors i and n/i
int d1 = summ(n / i);
int d2 = summ(i);
// if digit sum is greater
// then previous maximum
if (d1 > maxi)
{
dig = n / i;
maxi = d1;
}
// if digit sum is greater
// then previous maximum
if (d2 > maxi)
{
dig = i;
maxi = d2;
}
// if digit sum is same as
// then previous maximum, then
// check for larger divisor
if (d1 == maxi)
{
if (dig < (n / i))
{
dig = n / i;
maxi = d1;
}
}
// if digit sum is same as
// then previous maximum, then
// check for larger divisor
if (d2 == maxi)
{
if (dig < i)
{
dig = i;
maxi = d2;
}
}
}
}
// Print the digital roots
System.out.println(dig + " " + maxi);
}
// Driver Code
public static void main(String[] args)
{
int n = 10;
// Function call to print digital roots
printDigitalRoot(n);
}
}
// This code is contributed by mits
Python3
# Python3 program to print the digital
# roots of a number
# Function to return dig-sum
def summ(n):
if (n == 0):
return 0;
if(n % 9 == 0):
return 9;
else:
return (n % 9);
# Function to print the Digital Roots
def printDigitalRoot(n):
# store the largest digital roots
maxi = 1;
dig = 1;
# Iterate till sqrt(n)
for i in range(1, int(pow(n, 1/2) + 1)):
# if i is a factor
if (n % i == 0):
# get the digit sum of both
# factors i and n/i
d1 = summ(n / i);
d2 = summ(i);
# if digit sum is greater
# then previous maximum
if (d1 > maxi):
dig = n / i;
maxi = d1;
# if digit sum is greater
# then previous maximum
if (d2 > maxi):
dig = i;
maxi = d2;
# if digit sum is same as
# then previous maximum, then
# check for larger divisor
if (d1 == maxi):
if (dig < (n / i)):
dig = n / i;
maxi = d1;
# if digit sum is same as
# then previous maximum, then
# check for larger divisor
if (d2 == maxi):
if (dig < i):
dig = i;
maxi = d2;
# Print the digital roots
print(int(dig), " ", int(maxi));
# Driver Code
if __name__ == '__main__':
n = 10;
# Function call to prdigital roots
printDigitalRoot(n);
# This code is contributed by 29AjayKumar
C#
// C# program to print the digital
// roots of a number
using System;
class GFG
{
// Function to return dig-sum
static int summ(int n)
{
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
// Function to print the Digital Roots
static void printDigitalRoot(int n)
{
// store the largest digital roots
int maxi = 1;
int dig = 1;
// Iterate till sqrt(n)
for (int i = 1; i <= Math.Sqrt(n); i++)
{
// if i is a factor
if (n % i == 0)
{
// get the digit sum of both
// factors i and n/i
int d1 = summ(n / i);
int d2 = summ(i);
// if digit sum is greater
// then previous maximum
if (d1 > maxi)
{
dig = n / i;
maxi = d1;
}
// if digit sum is greater
// then previous maximum
if (d2 > maxi)
{
dig = i;
maxi = d2;
}
// if digit sum is same as
// then previous maximum, then
// check for larger divisor
if (d1 == maxi)
{
if (dig < (n / i))
{
dig = n / i;
maxi = d1;
}
}
// if digit sum is same as
// then previous maximum, then
// check for larger divisor
if (d2 == maxi)
{
if (dig < i)
{
dig = i;
maxi = d2;
}
}
}
}
// Print the digital roots
Console.WriteLine(dig + " " + maxi);
}
// Driver Code
public static void Main()
{
int n = 10;
// Function call to print digital roots
printDigitalRoot(n);
}
}
// This code is contributed
// by Akanksha Rai
5 5
时间复杂度: O(sqrt(N))