Question: Question 3 (a) Type a C++ function named sumOfTwoPrimes that checks (returns true or false) whether a given positive integer can be expressed as sum
Question 3
(a) Type a C++ function named sumOfTwoPrimes that checks (returns true or false) whether a given positive integer can be expressed as sum of two prime numbers. For example, sumOfTwoPrimes(28) returns true because 28 = 5 + 23, and both 5 and 23 are prime numbers (note: 28 can also be expressed as the sum of 11 + 17); Another example, sumOfTwoPrimes(11) returns false because 11 cannot be expressed as sum of two primes. Use the check_prime function (shown below) as a supporting function.
bool check_prime (int num)
{
bool isPrime = true;
for (int n = 2; n <= num / 2; n++)
{
if (num % n == 0)
isPrime = false;
}
return isPrime;
}
x --- x
(b) Define a function named calculationOfPI that takes a positive integer n as its argument and calculates using the following formula. Assume n is odd, which is the precondition of the function.
= 4*(1 1/3 + 1/5 1/7 + 1/9 1/11 + ... 1/n)
x---x
(c) - The Babylonian algorithm to compute the square root of a positive number n is as follows:
1. Make a guess at the answer (you may pick n/2 as your initial guess) 2. Compute r = num / guess 3. Set guess = (guess + r) / 2
4. Go back to step 2 for as many iterations as necessary. The more step 2 and 3 are repeated, the closer guess will become to the square root of n.
Type a C++ function using the above algorithm, named TheSquareRoot that takes a positive number of type double as its argument and iterates through the Babylonian algorithm until the change of the guesses is within 1% of the previous guess.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
