I would like to calculate the value of this series (for limited number of n)
s=2(1/1  1/3 + 1/5 1/7 + 1/9  1/11+...)
I tried forlop but it did not work well since the sign alternative (+,  , +, ...). Please help me
My second question is
how can I obtain this when using for loop to make a series 
>>>[1,2,4,8,16...]


# I can only print

1

2

4

8

16

...

8 1384
plz post your code..
only then can someone point out your mistake
I would like to calculate the value of this series (for limited number of n)
s=2(1/1  1/3 + 1/5 1/7 + 1/9  1/11+...)
I tried forlop but it did not work well since the sign alternative (+,  , +, ...). Please help me
My second question is
how can I obtain this when using for loop to make a series 
>>>[1,2,4,8,16...]


# I can only print

1

2

4

8

16

...

For the second question: 
>>> [2**i for i in range(28)]

[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728]
I would like to calculate the value of this series (for limited number of n)
s=2(1/1  1/3 + 1/5 1/7 + 1/9  1/11+...)
I tried forlop but it did not work well since the sign alternative (+,  , +, ...). Please help me

>>> n = 6

>>> numerators = (i for i in range(n * 2) if i%2)

>>> series = [(1/float(j), 1/float(j))[i%2] for i, j in enumerate(numerators)]

>>> series

[1.0, 0.33333333333333331, 0.20000000000000001, 0.14285714285714285, 0.1111111111111111, 0.090909090909090912]

>>> 2 * sum(series)

1.4880230880230882

>>>
bvdet 2,851
Expert Mod 2GB
I would like to calculate the value of this series (for limited number of n)
s=2(1/1  1/3 + 1/5 1/7 + 1/9  1/11+...)
I tried forlop but it did not work well since the sign alternative (+,  , +, ...). Please help me
A for loop works fine. You just need a way to alternate the + and .  factor = 1

series = range(1, 1000000, 2)

seq = []

for value in series:

factor *= 1

seq.append(factor*(1/float(value)))


print sum(seq)


# >>> 0.785397663397
I would like to calculate the value of this series (for limited number of n)
s=2(1/1  1/3 + 1/5 1/7 + 1/9  1/11+...)
Check it: 
>>> sum([(((1)**n)*(float(1)/(1+2*n))) for n in range(0, 8)])

0.75426795426795445

If you need some help understanding this, let me know.
Check it: 
>>> sum([(((1)**n)*(float(1)/(1+2*n))) for n in range(0, 8)])

0.75426795426795445

If you need some help understanding this, let me know.
Very nice! To increase efficiency and reduce syntactic clutter, use a generator instead of a list (I also dumped one set of parens on GPs): 
>>> sum(((1)**n)*(float(1)/(1+2*n)) for n in xrange(8))

0.75426795426795445

>>>
[EDIT: actually two generators, by using xrange() rather than range()]
Very nice! To increase efficiency and reduce syntactic clutter, use a generator instead of a list (I also dumped one set of parens on GPs): 
>>> sum(((1)**n)*(float(1)/(1+2*n)) for n in xrange(8))

0.75426795426795445

>>>
[EDIT: actually two generators, by using xrange() rather than range()]
FANTASTIC!
This is the first time I have been introduced to generators!
I honestly learn something new every time I post in this forum.
FANTASTIC!
This is the first time I have been introduced to generators!
I honestly learn something new every time I post in this forum.
From what I understand, generators are something to get used to.
They are efficient because they return values instead of storing them.
This means that you can't index into them nor print the entire contents; You must iterate over them.
They are required learning because Python 3.0 uses them in many places where lists were used in 2.5.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
reply
views
Thread by Anders Eriksson 
last post: by

5 posts
views
Thread by Jepsensen 
last post: by

reply
views
Thread by melledge 
last post: by

5 posts
views
Thread by kux 
last post: by
 
11 posts
views
Thread by Thomas 
last post: by

reply
views
Thread by terryhui 
last post: by

4 posts
views
Thread by Vince 
last post: by
           