Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
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.
Examples:
Input: N = 15
Output: 9
Explanation:
152 = 225, 2+2+5 = 9
Input: N = 9
Output: 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:
C++
#include <bits/stdc++.h>
using namespace std;
int digitalRootofN(string num)
{
if (num.compare( "0" ) == 0)
return 0;
int ans = 0;
for ( int i = 0; i < num.length(); i++)
ans = (ans + num[i] - '0' ) % 9;
return (ans == 0) ? 9 : ans % 9;
}
int digitalRootofNSquare(string N)
{
int NDigRoot = digitalRootofN(N);
if (NDigRoot == 1 || NDigRoot == 8)
return 1;
if (NDigRoot == 2 || NDigRoot == 7)
return 4;
if (NDigRoot == 3 || NDigRoot == 6)
return 9;
if (NDigRoot == 4 || NDigRoot == 5)
return 7;
}
int main()
{
string num = "15" ;
cout << digitalRootofNSquare(num);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int digitalRootofN(String num)
{
if (num.compareTo( "0" ) == 0 )
return 0 ;
int ans = 0 ;
for ( int i = 0 ; i < num.length(); i++)
ans = (ans + num.charAt(i) - '0' ) % 9 ;
return (ans == 0 ) ? 9 : ans % 9 ;
}
static int digitalRootofNSquare(String N)
{
int NDigRoot = digitalRootofN(N);
if (NDigRoot == 1 || NDigRoot == 8 )
return 1 ;
else if (NDigRoot == 2 || NDigRoot == 7 )
return 4 ;
else if (NDigRoot == 3 || NDigRoot == 6 )
return 9 ;
else
return 7 ;
}
public static void main (String[] args)
{
String num = "15" ;
System.out.print(digitalRootofNSquare(num));
}
}
|
Python3
def digitalRootofN(num):
if (num = = ( "0" )):
return 0 ;
ans = 0 ;
for i in range ( 0 , len (num)):
ans = (ans + ord (num[i]) - ord ( '0' )) % 9 ;
return 9 if (ans = = 0 ) else ans % 9 ;
def digitalRootofNSquare(N):
NDigRoot = digitalRootofN(N);
if (NDigRoot = = 1 or NDigRoot = = 8 ):
return 1 ;
elif (NDigRoot = = 2 or NDigRoot = = 7 ):
return 4 ;
elif (NDigRoot = = 3 or NDigRoot = = 6 ):
return 9 ;
else :
return 7 ;
if __name__ = = '__main__' :
num = "15" ;
print (digitalRootofNSquare(num));
|
C#
using System;
class GFG{
static int digitalRootofN( string num)
{
if (num.CompareTo( "0" ) == 0)
return 0;
int ans = 0;
for ( int i = 0; i < num.Length; i++)
ans = (ans + num[i] - '0' ) % 9;
return (ans == 0) ? 9 : ans % 9;
}
static int digitalRootofNSquare( string N)
{
int NDigRoot = digitalRootofN(N);
if (NDigRoot == 1 || NDigRoot == 8)
return 1;
else if (NDigRoot == 2 || NDigRoot == 7)
return 4;
else if (NDigRoot == 3 || NDigRoot == 6)
return 9;
else
return 7;
}
public static void Main ()
{
string num = "15" ;
Console.Write(digitalRootofNSquare(num));
}
}
|
Javascript
<script>
function digitalRootofN(num)
{
if (num == "0" )
return 0;
var ans = 0;
for ( var i = 0; i < num.length; i++)
ans = (ans + num[i] - '0' ) % 9;
return (ans == 0) ? 9 : ans % 9;
}
function digitalRootofNSquare(N)
{
var NDigRoot = digitalRootofN(N);
if (NDigRoot == 1 || NDigRoot == 8)
return 1;
if (NDigRoot == 2 || NDigRoot == 7)
return 4;
if (NDigRoot == 3 || NDigRoot == 6)
return 9;
if (NDigRoot == 4 || NDigRoot == 5)
return 7;
}
var num = "15" ;
document.write( digitalRootofNSquare(num));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
20 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...