computing the Riemann zeta function

Sep 30, 2013 at 8:29pm
closed account (ETA9216C)
I am new to c++ and taking a college coarse on it. Immediately i need help on one of my labs. I'm suppose to provide an inner loop that computes the zeta function and the other loop is just press y for to continue, anything else exit.so far i got this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cmath>
#include <stdlib.h>


using namespace std;


int main()
{
	double x, n,zeta;
    char flag = 'y';

	while(flag=='y')
 {
        cout <<"Enter x value ";
        cin >> x;


        n=1;
        zeta=0
	while(1/(pow(n,x))>1e-7)
{
        zeta= 1/(pow(n,x));
        zeta=+zeta;
        n++;




}
    cout<<"Sum is "<<zeta<<endl;
    cout<<"Enter y for Yes, anything else is no"<<endl;
    cin>>flag;

 }
    system("pause");
    return 0;

}


I can't seem to get the formula right.
Last edited on Sep 30, 2013 at 9:15pm
Sep 30, 2013 at 8:49pm
When learning, it is extremely important to use correct indenting. It may seem trivial and something you can 'fix' later, but when your programs get to be more than 40 lines, it will become impossible to read.

But I think this is what you are looking for:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cmath>

double zeta(double s)
{
    const double precision = 1e-7;
    double output = 0.0;
    double calculation = 1.0;
    
    for (int denom = 2; abs(output - calculation) > precision; ++denom)
    {
        output += calculation;
        calculation = 1.0 / pow(denom, s);
    }
    
    return output;
}


If you haven't learned functions yet, then just stick lines 5-13 inside of your while(flag=='y') loop. If you can't figure out where your while loops start and end, then fix your formatting.
Last edited on Sep 30, 2013 at 8:53pm
Sep 30, 2013 at 8:59pm
closed account (ETA9216C)
i forgot to mention i must put it in a "while" loop, and my prof. has restricted the class from using break (except in switch) and constant?. Is it also possible to dumb things down to a level of a beginner, i don't want to use anything beside what i learn which is simple stuff what i just kind of did.
Topic archived. No new replies allowed.