给定数字X(1 <= X <= 9)和正数N,找到数字根为X的第N个正数。
数字根:正数的数字根是通过对数字进行迭代求和而获得的。在每次迭代中,该数字都用其数字的总和替换,并且当该数字减少为一个数字时,迭代将停止。这个单位数字称为数字根。例如,数字根数65是2,因为6 + 5 = 11和1 +1 = 2。
例子:
Input : X = 3, N = 100
Output : 894
The N-th Number whose digit root is X is 894
Input : X = 7, N = 43
Output : 385
简单方法:简单的方法是从1开始计算每个数字的数字根,每当遇到一个数字根等于X的数字时,我们将计数器加1。当计数器为1时,我们将停止搜索。等于N
下面是上述方法的实现:
C++
// C++ program to find the N-th number whose
// digital root is X
#include
using namespace std;
// Function to find the digital root of
// a number
int findDigitalRoot(int num)
{
int sum = INT_MAX, tempNum = num;
while (sum >= 10) {
sum = 0;
while (tempNum > 0) {
sum += tempNum % 10;
tempNum /= 10;
}
tempNum = sum;
}
return sum;
}
// Function to find the Nth number with
// digital root as X
void findAnswer(int X, int N)
{
// Counter variable to keep the
// count of valid numbers
int counter = 0;
for (int i = 1; counter < N; ++i) {
// Find digital root
int digitalRoot = findDigitalRoot(i);
// Check if is required answer or not
if (digitalRoot == X) {
++counter;
}
// Print the answer if you have found it
// and breakout of the loop
if (counter == N) {
cout << i;
break;
}
}
}
// Driver Code
int main()
{
int X = 1, N = 3;
findAnswer(X, N);
return 0;
}
Java
// Java program to find the N-th number whose
// digital root is X
class GFG
{
// Function to find the digital root of
// a number
static int findDigitalRoot(int num)
{
int sum = Integer.MAX_VALUE, tempNum = num;
while (sum >= 10)
{
sum = 0;
while (tempNum > 0)
{
sum += tempNum % 10;
tempNum /= 10;
}
tempNum = sum;
}
return sum;
}
// Function to find the Nth number with
// digital root as X
static void findAnswer(int X, int N)
{
// Counter variable to keep the
// count of valid numbers
int counter = 0;
for (int i = 1; counter < N; ++i)
{
// Find digital root
int digitalRoot = findDigitalRoot(i);
// Check if is required answer or not
if (digitalRoot == X)
{
++counter;
}
// Print the answer if you have found it
// and breakout of the loop
if (counter == N)
{
System.out.print( i);
break;
}
}
}
// Driver Code
public static void main(String args[])
{
int X = 1, N = 3;
findAnswer(X, N);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find the N-th number whose
# digital root is X
import sys
# Function to find the digital root of
# a number
def findDigitalRoot(num):
sum = sys.maxsize;
tempNum = num;
while (sum >= 10):
sum = 0;
while (tempNum > 0):
sum += tempNum % 10;
tempNum //= 10;
tempNum = sum;
return sum;
# Function to find the Nth number with
# digital root as X
def findAnswer(X, N):
# Counter variable to keep the
# count of valid numbers
counter = 0;
i = 0;
while (counter < N):
i += 1;
# Find digital root
digitalRoot = findDigitalRoot(i);
# Check if is required answer or not
if (digitalRoot == X):
counter += 1;
# Print the answer if you have found it
# and breakout of the loop
if (counter == N):
print(i);
break;
# Driver Code
if __name__ == '__main__':
X = 1;
N = 3;
findAnswer(X, N);
# This code is contributed by 29AjayKumar
C#
// C# program to find the N-th number whose
// digital root is X
using System;
class GFG
{
// Function to find the digital root of
// a number
static int findDigitalRoot(int num)
{
int sum = int.MaxValue, tempNum = num;
while (sum >= 10)
{
sum = 0;
while (tempNum > 0)
{
sum += tempNum % 10;
tempNum /= 10;
}
tempNum = sum;
}
return sum;
}
// Function to find the Nth number with
// digital root as X
static void findAnswer(int X, int N)
{
// Counter variable to keep the
// count of valid numbers
int counter = 0;
for (int i = 1; counter < N; ++i)
{
// Find digital root
int digitalRoot = findDigitalRoot(i);
// Check if is required answer or not
if (digitalRoot == X)
{
++counter;
}
// Print the answer if you have found it
// and breakout of the loop
if (counter == N)
{
Console.Write( i);
break;
}
}
}
// Driver Code
public static void Main(String []args)
{
int X = 1, N = 3;
findAnswer(X, N);
}
}
// This code has been contributed by 29AjayKumar
PHP
= 10)
{
$sum = 0;
while ($tempNum > 0)
{
$sum += $tempNum % 10;
$tempNum /= 10;
}
$tempNum = $sum;
}
return $sum;
}
// Function to find the Nth number
// with digital root as X
function findAnswer($X, $N)
{
// Counter variable to keep the
// count of valid numbers
$counter = 0;
for ($i = 1; $counter < $N; ++$i)
{
// Find digital root
$digitalRoot = findDigitalRoot($i);
// Check if is required answer or not
if ($digitalRoot == $X)
{
++$counter;
}
// Print the answer if you have found
// it and breakout of the loop
if ($counter == $N)
{
echo( $i);
break;
}
}
}
// Driver Code
$X = 1; $N = 3;
findAnswer($X, $N);
// This code is contributed by Code_Mech.
C++
// C++ program to find the N-th number with
// digital root as X
#include
using namespace std;
// Function to find the N-th number with
// digital root as X
int findAnswer(int X, int N)
{
return (N - 1) * 9 + X;
}
// Driver Code
int main()
{
int X = 7, N = 43;
cout << findAnswer(X, N);
return 0;
}
Java
// Java program to find the N-th number with
// digital root as X
class GfG
{
// Function to find the N-th number with
// digital root as X
static int findAnswer(int X, int N)
{
return (N - 1) * 9 + X;
}
// Driver Code
public static void main(String[] args)
{
int X = 7, N = 43;
System.out.println(findAnswer(X, N));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to find the N-th
# number with digital root as X
# Function to find the N-th number
# with digital root as X
def findAnswer(X, N):
return (N - 1) * 9 + X;
# Driver Code
X = 7;
N = 43;
print(findAnswer(X, N));
# This code is contributed by mits
C#
// C# program to find the N-th number
// with digital root as X
using System;
class GFG
{
// Function to find the N-th
// number with digital root as X
static int findAnswer(int X, int N)
{
return (N - 1) * 9 + X;
}
// Driver Code
public static void Main()
{
int X = 7, N = 43;
Console.WriteLine(findAnswer(X, N));
}
}
// This code contributed by Ryuga
PHP
Javascript
输出:
19
高效方法:我们可以使用以下公式直接找到数字K的数字根:
digitalRoot(k) = (k - 1)mod 9 +1
由此我们可以找到数字根为K的第N个数字,
Nth number = (N - 1)*9 + K
下面是上述方法的实现:
C++
// C++ program to find the N-th number with
// digital root as X
#include
using namespace std;
// Function to find the N-th number with
// digital root as X
int findAnswer(int X, int N)
{
return (N - 1) * 9 + X;
}
// Driver Code
int main()
{
int X = 7, N = 43;
cout << findAnswer(X, N);
return 0;
}
Java
// Java program to find the N-th number with
// digital root as X
class GfG
{
// Function to find the N-th number with
// digital root as X
static int findAnswer(int X, int N)
{
return (N - 1) * 9 + X;
}
// Driver Code
public static void main(String[] args)
{
int X = 7, N = 43;
System.out.println(findAnswer(X, N));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to find the N-th
# number with digital root as X
# Function to find the N-th number
# with digital root as X
def findAnswer(X, N):
return (N - 1) * 9 + X;
# Driver Code
X = 7;
N = 43;
print(findAnswer(X, N));
# This code is contributed by mits
C#
// C# program to find the N-th number
// with digital root as X
using System;
class GFG
{
// Function to find the N-th
// number with digital root as X
static int findAnswer(int X, int N)
{
return (N - 1) * 9 + X;
}
// Driver Code
public static void Main()
{
int X = 7, N = 43;
Console.WriteLine(findAnswer(X, N));
}
}
// This code contributed by Ryuga
的PHP
Java脚本
输出:
385