5

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/k
     else:
      sum = sum - 1.0/k
 k = k + 1
 print()

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, and description 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.

ayhan
  • 70,170
  • 20
  • 182
  • 203

5 Answers5

6

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 / k
    else:
        Answer -= 1.0 / k

    # or simply:
    # Answer += 1.0 / k if k % 2 else -1.0 / k
Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98
  • 1
    Or `sum((-1)**(~k % 2) / k for k in range(1, 10001))`. – Stefan Pochmann Sep 18 '16 at 17:05
  • 1
    Absolutely beautiful, but if I saw a student hand this in, I'd scratch my head about his/her own programming efforts that went into answering the question. :) – Nelewout Sep 18 '16 at 17:10
  • This is what i ended up with, it prints all 10001 outputs on the screen, which i think i the teacher wants x = 0 y = 0 for k in range (1,10001): if k % 2 == 0: x += 1.0/k else: y += 1.0/k print (y - x - 1.0/10001**2) – Jamie Schwiderski Sep 18 '16 at 17:27
  • @JamieSchwiderski, why use two variables if you could do `x += 1.0 / k if k % 2 else -1.0 / k` instead of the `if/else` case? – ForceBru Sep 18 '16 at 17:30
  • we are in a for loop or while loop in our chapter – Jamie Schwiderski Sep 18 '16 at 17:35
2

You're almost there, all you need to do is to replace

while k <= 0.0001:

with:

 while term <= 0.0001:

term is naturally 1/k

Dominique Lorre
  • 1,168
  • 1
  • 10
  • 19
1

To make the teacher happy, you must follow the details of the problem, as well as the spirit of the problem. The problem clearly states to print the sum, not all the partial sums. You will anger the teacher by submitting a solution that spews 10000 lines of crap not requested.

Some have suggested pre-calculating a loop limit of 10000, but that was not the requested algorithm. Instead, one is to calculate successive terms (1, -1/2, 1/3, -1/4, ...) until reaching a term less than 0.0001.

The reason the problem was specified that way is that one ends up with a more generally useful program, applicable to a wide class of term formulas. Not a fragile one that gets the wrong answer if the term formula is changed from (-1)**(k-1)/k, to say 1/k or 1/k^2.

The teacher's wording "term less than 0.0001" is imprecise and assumed some math knowledge. They want the magnitude (absolute value) of the term to be less than 0.0001. Otherwise, iteration would stop at the second term -1/2, as someone pointed out.

So, this answer would not be complete without a pompous pedantic solution that skips ahead a chapter. ;) Note that previous some answers will not work in Python2.x without a conversion to float.

def term(k):
    return (-1)**(k - 1) / float(k)

err = 0.0001

def terms():
    k = 1
    t = term(k)
    while abs(t) >= err:
        yield t
        k += 1
        t = term(k)

print(sum(terms()))
Curt
  • 470
  • 3
  • 7
0

Here is the answer your teacher is looking for for full credit.
until < .0001 means while >= 0.0001 This modifies your code the least, so makes it a correction of what you wrote

sum = 0
k = 1
while 1.0/k >= 0.0001:
     if k % 2 == 1:
       sum = sum + 1.0/k
     else:
       sum = sum - 1.0/k
     k = k + 1
print(sum)
SAS
  • 9
  • 3
0

Absolutly simplest way would be the following sum((-1)**(k) / k for k in range(1, 10001))

henryJack
  • 4,220
  • 2
  • 20
  • 24