Related Articles

# Sum of the digits of square of the given number which has only 1’s as its digits

• Difficulty Level : Basic
• Last Updated : 03 May, 2021

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:
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++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the sum``// of the digits of num ^ 2``int` `squareDigitSum(string number)``{``    ``int` `summ = 0;``    ``int` `num = stoi(number);``    ` `    ``// Store the square of num``    ``int` `squareNum = num * num;` `    ``// Find the sum of its digits``    ``while``(squareNum > 0)``    ``{``        ``summ = summ + (squareNum % 10);``        ``squareNum = squareNum / 10;``    ``}``    ``return` `summ;``}` `// Driver code``int` `main()``{``    ``string N = ``"1111"``;` `    ``cout << squareDigitSum(N);` `    ``return` `0;``}` `// This code is contributed by Princi Singh`

## Java

 `// Java implementation of the approach``// Java implementation of the approach``class` `GFG``{` `// Function to return the sum``// of the digits of num ^ 2``static` `int` `squareDigitSum(String number)``{``    ``int` `summ = ``0``;``    ``int` `num = Integer.parseInt(number);``    ` `    ``// Store the square of num``    ``int` `squareNum = num * num;` `    ``// Find the sum of its digits``    ``while``(squareNum > ``0``)``    ``{``        ``summ = summ + (squareNum % ``10``);``        ``squareNum = squareNum / ``10``;``    ``}``    ``return` `summ;``}` `// Driver code``public` `static` `void` `main (String[] args)``{``    ``String N = ``"1111"``;` `    ``System.out.println(squareDigitSum(N));``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 implementation of the approach` `# Function to return the sum``# of the digits of num ^ 2``def` `squareDigitSum(num):` `    ``summ ``=` `0``    ``num ``=` `int``(num)``    ` `    ``# Store the square of num``    ``squareNum ``=` `num ``*` `num` `    ``# Find the sum of its digits``    ``while` `squareNum > ``0``:``        ``summ ``=` `summ ``+` `(squareNum ``%` `10``)``        ``squareNum ``=` `squareNum``/``/``10` `    ``return` `summ``    ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `"1111"``    ``print``(squareDigitSum(N))`

## C#

 `// C# implementation of the approach``using` `System;` `class` `GFG``{``    ` `    ``// Function to return the sum``    ``// of the digits of num ^ 2``    ``static` `int` `squareDigitSum(String number)``    ``{``        ``int` `summ = 0;``        ``int` `num = ``int``.Parse(number);` `        ``// Store the square of num``        ``int` `squareNum = num * num;` `        ``// Find the sum of its digits``        ``while``(squareNum > 0)``        ``{``            ``summ = summ + (squareNum % 10);``            ``squareNum = squareNum / 10;``        ``}``        ``return` `summ;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `Main (String[] args)``    ``{``        ``String s = ``"1111"``;``    ` `        ``Console.WriteLine(squareDigitSum(s));``    ``}``}` `// This code is contributed by Princi Singh`

## Javascript

 ``
Output:
`16`

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++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `#define lli long long int` `// Function to return the sum``// of the digits of num^2``lli squareDigitSum(string s)``{``    ``// To store the number of 1's``    ``lli lengthN = s.length();` `    ``// Find the sum of the digits of num^2``    ``lli result = (lengthN / 9) * 81``                 ``+ ``pow``((lengthN % 9), 2);` `    ``return` `result;``}` `// Driver code``int` `main()``{``    ``string s = ``"1111"``;` `    ``cout << squareDigitSum(s);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``class` `GFG``{``    ` `    ``// Function to return the sum``    ``// of the digits of num^2``    ``static` `long` `squareDigitSum(String s)``    ``{``        ``// To store the number of 1's``        ``long` `lengthN = s.length();``    ` `        ``// Find the sum of the digits of num^2``        ``long` `result = (lengthN / ``9``) * ``81` `+``                      ``(``long``)Math.pow((lengthN % ``9``), ``2``);``    ` `        ``return` `result;``    ``}``    ` `    ``// Driver code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``String s = ``"1111"``;``    ` `        ``System.out.println(squareDigitSum(s));` `    ``}``}` `// This code is contributed by AnkitRai01`

## Python3

 `# Python3 implementation of the approach` `# Function to return the sum``# of the digits of num ^ 2``def` `squareDigitSum(num):` `    ``# To store the number of 1's``    ``lengthN ``=` `len``(num)` `    ``# Find the sum of the digits of num ^ 2``    ``result ``=` `(lengthN``/``/``9``)``*``81` `+` `(lengthN ``%` `9``)``*``*``2` `    ``return` `result` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``N ``=` `"1111"``    ``print``(squareDigitSum(N))`

## C#

 `// C# implementation of the approach``using` `System;``                    ` `class` `GFG``{``    ` `// Function to return the sum``// of the digits of num^2``static` `long` `squareDigitSum(String s)``{``    ``// To store the number of 1's``    ``long` `lengthN = s.Length;` `    ``// Find the sum of the digits of num^2``    ``long` `result = (lengthN / 9) * 81 +``                  ``(``long``)Math.Pow((lengthN % 9), 2);` `    ``return` `result;``}` `// Driver code``public` `static` `void` `Main (String[] args)``{``    ``String s = ``"1111"``;` `    ``Console.WriteLine(squareDigitSum(s));``}``}` `// This code is contributed by 29AjayKumar`

## Javascript

 ``
Output:
`16`

Time Complexity 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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up