📌  相关文章
📜  通过重复追加形成的数字的数字的递归总和

📅  最后修改于: 2021-04-29 16:05:13             🧑  作者: Mango

给定两个正数NX。任务是找到由N重复X次直到所得到的个位数所形成的数字的总和。

例子 :

Input : N = 24, X = 3
Output : 9
Number formed after repeating 24 three time = 242424
Sum = 2 + 4 + 2 + 4 + 2 + 4
    = 18
Sum is not the single digit, so finding 
the sum of digits of 18,
1 + 8 = 9

Input : N = 4, X = 4
Output : 7

如本文所讨论的,如果数字是9的倍数,则数字的递归总和为9,否则为n%9。由于除数和模运算与乘法兼容,我们只需找到一次出现的结果,将结果与x相乘,然后再次找到结果。

这是如何运作的 ?
令N = 24且X = 3。
因此,sumUntilSingle(N)= 2 + 4 = 6。
将6乘以3 = 18
sumUntilSingle(18)= 9。

以下是此方法的实现:

C++
// C++ program to find Sum of digits of a
// number formed by repeating a number X number of
// times until sum become single digit.
#include 
using namespace std;
 
// return single digit sum of a number.
int digSum(int n)
{
    if (n == 0)
        return 0;
    return (n % 9 == 0) ? 9 : (n % 9);
}
 
// Returns recursive sum of digits of a number
// formed by repeating a number X number of
// times until sum become single digit.
int repeatedNumberSum(int n, int x)
{
    int sum = x*digSum(n);
    return digSum(sum);
}
 
// Driver program
int main()
{
    int n = 24, x = 3;
    cout << repeatedNumberSum(n, x) << endl;
    return 0;
}


Java
// Java program to find Sum of digits of a
// number formed by repeating a number X number of
// times until sum become single digit.
 
class GFG {
     
    // return single digit sum of a number.
    static int digSum(int n)
    {
        if (n == 0)
            return 0;
        return (n % 9 == 0) ? 9 : (n % 9);
    }
     
    // Returns recursive sum of digits of a number
    // formed by repeating a number X number of
    // times until sum become single digit.
    static int repeatedNumberSum(int n, int x)
    {
        int sum = x * digSum(n);
        return digSum(sum);
    }
         
    // Driver program
    public static void main (String[] args)
    {
        int n = 24, x = 3;
        System.out.println(repeatedNumberSum(n, x));
    }
}
 
// This code is contributed by Ajit.


Python3
# Python program to find Sum of digits of a
# number formed by repeating a number X number
# of times until sum become single digit.
 
# Return single digit sum of a number
def digSum(n):
    if n == 0:
        return 0
    return (n % 9 == 0) and 9 or (n % 9)
     
# Returns recursive sum of digits of a number
# formed by repeating a number X number of
# times until sum become single digit.
def repeatedNumberSum(n, x):
    sum = x * digSum(n)
    return digSum(sum)
 
# Driver Code
n = 24; x = 3
print(repeatedNumberSum(n, x))
 
# This code is contributed by Ajit.


C#
// C# program to find Sum of digits of a
// number formed by repeating a number X
// number of times until sum becomes
// single digit.
using System;
 
public class GFG
{    
    // return single digit sum of a number.
    static int digSum(int n)
    {
        if (n == 0)
            return 0;
             
        return (n % 9 == 0) ? 9 : (n % 9);
    }
     
    // Returns recursive sum of digits of a
    // number formed by repeating a number X
    // number of times until sum become
    // single digit.
    static int repeatedNumberSum(int n, int x)
    {
        int sum = x * digSum(n);
        return digSum(sum);
    }
     
    // driver program
    public static void Main ()
    {
        int n = 24, x = 3;
        Console.Write( repeatedNumberSum(n, x));
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


输出 :

9