Given two positive number N and X. The task is to find the sum of digits of a number formed by N repeating X number of times until sum become single digit.
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
As discussed in this post, recursive sum of digits is 9 if number is multiple of 9, else n % 9. Since divisibility and modular arithmetic are compatible with multiplication, we simply find result for single occurrence, multiply result with x and again find the result.
How does this work ?
Lets N = 24 and X = 3.
So, sumUntilSingle(N) = 2 + 4 = 6.
Multiplying 6 by 3 = 18
sumUntilSingle(18) = 9.
Below is the implemenatation of this approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Remove repeated digits in a given number
- Count ways to spell a number with repeated digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Recursive sum of digits of a number is prime or not
- Find M-th number whose repeated sum of digits of a number is N
- Check if a number is magic (Recursive sum of digits is 1)
- Find maximum number that can be formed using digits of a given number
- Greatest number less than equal to B that can be formed from the digits of A
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Find Nth even length palindromic number formed using digits X and Y
- Find the largest number that can be formed by changing at most K digits
- Find all strings formed from characters mapped to digits of a number
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Total numbers with no repeated digits in a range
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m