简体   繁体   中英

given a number p, print value of n at which sum of (1/1 + 1/2 + ... + 1/n) is greater than p

Given a number 'p', develop a logic and write a Python program to determine the smallest integer 'n' in the sum of the reciprocals ( 1 + 1/2 + 1/3 + ⋅⋅⋅ + 1/n) that makes the summation to be greater than 'p'. Print the value of 'n' and the sum 's'.

For example, when p=3, the sum of the reciprocals continues to be added until the value of s become greater than 3 ie n is 11

p=int(input())
new=0
for i in range(1,1000000):
    term=1/i
    sum1=new+term
    new=sum1
    i=i+1
    if sum1>p:
        print(i-1,sum1)
        break

I have a doubt in how to set the upper limit of range. How do I decide what to put in this?

I have a doubt in how to set the upper limit of range. How do I decide what to put in this?

It's good that you've recognized this as a potential problem in your code: And the answer is: you don't know how many times you might need to run this loop. So it's not possible to decide what that upper bound is.

for loops are useful when you know how many times you want to loop. Is there another kind of loop you can use when you're not sure how many times you should be looping?

You can use itertools.count to generate an infinite for loop, and stop it when a condition is met (either by break or return ):

import itertools

def first_greater(p):
    total = 0
    for i, x in enumerate(itertools.count(1), start=1):
        total += 1 / x
        if total > p:
            return i

print(first_greater(3)) # 11

Another approach is to use while True (an infinite loop) with a stop condition:

import itertools

p = 3
n, total = 0, 0
while True:
    n += 1
    total += 1 / n
    if total > p:
        break

print(n) # 11

A fun "functional" approach: you can use itertools.count with itertools.dropwhile (or itertools.takewhile )

import itertools

p = 3
series = itertools.accumulate(1 / x for x in itertools.count(1))
greaters = itertools.dropwhile(lambda x: x[1] <= p, enumerate(series, start=1))
print(next(greaters)[0]) # 11

Use a while loop.

p = int(input())
new, i = 1, 1
while new <= p:
    new = 1 / i
    i += 1
print(i - 1, new)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM