给定一个表示为仅由数字1组成的字符串str的数字,即1,11,111,… 。任务是找到给定数字平方的数字总和。
例子:
Input: str = 11
Output: 4
112 = 121
1 + 2 + 1 = 4
Input: str = 1111
Output: 16
天真的方法:找到给定数字的平方,然后找到其数字的总和。
下面是上述方法的实现:
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
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
输出:
16
有效方法:可以观察到,在给定数字的平方中,序列[1、2、3、4、5、6、7、9、0]在左侧重复,序列[0、9, [ 8、7、6、5、4、3、2、1 ]在右侧重复。这两个序列都出现了floor(length(str)/ 9)次,这两个序列的总和为81,数字的平方最后增加了1 。
因此,所有这些的总和为[floor(length(str)/ 9)] * 81 +1 。
并且中间的数字具有一个序列,例如,如果length(str)%9 = a,则中间的序列为[1、2、3….a,a – 1,a – 2 ……2] 。现在,可以观察到这部分[1、2、3….a]的总和等于(a *(a + 1))/ 2以及另一部分[a – 1,a – 2, …2]是((a *(a – 1))/ 2)– 1 。
总和=地板(长度(str)/ 9)* 81 +1 +(长度(str)%9) 2 – 1 =地板(长度(str)/ 9)* 81 +(长度(str)%9) 2 。
下面是上述方法的实现:
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
输出:
16
时间复杂度O(1)