Given an integer N, the task is to find the digital root N2 using the digital root of N.
Digital Root of a positive integer is calculated by adding the digits of the integer. If the resultant value is a single digit, then that digit is the digital root. If the resultant value contains two or more digits, those digits are summed and the process is repeated until a single-digit is obtained.
Input: N = 15
152 = 225, 2+2+5 = 9
Input: N = 9
Approach: The idea is to find the Digital Root of N. Now we can find the digital root of N2 using the digital root of N by observing the below points :
- If the digital root of N is 1 or 8 then the digital root of N2 is always 1;
- If the digital root of N is 2 or 7 then the digital root of N2 is always 4;
- If the digital root of N is 3 or 6 or 9 then the digital root of N2 is always 9;
- If the digital root of N is 4 or 5 then the digital root of N2 is always 7;
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Digital Root (repeated digital sum) of the given large integer
- Square root of a number by Repeated Subtraction method
- Digital Root of a given large integer using Recursion
- Check if a number is perfect square without finding square root
- Square root of an integer
- Find square root of number upto given precision using binary search
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Square root of a number using log
- Square root of a number without using sqrt() function
- Floor square root without using sqrt() function : Recursive
- C program to find square root of a given number
- Count ways to obtain given sum by repeated throws of a dice
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Numbers in a Range with given Digital Root
- Count square and non-square numbers before n
- Find smallest perfect square number A such that N + A is also a perfect square number
- Check if a number is a perfect square having all its digits as a perfect square
- Babylonian method for square root
- Euler's criterion (Check if square root under modulo p exists)
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.