Author |
Topic: The infinite series (Read 938 times) |
|
brock123
Newbie
Posts: 13
|
Please Please can you solve this question Q) The infinite series x ‡” k=0 1/k! (where x above ‡” and k=0 under ‡” ) you can see the picture to see what i mean.(picture is down) converges to the number e, whose approximate is 2.71828. The nth partial sum of such series is the sum of the first n terms of the series; for example 1/0! + 1/1! + 1/2! + 1/3! is the fourth sum. Write a program to calculate and print the first 10 partial sums of this series. thanks to everyone try to solve it.
|
« Last Edit: Nov 21st, 2004, 9:07pm by brock123 » |
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Please can you solve this question
« Reply #1 on: Nov 21st, 2004, 3:12pm » |
Quote Modify
|
Surely you're smart enough to program this yourself, aren't you? initialize the starting values and then start ten loops, print the partial sum then update the factorial update the partial sum, repeat the loop (untill you've done ten) And please, if you can, pick a better title for this thread.
|
« Last Edit: Nov 21st, 2004, 3:16pm by towr » |
IP Logged |
Wikipedia, Google, Mathworld, Integer sequence DB
|
|
|
Aryabhatta
Uberpuzzler
Gender:
Posts: 1321
|
|
Re: Please can you solve this question
« Reply #2 on: Nov 21st, 2004, 3:58pm » |
Quote Modify
|
This looks like homework to me. Is that the case, brock123? You could at least show your working and tell where you got stuck...
|
|
IP Logged |
|
|
|
brock123
Newbie
Posts: 13
|
|
Re: The infinite series
« Reply #3 on: Nov 21st, 2004, 9:11pm » |
Quote Modify
|
Thank you i'll pst my solution soon to tell you where i stuked.
|
|
IP Logged |
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: The infinite series
« Reply #4 on: Nov 22nd, 2004, 2:11am » |
Quote Modify
|
Note that if you do the sum backwards (.. + 1/3! + 1/2! + 1/1! + 1/0!) the result is more precise. Of course, it makes the program much more complicated.
|
|
IP Logged |
|
|
|
brock123
Newbie
Posts: 13
|
|
Re: The infinite series
« Reply #5 on: Nov 23rd, 2004, 2:05pm » |
Quote Modify
|
This is my answer ( but it is not 100%) becuase it work only when the user enter number 4-------> value which is near 2.71828 but if the the user enter number 7 for example the value will be far away from 2.71828 please can check my soultion to tell me how do i chang it. By the way I'm really sorry because i post my soultion late becaue i had an exam yesterday. #include<iostream> using namespace std; int main() { float n,i,fact=1; int counter=0; cout<<"Enter the number: "; cin>>n; for (i=n;i>0;--i) { counter++; fact=(1/fact)*(1/i); } cout<<"The number of terms = "<<counter<<endl; cout<<fact<<endl; return 0; }
|
|
IP Logged |
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: The infinite series
« Reply #6 on: Nov 23rd, 2004, 9:56pm » |
Quote Modify
|
Hm.... I can see that counter is just used to recompute n... I would prefer n and i to be integers. The only problem is that then, 1/i must be made float. Replace (1/i) by (1.0/i). Also, I would use double, since all computations are made in double. Anyway, what you compute is: 2 / 3 * 4 / 5 * ... * n ( or ... / n) which is probably not what you want. To compute the sum, you can write double fact = 1; double sum = 1; for (i=1 ; i<=n ; i++ ){ fact *= i; sum += 1.0/fact; } sum -> e As I said, it is more accurate to compute from the end, but the problem is that there is no easy way to compute the factorial backwards. double fact[100]; double sum = 0; fact[0] = 1; for (i=1 ; i<=n ; i++ ){ fact[i] = fact[i-1]*i; } for ( int i=n ; i>=0 ; i-- ){ sum += 1/fact[i]; } cout << sum << endl; You can see the difference in the last digits. Bit what I would do, is to compute e as a product: e = 1/0! + 1/1! + 1/2! + 1/3! + ... = (((.../3+1))/2+1)/1+1 double sum = 1; for ( int i=n ; i>0 ; i-- ){ sum = sum/i + 1; } cout << sum << endl; Last but not least, the real value is in math.h as M_E. cout << M_E << endl;
|
« Last Edit: Nov 23rd, 2004, 11:01pm by Grimbal » |
IP Logged |
|
|
|
brock123
Newbie
Posts: 13
|
|
Re: The infinite series
« Reply #7 on: Nov 28th, 2004, 6:54am » |
Quote Modify
|
sorry because I post my reply late because I had alot of exams. anywhy thank you so much (Grimbal ) for the solution & the idea.
|
|
IP Logged |
|
|
|
|