{"id":19934,"date":"2021-09-02T15:23:41","date_gmt":"2021-09-02T09:53:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=19934"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-find-the-sum-of-series-1-x-x-3-x-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-series-1-x-x-3-x-n\/","title":{"rendered":"Python Program to Find the Sum of Series 1+X+X^3…+X^N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of Series 1\/2-2\/3+3\/4-4\/5+5\/6…+N\/N+1<\/a><\/p>\n Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^3…+X^N)<\/strong> for the given numbers in Python.<\/p>\n 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+X+X^3…+X^N) <\/strong>for the given numbers in Python :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n In the previous article, we have discussed Python Program to Find Sum of Series 1\/2-2\/3+3\/4-4\/5+5\/6…+N\/N+1 Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^3…+X^N) for the given numbers in Python. Examples: Example1: Input: Given Number = 4 Given X value = 8 Output: The total …<\/p>\nGiven Number = 4\r\nGiven X value = 8<\/pre>\n
The total sum of the series till the given number N and x value { 4 8 } = 520<\/pre>\n
Given Number = 5\r\nGiven X value = 11<\/pre>\n
The total sum of the series till the given number N and x value { 5 11 } = 162393<\/pre>\n
Program to Find the Sum of Series 1+X+X^3…+X^N in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvn_numb = 4\r\n# Give the value of x as static input and store it in another variable.\r\ngvn_x_val = 8\r\n# Take a variable say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.\r\nresltsum = 0\r\n# Take another variable say k and initialize its value with 1.\r\nk = 1\r\n# Loop until the value of k is less than or equal to the given number using the while loop.\r\nwhile(k <= gvn_numb):\r\n # Inside the loop, calculate the value of X raised to the power k using the pow() function\r\n # and store it in a variable say p.\r\n p = pow(gvn_x_val, k)\r\n # Add the above value of p to the\u00a0resltsum and store it in the same variable.\r\n resltsum += p\r\n # Increment the value of k by 2 and store it in the same variable k.\r\n k += 2\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n \"The total sum of the series till the given number N and x value {\", gvn_numb, gvn_x_val, \"} = \", resltsum)\r\n<\/pre>\n
The total sum of the series till the given number N and x value { 4 8 } = 520<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the number N as user input using the int(input()) function and\r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some Random Number = \"))\r\n# Give the value of X as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_x_val = int(input(\"Enter some Random Number = \"))\r\n# Take a variable say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.\r\nresltsum = 0\r\n# Take another variable say k and initialize its value with 1.\r\nk = 1\r\n# Loop until the value of k is less than or equal to the given number using the while loop.\r\nwhile(k <= gvn_numb):\r\n # Inside the loop, calculate the value of X raised to the power k using the pow() function\r\n # and store it in a variable say p.\r\n p = pow(gvn_x_val, k)\r\n # Add the above value of p to the\u00a0resltsum and store it in the same variable.\r\n resltsum += p\r\n # Increment the value of k by 2 and store it in the same variable k.\r\n k += 2\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n \"The total sum of the series till the given number N and x value {\", gvn_numb, gvn_x_val, \"} = \", resltsum)\r\n<\/pre>\n
Enter some Random Number = 5\r\nEnter some Random Number = 11\r\nThe total sum of the series till the given number N and x value { 5 11 } = 162393<\/pre>\n
\n