Please program in C++ Program 4. Write a function: double zeta( double x) // her
ID: 3720137 • Letter: P
Question
Please program in C++
Program 4. Write a function: double zeta( double x) // here x must be > 1.0 to compute and return the value of the sum Note that n is not a parameter of the function. Your while loop should stop adding once the term 1/k" becomes smaller than 10 parameter n and print the zeta value 10 Then in main function use a while loop to read values of the Program 5. Write a function int arithmeticsum( unsigned int a, unsigned int k, unsigned int n) to compute and return the value of the sum of the first n terms of an arithmetic sequence: a, a + k, a + 2k, . . . a + (n-1): where a and k are positive integers. The main function use a while loop to read values a, k, n and rint the sum with each iteration Program 6. Using the function: bool isprime( unsigned int n ) write a program to construct a vector of the first n prime numbers. Your program should 1. read in 2. use a while loop starting with 2 and with each iteration going up by one. 3. if a number is prime, add it to the vector; 4. finish the loop once n primes have been added. 5. Print the vector in the following fashion: supposing that n -10 your code should print primes[10]-2, 3, 5, 7, 11, 13, 17, 19, 23, 29)Explanation / Answer
4)
/**
The c++ program that demonstrates the zeta
method.
*/
#include<iostream>
using namespace std;
//prototype zeta
double zeta(double x);
int main()
{
/**
The c++ program that demonstrates the zeta
method.
*/
#include<iostream>
using namespace std;
//prototype zeta
double zeta(double x);
int main()
{
//call zeta method
cout<<"zeta(2) :"
<<zeta(2)<<endl;
system("pause");
return 0;
}
/*
Method zeta that takes double x
and returns zeta value.
*/
double zeta(double x)
{
//set sum=0
double sum=0;
//set 10^-10=0.0000000001
const double LIMIT=0.0000000001;
double k=1;
//run while loop until 1/k^x is >= LIMIT
while((1/pow(k,x))>=LIMIT)
{
sum+=(1/pow(k,x));
k=k+1;
}
//return sum
return sum;
}
Sample output:
zeta(2): 1.64492
------------------------------------------------------------------------------
5)
/**
The c++ program that demonstrates the arithmeticsum
method.
*/
#include<iostream>
using namespace std;
//prototype arithmeticsum
int arithmeticsum(unsigned int a, unsigned int k, unsigned int n);
int main()
{
//set a,k and n values
unsigned int a=1;
unsigned int k=10;
unsigned int n=10;
cout<<"a= "<<a<<endl;
cout<<"k= "<<k<<endl;
cout<<"n= "<<n<<endl;
//calling arithmeticsum method
cout<<"arithmeticsum(1,10,10) : "<<arithmeticsum(a,k,n)<<endl;
system("pause");
return 0;
}
/*
Method zeta that takes double x
and returns arithmeticsumvalue.
*/
int arithmeticsum(unsigned int a, unsigned int k, unsigned int n)
{
//set sum=0
double sum=0;
int x=0;
while(x<n-1)
{
sum+=(a+x*k);
x=x+1;
}
//return sum
return sum;
}
Sample output :
a= 1
k= 10
n= 10
arithmeticsum(1,10,10) : 369
------------------------------------------------------------------------------------
6)
/**
The c++ program that demonstrates the isprime
method.
*/
#include<iostream>
#include<vector>
using namespace std;
//prototype isprime
bool isprime(unsigned int n);
int main()
{
//create a vector of integer type
vector<int> primes;
int x=2;
int n=10;
int primcount=0;
//continue for 10 prime numbers
while(primcount<n)
{
//check if start number is prime
if(isprime(x))
{
primes.push_back(x);
primcount++;
}
x++;
}
cout<<"primes["<<n<<"] = {";
for (int i = 0; i < primes.size(); i++)
{
if(i==primes.size()-1)
cout << primes[i]<< " ";
else
cout << primes[i]<< ",";
}
cout << "}";
system("pause");
return 0;
}
/*Returns true if the number n is prime */
bool isprime(unsigned int n)
{
bool flag=true;
for(int i = 2; i <= n / 2; i++)
{
if(n % i == 0) {
flag = false;
break;
}
}
return flag;
}
Sample output:
primes[10] = {2,3,5,7,11,13,17,19,23,29 }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.