{"id":19475,"date":"2021-08-30T16:09:32","date_gmt":"2021-08-30T10:39:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=19475"},"modified":"2021-11-22T18:36:30","modified_gmt":"2021-11-22T13:06:30","slug":"python-program-to-find-the-sum-of-series-1-2-2-2-3-2-n-2","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-series-1-2-2-2-3-2-n-2\/","title":{"rendered":"Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Convert Binary to Octal using While Loop<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the sum of the given series 1^2+2^2+3^2…+N^2<\/strong>\u00a0for the given number in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity:<\/strong> O(N) where N is the value of the given number (As the loop runs N times)<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity:<\/strong> O(N) where N is the value of the given number (As the loop runs N times)<\/p>\n We can calculate the sum of the above series directly by using the mathematical formula :<\/p>\n (n * ( n + 1 ) *( 2 * n +1) ) \/ 6<\/p><\/blockquote>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity:<\/strong> O(1)<\/p>\n We can calculate the sum of the above series directly by using the mathematical formula :<\/p>\n (n * ( n + 1 ) *( 2 * n +1) ) \/ 6<\/p><\/blockquote>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Time Complexity:<\/strong> O(1)<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Convert Binary to Octal using While Loop Given a number N the task is to find the sum of the given series 1^2+2^2+3^2…+N^2\u00a0for the given number in Python. Examples: Example1: Input: Given Number =3 Output: The sum of the series till the given number { …<\/p>\n
\nGiven a number N the task is to find the sum of the given series 1^2+2^2+3^2…+N^2<\/strong>\u00a0for the given number in Python.<\/p>\nGiven Number =3<\/pre>\n
The sum of the series till the given number { 3 } is : 14<\/pre>\n
Given Number =5<\/pre>\n
The sum of the series till the given number { 6 } is : 55<\/pre>\n
Program to find the Sum of Series 1^2+2^2+3^2…+N^2 <\/strong>in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvnNumbr = 5\r\n# Take a variable say resltsum which gives the sum of\r\n# the given series till N and initialize its value to 0.\r\nresltsum = 0\r\n# Loop from 1 to given number using the For loop.\r\nfor k in range(1, gvnNumbr+1):\r\n # Inside the For loop,\r\n # Calculate the value of iterator value * iterator value and store it in a variable.\r\n squarevalu = k*k\r\n # Add the above variable to the resltsum\r\n resltsum = resltsum+squarevalu\r\n\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)\r\n<\/pre>\n
The sum of the series till the given number { 5 } is : 55<\/pre>\n
Method #2: Using For loop and pow() function (User Input)<\/h3>\n
\n
# Give the number N as user input using the int(input()) function\r\n# and store it in a variable.\r\ngvnNumbr = int(input('Enter some random number N = '))\r\n# Take a variable say resltsum which gives the sum of\r\n# the given series till N and initialize its value to 0.\r\nresltsum = 0\r\n# Loop from 1 to given number using the For loop.\r\nfor k in range(1, gvnNumbr+1):\r\n # Inside the For loop,\r\n # Calculate the value of iterator value * iterator value and store it in a variable.\r\n squarevalu = k*k\r\n # Add the above variable to the resltsum\r\n resltsum = resltsum+squarevalu\r\n\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)\r\n<\/pre>\n
Enter some random number N = 3\r\nThe sum of the series till the given number { 3 } is : 14<\/pre>\n
Method #3: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvnNumbr = 5\r\n# Calculate the sum of the given series using the above mathematical formula\r\n# and store it in a variable to say\u00a0resltsum\r\nresltsum = (gvnNumbr*(gvnNumbr+1)*((2*gvnNumbr)+1))\/\/6\r\n\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)\r\n<\/pre>\n
The sum of the series till the given number { 5 } is : 55<\/pre>\n
Method #4: Using For loop and ** operator (User Input)<\/h3>\n
\n
# Give the number N as user input using the int(input()) function\r\n# and store it in a variable.\r\ngvnNumbr = int(input('Enter some random number N = '))\r\n# Calculate the sum of the given series using the above mathematical formula\r\n# and store it in a variable to say\u00a0resltsum\r\nresltsum = (gvnNumbr*(gvnNumbr+1)*((2*gvnNumbr)+1))\/\/6\r\n\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n 'The sum of the series till the given number {', gvnNumbr, '} is :', resltsum)\r\n<\/pre>\n
Enter some random number N = 8\r\nThe sum of the series till the given number { 8 } is : 204<\/pre>\n
\n