How can I write this complete code in python in just one line or may be I must say something which uses least space or least no of characters?
t=int(input())
while t>0:
n=int(input())
s=sum(1/(2.0*i+1) for i in range(n))
print "%.15f"%s
t-=1
How can I write this complete code in python in just one line or may be I must say something which uses least space or least no of characters?
t=int(input())
while t>0:
n=int(input())
s=sum(1/(2.0*i+1) for i in range(n))
print "%.15f"%s
t-=1
You're welcome
for t in range(int(input()), 0, -1): print '%.15f' % sum(1/(2.0*i+1) for i in range(int(input())))
EDIT (explanation):
Firstly, instead of a while loop you can use a for loop in a range. The last argument in the for loop is a -1 to subtract 1 every time instead of the default of plus 1 every time.
If there is only one statement in an if statement, or loop, you can keep the one statement in the same line without going to the next line.
Instead of creating the variable of n, you can simply plug it in since it's only being used once. Same goes for s.
int(input()) is redundant in Python 2 since it would already return an integer (if one is typed in). Also, I think it should be range(input(), 0, -1) because the condition is while t>0, not while t>=0.input() to an int. (Although int(raw_input()) is probably the better way)