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 i.e. 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?
CodePudding user response:
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?
CodePudding user response:
You can use an infinite loop while True
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
Another approach is to 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
A more condensed version (or "functional programming" 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