- Related Questions & Answers
- C++ Program for Sum of squares of first n natural numbers?
- Sum of squares of first n natural numbers in C Program?
- Python Program for cube sum of first n natural numbers
- Java Program to calculate Sum of squares of first n natural numbers
- C Program for cube sum of first n natural numbers?
- C++ Program for cube sum of first n natural numbers?
- Difference between sum of the squares of and square of sum first n natural numbers.
- C Program for the cube sum of first n natural numbers?
- Program for cube sum of first n natural numbers in C++
- Sum of first n natural numbers in C Program
- Average of Squares of n Natural Numbers?
- Sum of sum of first n natural numbers in C++
- Java Program to cube sum of first n natural numbers
- Sum of square-sums of first n natural numbers
- Sum of squares of the first n even numbers in C Program

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution and approach to solve the given problem statement.

Given a positive integer N as input . We need to compute the value of 12 + 22 + 32 + ….. + N2.

Problem statement:This can be solved by two methods

- Multiplication addition arithmetic
- Using mathematical formula

Here we run a loop from 1 to n and for each i, 1 <= i <= n, find i2 and add to sm.

def sqsum(n) : sm = 0 for i in range(1, n+1) : sm = sm + pow(i,2) return sm # main n = 5 print(sqsum(n))

55

As we all know that the sum of squares of natural numbers is given by the formula −

(n * (n + 1) * (2 * n + 1)) // 6n * (n + 1) * (2 * n + 1)) // 6 (n * (n + 1) * (2 * n + 1)) // 6(n * (n + 1) * (2 * n + 1)) // 6

def squaresum(n) : return (n * (n + 1) * (2 * n + 1)) // 6 # Driven Program n = 10 print(squaresum(n))

385

In this article, we learned about the approach to find the Sum of squares of first n natural numbers.

Advertisements