Sum of the digits of square of the given number which has only 1’s as its digits
Given a number represented as string str consisting of the digit 1 only i.e. 1, 11, 111, …. The task is to find the sum of digits of the square of the given number.
Examples:
Input: str = 11
Output: 4
112 = 121
1 + 2 + 1 = 4
Input: str = 1111
Output: 16
Naive approach: Find the square of the given number and then find the sum of its digits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int squareDigitSum(string number)
{
int summ = 0;
int num = stoi(number);
int squareNum = num * num;
while (squareNum > 0)
{
summ = summ + (squareNum % 10);
squareNum = squareNum / 10;
}
return summ;
}
int main()
{
string N = "1111" ;
cout << squareDigitSum(N);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int squareDigitSum(String number)
{
int summ = 0 ;
int num = Integer.parseInt(number);
int squareNum = num * num;
while (squareNum > 0 )
{
summ = summ + (squareNum % 10 );
squareNum = squareNum / 10 ;
}
return summ;
}
public static void main (String[] args)
{
String N = "1111" ;
System.out.println(squareDigitSum(N));
}
}
|
Python3
def squareDigitSum(num):
summ = 0
num = int (num)
squareNum = num * num
while squareNum > 0 :
summ = summ + (squareNum % 10 )
squareNum = squareNum / / 10
return summ
if __name__ = = "__main__" :
N = "1111"
print (squareDigitSum(N))
|
C#
using System;
class GFG
{
static int squareDigitSum(String number)
{
int summ = 0;
int num = int .Parse(number);
int squareNum = num * num;
while (squareNum > 0)
{
summ = summ + (squareNum % 10);
squareNum = squareNum / 10;
}
return summ;
}
public static void Main (String[] args)
{
String s = "1111" ;
Console.WriteLine(squareDigitSum(s));
}
}
|
Javascript
<script>
function squareDigitSum(number)
{
var summ = 0;
var num = parseInt(number);
var squareNum = num * num;
while (squareNum > 0)
{
summ = summ + (squareNum % 10);
squareNum = parseInt(squareNum / 10);
}
return summ;
}
var N = "1111" ;
document.write(squareDigitSum(N));
</script>
|
Time complexity: O(log10n), where n is no of digits in the given number
Auxiliary Space: O(1)
Efficient approach: It can be observed that in the square of the given number, the sequence [1, 2, 3, 4, 5, 6, 7, 9, 0] repeats in the left part and the sequence [0, 9, 8, 7, 6, 5, 4, 3, 2, 1] repeats in the right part. Both of these sequences appear floor(length(str) / 9) times and the sum of both of these sequences is 81 and the square of the number adds an extra 1 in the end.
So, the sum of all these would be [floor(length(str) / 9)] * 81 + 1.
And the middle digits have a sequence such as if length(str) % 9 = a then middle sequence is [1, 2, 3….a, a – 1, a – 2, … 2]. Now, it can be observed that sum of this part [1, 2, 3….a] is equal to (a * (a + 1)) / 2 and sum of the other part [a – 1, a – 2, … 2] is ((a * (a – 1)) / 2) – 1.
Total sum = floor(length(str) / 9) * 81 + 1 + (length(str) % 9)2 – 1 = floor(length(str) / 9) * 81 + (length(str) % 9)2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
lli squareDigitSum(string s)
{
lli lengthN = s.length();
lli result = (lengthN / 9) * 81
+ pow ((lengthN % 9), 2);
return result;
}
int main()
{
string s = "1111" ;
cout << squareDigitSum(s);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long squareDigitSum(String s)
{
long lengthN = s.length();
long result = (lengthN / 9 ) * 81 +
( long )Math.pow((lengthN % 9 ), 2 );
return result;
}
public static void main (String[] args)
{
String s = "1111" ;
System.out.println(squareDigitSum(s));
}
}
|
Python3
def squareDigitSum(num):
lengthN = len (num)
result = (lengthN / / 9 ) * 81 + (lengthN % 9 ) * * 2
return result
if __name__ = = "__main__" :
N = "1111"
print (squareDigitSum(N))
|
C#
using System;
class GFG
{
static long squareDigitSum(String s)
{
long lengthN = s.Length;
long result = (lengthN / 9) * 81 +
( long )Math.Pow((lengthN % 9), 2);
return result;
}
public static void Main (String[] args)
{
String s = "1111" ;
Console.WriteLine(squareDigitSum(s));
}
}
|
Javascript
<script>
function squareDigitSum(s)
{
let lengthN = s.length;
let result = parseInt(lengthN / 9) * 81
+ Math.pow((lengthN % 9), 2);
return result;
}
let s = "1111" ;
document.write(squareDigitSum(s));
</script>
|
Time Complexity O(1)
Auxiliary Space: O(1)
Last Updated :
16 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...