Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Sumofsquareofnnumbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'''
Given a positive integer N. The task is to find 12 + 22 + 32 + ….. + N2.
if N = 4
1^2 + 2^2 + 3^2 + 4^2 = 30
The idea is to run a loop from 1 to n and for each i, 1 <= i <= n, find i2 to sum.
'''

def squaresum(n):
sum = 0
for i in range(1, n+1):
sum += (i * i)
return sum

n = int(input("Enter Number to Print Sum Of square of N Natural Number :\n"))
print(squaresum(n))


'''
The above approach takes linear time to compute the squaresum however we can solve
this in constant time using the formula:
Σn^2 = [n(n+1)(2n+1)]/6
Source : https://en.wikipedia.org/wiki/Square_pyramidal_number
'''

def squaresum(n):
sum = (n*(n+1)*(2*n+1))//6
return sum

n = int(input("Enter Number to Print Sum Of square of N Natural Number :\n"))
print(squaresum(n))