473,218 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,218 software developers and data experts.

calculate the series and print

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 for-lop 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
Expand|Select|Wrap|Line Numbers
  1. >>>[1,2,4,8,16...]
  2.  
  3. # I can only print
  4. 1
  5. 2
  6. 4
  7. 8
  8. 16
  9. ...
  10.  
Oct 11 '07 #1
8 1446
rhitam30111985
112 100+
plz post your code..
only then can someone point out your mistake
Oct 11 '07 #2
bartonc
6,596 Expert 4TB
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 for-lop 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
Expand|Select|Wrap|Line Numbers
  1. >>>[1,2,4,8,16...]
  2.  
  3. # I can only print
  4. 1
  5. 2
  6. 4
  7. 8
  8. 16
  9. ...
  10.  
For the second question:
Expand|Select|Wrap|Line Numbers
  1. >>> [2**i for i in range(28)]
  2. [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]
Oct 11 '07 #3
bartonc
6,596 Expert 4TB
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 for-lop but it did not work well since the sign alternative (+, - , +, -...). Please help me
Expand|Select|Wrap|Line Numbers
  1. >>> n = 6
  2. >>> numerators = (i for i in range(n * 2) if i%2)
  3. >>> series = [(1/float(j), -1/float(j))[i%2] for i, j in enumerate(numerators)]
  4. >>> series
  5. [1.0, -0.33333333333333331, 0.20000000000000001, -0.14285714285714285, 0.1111111111111111, -0.090909090909090912]
  6. >>> 2 * sum(series)
  7. 1.4880230880230882
  8. >>> 
Oct 11 '07 #4
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 for-lop 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 -.
Expand|Select|Wrap|Line Numbers
  1. factor = -1
  2. series = range(1, 1000000, 2)
  3. seq = []
  4. for value in series:
  5.     factor *= -1
  6.     seq.append(factor*(1/float(value)))
  7.  
  8. print sum(seq)
  9.  
  10. # >>> 0.785397663397
Oct 11 '07 #5
Motoma
3,237 Expert 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+...)
Check it:
Expand|Select|Wrap|Line Numbers
  1. >>> sum([(((-1)**n)*(float(1)/(1+2*n))) for n in range(0, 8)])
  2. 0.75426795426795445
  3.  
If you need some help understanding this, let me know.
Oct 11 '07 #6
bartonc
6,596 Expert 4TB
Check it:
Expand|Select|Wrap|Line Numbers
  1. >>> sum([(((-1)**n)*(float(1)/(1+2*n))) for n in range(0, 8)])
  2. 0.75426795426795445
  3.  
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):
Expand|Select|Wrap|Line Numbers
  1. >>> sum(((-1)**n)*(float(1)/(1+2*n)) for n in xrange(8))
  2. 0.75426795426795445
  3. >>> 
[EDIT: actually two generators, by using xrange() rather than range()]
Oct 11 '07 #7
Motoma
3,237 Expert 2GB
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):
Expand|Select|Wrap|Line Numbers
  1. >>> sum(((-1)**n)*(float(1)/(1+2*n)) for n in xrange(8))
  2. 0.75426795426795445
  3. >>> 
[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.
Oct 11 '07 #8
bartonc
6,596 Expert 4TB
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.
Oct 11 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anders Eriksson | last post by:
Hello! I want to print out a series of PDF files. I need to print them in a controlled order. I have tried to use this funktion, see below, but it stops on WaitForExit(). The first file...
5
by: Jepsensen | last post by:
Dear Everybody. I got a problem with my cpp code. I'm trying to calculate a very simple Discrete Cosine Transform (DCT), but my c++ code seams to calculate a wrong result. I have just started...
0
by: melledge | last post by:
XML Master Series - XML 2005 - November 14-18 - Hilton Hotel - Atlanta, GA IDEAlliance in its continued efforts to provide groundbreaking services to the XML community is introducing the XML...
5
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I...
8
by: srinpraveen | last post by:
I know to write a program to print the fibonacci series. But the problem is my teacher has asked us to write a program to print the natural numbers that are not involved in the fibonacci series. For...
11
by: Thomas | last post by:
Hi, I'm pretty new to the programming world. I got stuck in the following problem. Please guide me about it. Well I'm trying to get the value of (128^100)^2 I've used long double type but to no...
0
by: terryhui | last post by:
Hi all, I am new to asp. I am developing a system and having some questions in my mind. 1. The system will allow user to add node and delete node in the tree menu. Is it possible to first...
4
by: Vince | last post by:
Given a week Number, how do I calculate the date that for the Monday of that week?
1
by: cmwb2000 | last post by:
Hi I am trying to calculate the radius of a circle of best fit from 2 or 3 mouse positions. At present I am recording a series of points a mouse passes, I intended to pick the first and last point...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.