Calculating the sum of a series?

2024/7/27 15:16:14

This is my assignment and for the life of me i cant seem to think of a way to do it. This is the code I have so far:

sum = 0
k = 1
while k <= 0.0001:if k % 2 == 1:sum = sum + 1.0/kelse:sum = sum - 1.0/kk = k + 1print()

This is my assignment :

Create a python program named sumseries.py that does the following:Put comments at the top of your program with your name, date, anddescription of what the program does.

Write a program to calculate and display the sum of the series:

1 - 1/2 + 1/3 - 1/4 + ...

until a term is reached that is less than 0.0001.

The answer with 10,000 iterations appears to be 0.6930971830599583

I ran the program with 1,000,000,000 (billion) iterations and came up with a number of 0.6931471810606472. I need to create a loop to programmably create the series.

Answer

Actually, you could write this shorter:

Answer = sum(1.0 / k if k % 2 else -1.0 / k for k in range(1, 10001))

What this code does:

  • the innermost part is a generator expression, which computes the elements of a series 'on the fly'
    • 1.0 / k if k % 2 else -1.0 / k results in 1.0 / k if k is odd and -1.0 / k otherwise (a - b is the same as a + (-b))
    • for k in range(1, 10001) goes through all ks in range from 1 (included) to 10001 (excluded)
  • sum can compute the sum of any sequence (any iterable, to be precise), be it a list, a tuple, or a generator expression

The same without generator expressions:

Answer = 0
for k in range(1, 10001):if k % 2:Answer += 1.0 / kelse:Answer -= 1.0 / k# or simply:# Answer += 1.0 / k if k % 2 else -1.0 / k
https://en.xdnf.cn/q/73136.html

Related Q&A

get all the partitions of the set python with itertools

How to get all partitions of a set? For example, I have array [1, 2, 3]. I need to get [[1], [2], [3]], [[1], [2, 3]], [[2], [1,3]], [[3], [1, 2]], [[1, 2, 3]]. Now, I wrote this code:def neclusters(S…

Installing Python binary modules to a custom location in Windows

Suppose that I want to install a binary module for Python on Windows. Suppose that the module is distributed as a pre-built installer xxx-n.n.n.win32-py2.7.exe, prepared using distutils.My problem is t…

tkinter tkMessageBox html link

I got a tkMEssagebox.showerror showing up in a python tkinter application, when somebody failed to login with the application. Is it possible to have a url link in the tkMessageBox.showerror?ie.tkMess…

How to build a MultiIndex Pandas DataFrame from a nested dictionary with lists

I have the following dictionary.d= {key1: {sub-key1: [a,b,c,d,e]},key2: {sub-key2: [1,2,3,5,8,9,10]}}With the help of this post, I managed to successfully convert this dictionary to a DataFrame.df = pd…

Functions and if - else in python. Mutliple conditions. Codeacademy

Write a function, shut_down, that takes one parameter (you can use anything you like; in this case, wed use s for string).The shut_down function should return "Shutting down..." when it gets …

ipython up and down arrow strange behaviour

In my installation of ipython I have this strange problem where I cannot reliably move through command history with up and down arrows... a lot of the time it just doesnt work (nothing happens on the k…

Python for loop slows and evenutally hangs

Im totally new to Python (as of half an hour ago) and trying to write a simple script to enumerate users on an SMTP server.The users file is a simple list (one per line) of usernames.The script runs fi…

Jupyter Notebook: Change Data Rate Limit Inside Active Notebook

I have a jupyter notebook where an executed cell gives the following error:IOPub data rate exceeded...I understand this is an option:jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10However, …

How to obtain the training error in svm of Scikit-learn?

My question: How do I obtain the training error in the svm module (SVC class)?I am trying to do a plot of error of the train set and test set against the number of training data used ( or other featur…

Flask-Migrate not detecting tables

I have the following project structure:project/__init__.pyfrom flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migratedb = SQLAlchemy() migrate = Migrate()def creat…