Method 1: Using functions to find the sum of squares in python
By using functions, there are two methods available to find the sum of squares in python. One using a loop, another one without using the loop.
Code 1
Explanation
Create a function named square. Initialize sum is zero. Create a for loop to iterate till the end of the loop. We are giving the num value as 6. So it will iterate till it reaches 6. Once it reached 6. It will come out of the loop and execute the result.
Output
Sum of square is: 91Code 2
Explanation
Create a function named square. Using the formula to calculate the square of the numbers. Printing the result.
Output
Sum of square is: 55Method 2: Using for loop to find the sum of squares in python
Now we will discuss how to calculate the sum of squares using for loop. By using for loop, we can find the sum of squares in two different methods.
Code 1
Explanation
Getting the num value from the user. Initialize sum is equal to zero. Creating for loop to calculate the result. Inside for loop, we have declared sum=sum+[i*i]. That is calculating the square of i and adding it to the sum. Finally, printing the sum of squares.
Output
Enter value of num: 3 Sum of squares = 14Code 2
Explanation
Getting the num value from the user. Using the formula to calculate the square of the numbers. Printing the result.
Output
Enter value of num: 7 Sum of squares = 140Python Program for Sum of squares of first n natural numbers
Given a positive integer N. The task is to find 12 + 22 + 32 + ….. + N2.
Examples:
Input : N = 4 Output : 30 12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30 Input : N = 5 Output : 55Method 1: O[N] The idea is to run a loop from 1 to n and for each i, 1 2 to sum.
# Python3 Program to
# find sum of square
# of first n natural
# numbers
# Return the sum of
# square of first n
# natural numbers
def squaresum[n] :
# Iterate i from 1
# and n finding
# square of i and
# add to sum.
sm = 0
for i in range[1, n+1] :
sm = sm + [i * i]
return sm
# Driven Program
n = 4
print[squaresum[n]]
# This code is contributed by Nikita Tiwari.*/
|
Output:
30Method 2: O[1]
Proof:We know, [k + 1]3 = k3 + 3 * k2 + 3 * k + 1 We can write the above identity for k from 1 to n: 23 = 13 + 3 * 12 + 3 * 1 + 1 ......... [1] 33 = 23 + 3 * 22 + 3 * 2 + 1 ......... [2] 43 = 33 + 3 * 32 + 3 * 3 + 1 ......... [3] 53 = 43 + 3 * 42 + 3 * 4 + 1 ......... [4] ... n3 = [n - 1]3 + 3 * [n - 1]2 + 3 * [n - 1] + 1 ......... [n - 1] [n + 1]3 = n3 + 3 * n2 + 3 * n + 1 ......... [n] Putting equation [n - 1] in equation n, [n + 1]3 = [n - 1]3 + 3 * [n - 1]2 + 3 * [n - 1] + 1 + 3 * n2 + 3 * n + 1 = [n - 1]3 + 3 * [n2 + [n - 1]2] + 3 * [ n + [n - 1] ] + 1 + 1 By putting all equation, we get [n + 1]3 = 13 + 3 * Σ k2 + 3 * Σ k + Σ 1 n3 + 3 * n2 + 3 * n + 1 = 1 + 3 * Σ k2 + 3 * [n * [n + 1]]/2 + n n3 + 3 * n2 + 3 * n = 3 * Σ k2 + 3 * [n * [n + 1]]/2 + n n3 + 3 * n2 + 2 * n - 3 * [n * [n + 1]]/2 = 3 * Σ k2 n * [n2 + 3 * n + 2] - 3 * [n * [n + 1]]/2 = 3 * Σ k2 n * [n + 1] * [n + 2] - 3 * [n * [n + 1]]/2 = 3 * Σ k2 n * [n + 1] * [n + 2 - 3/2] = 3 * Σ k2 n * [n + 1] * [2 * n + 1]/2 = 3 * Σ k2 n * [n + 1] * [2 * n + 1]/6 = Σ k2
# Python3 Program to
# find sum of square
# of first n natural
# numbers
# Return the sum of
# square of first n
# natural numbers
def squaresum[n] :
return [n * [n + 1] * [2 * n + 1]] // 6
# Driven Program
n = 4
print[squaresum[n]]
#This code is contributed by Nikita Tiwari.
|
Output:
30Avoiding early overflow:
For large n, the value of [n * [n + 1] * [2 * n + 1]] would overflow. We can avoid overflow up to some extent using the fact that n*[n+1] must be divisible by 2.
# Python Program to find sum of square of first
# n natural numbers. This program avoids
# overflow upto some extent for large value
# of n.y
def squaresum[n]:
return [n * [n + 1] / 2] * [2 * n + 1] / 3
# main[]
n = 4
print[squaresum[n]];
# Code Contributed by Mohit Gupta_OMG
|
Output:
30Please refer complete article on Sum of squares of first n natural numbers for more details!
Python | Sum of squares in list
Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding sum of squares of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1 : Using reduce[] + lambda
The power of lambda functions to perform lengthy tasks in just one line, allows it combined with reduce which is used to accumulate the subproblem, to perform this task as well. Works with only Python 2.
# Python code to demonstrate
# sum of squares
# using reduce[] + lambda
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ["The original list is : " + str[test_list]]
# using reduce[] + lambda
# sum of squares
res = reduce[lambda i, j: i + j * j, [test_list[:1][0]**2]+test_list[1:]]
# printing result
print ["The sum of squares of list is : " + str[res]]
|
Method #2 : Using map[] + sum[]
The similar solution can also be obtained using the map function to integrate and sum function to perform the summation of the squared number.
# Python3 code to demonstrate
# sum of squares
# using sum[] + max[]
# initializing list
test_list = [3, 5, 7, 9, 11]
# printing original list
print ["The original list is : " + str[test_list]]
# using sum[] + max[]
# sum of squares
res = sum[map[lambda i : i * i, test_list]]
# printing result
print ["The sum of squares of list is : " + str[res]]
|
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
What is the Sum of Squares?
Thesum of squaresrefers to the sum of the squared numbers in a range of numbers. So, say you wanted to find the sum of squares of the numbers from 1 through N, this would be represented by:
1² + 2² + 3² + 4².....+ N²The sum of squares represents a measure of variation and can be used to calculate the deviation from a mean.
Python Sum of Squares with a For Loop
Now, the most intuitive way may be to calculate the Python sum of squares using a for loop. If you wanted a refresher on Python for-loops, check out my post here.
Say we want to calculate the sum of squares for the first 5 numbers, we can write:
sum_of_squares = 0 for num in range[6]: sum_of_squares += num ** 2 print[sum_of_squares] # Returns: 55What we’ve done here is created a variablesum_of_squaresand assigned it the value of 0. We then loop over a range of numbers and add each numbers square to the variablesum_of_squares.
We can easily turn this into a function by writing:
def sum_of_squares_for_loop[end_number]: sum_of_squares = 0 for num in range[end_number + 1]: sum_of_squares += num ** 2 return sum_of_squares print[sum_of_squares_for_loop[5]] # Returns: 55Python Program to Calculate the Sum of Squares of First n Natural Number
In this tutorial, you will learn how to create a program to calculate the sum of squares of first N natural numbers. To execute this program, we will be using the concept of loops in Python. You must know this concept before moving further.
For a positive integer N, the task is to find the value of,
1² + 2² + 3² + 4².....+ N²The program should accept the following input, look at the sample input and output below,
Input- enter n: 13
Output- sum of square of first 13 natural numbers: 819
For executing this task, we can follow two approaches:
- Using loops
- Using formula
Python Program to Find Sum of Squares of Digits of a Number
This article is created to cover a program in Python that find and prints the sum of squares of digits of a number entered by user. For example, if the number entered by user is 342, then the calculation goes like:
That is, 29 is the output produced by the program given below after providing exactly the same input.