Sum of the digits of square of the given number which has only 1's as its digits
Last Updated :
16 Dec, 2022
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
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
import java.io.*;
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
<script>
// Javascript implementation of the approach
// Function to return the sum
// of the digits of num ^ 2
function squareDigitSum(number)
{
var summ = 0;
var num = parseInt(number);
// Store the square of num
var squareNum = num * num;
// Find the sum of its digits
while (squareNum > 0)
{
summ = summ + (squareNum % 10);
squareNum = parseInt(squareNum / 10);
}
return summ;
}
// Driver code
var N = "1111";
document.write(squareDigitSum(N));
// This code is contributed by todaysgaurav
</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++
// C++ implementation of the approach
#include <bits/stdc++.h>
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
import java.io.*;
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
<script>
// Javascript implementation of the approach
// Function to return the sum
// of the digits of num^2
function squareDigitSum(s)
{
// To store the number of 1's
let lengthN = s.length;
// Find the sum of the digits of num^2
let result = parseInt(lengthN / 9) * 81
+ Math.pow((lengthN % 9), 2);
return result;
}
// Driver code
let s = "1111";
document.write(squareDigitSum(s));
</script>
Time Complexity O(1)
Auxiliary Space: O(1)
Similar Reads
Print numbers with digits 0 and 1 only such that their sum is N
Given a number N, the task is to find the required numbers consist of only 0 and 1 digit whose sum is equal to N. Example: Input: 9 Output: 1 1 1 1 1 1 1 1 1 Only numbers smaller than or equal to 9 with digits 0 and 1 only are 0 and 1 itself. So to get 9, we have to add 1 - 9 times. Input: 31 Output
6 min read
Find the last two missing digits of the given phone number
Given eight digits of a phone number as an integer N, the task is to find the missing last two digits and print the complete number when the last two digits are the sum of given eight digits.Examples: Input: N = 98765432 Output: 9876543244Input: N = 10000000 Output: 1000000001 Approach: Get the eigh
5 min read
Find N numbers such that a number and its reverse are divisible by sum of its digits
Given a number N, the task is to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits.Example: Input: N = 4 Output: 1 2 3 4 Explanation: The reverse of every single digit number is the same number. And, every number is divisible by itself.
9 min read
Sum of all N-digit palindromic numbers which doesn't contains 0 and are divisible by 9
Given a number N, the task is to find the sum of all N-digit palindromic numbers which are divisible by 9 and the number doesn't contain 0 in it.Note: The sum can be very large, take modulo with 109+7. Example: Input: N = 2 Output: 99 Explanation: There is only one 2-digit palindromic number divisib
9 min read
Find sum of Series with n-th term as n^2 - (n-1)^2
We are given an integer n and n-th term in a series as expressed below: Tn = n2 - (n-1)2 We need to find Sn mod (109 + 7), where Sn is the sum of all of the terms of the given series and, Sn = T1 + T2 + T3 + T4 + ...... + Tn Examples: Input : 229137999 Output : 218194447 Input : 344936985 Output : 7
3 min read
Minimum count of numbers required with unit digit X that sums up to N
Given two integers N and X, the task is to find the minimum count of integers with sum N and having unit digit X. If no such representation exists then print -1.Examples: Input: N = 38, X = 9 Output: 2 Explanation: Minimum two integers are required with unit digit as X to represent as a sum equal to
7 min read
Minimum N-Digit number required to obtain largest N-digit number after performing given operations
Given a positive integer N, the task is to find the minimum N-digit number such that performing the following operations on it in the following order results into the largest N-digit number: Convert the number to its Binary Coded Decimal form.Concatenate all the resulting nibbles to form a binary nu
5 min read
Sum of last digit of all integers from 1 to N divisible by M
Given two integer N and N. The task is to compute the sum of last digit of all integers from 1 to N that is divisible by M. Examples: Input: N = 12, M = 1 Output: 48 Number divisible by M = 1 from 1 to 12 is : {1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 + 2} = 48 Input: N = 100, M = 3 Output: 153 App
6 min read
Check if a number can be represented as a sum of a Prime Number and a Perfect Square
Given a positive integer N, the task is to check if N can be represented as a sum of a Prime Number and a Perfect Square or not. If it is possible to represent N in required form, then print "Yes". Otherwise, print "No". Examples: Input: N = 27Output: YesExplanation: 27 can be expressed as sum of 2
15+ min read
Find Nth positive number whose digital root is X
Given a number X ( 1<= X <= 9) and a positive number N, find the Nth positive number whose digital root is X.Digital Root: The digital root of a positive number is obtained by iteratively summing up the digits of a number. On each iteration, the number is replaced by the sum of its digit and t
9 min read