//Requires: start > 0; end > 0 //Modifies: nothing //Effects: prints out the cou
ID: 3631457 • Letter: #
Question
//Requires: start > 0; end > 0
//Modifies: nothing
//Effects: prints out the cousin primes between start and end inclusive
//if start > end, it will swap them
//if start = 1, end = 25 this prints
//(3, 7) (7, 11) (13, 17) (19, 23)
//FYI: cousin primes are 2 prime numbers whose difference is 4
void printCousinPrimes(int start, int end);
//Requires: n >= 0
//Modifies: nothing
//Effects: returns the sum of the first 'n' primes
// a parameter of 5 will return
// 2 + 3 + 5 + 7 + 11 or 28
int sumPrimes(int n);
Explanation / Answer
please rate - thanks
#include <iostream>
#include <cmath>
using namespace std;
int sumPrimes(int n);
void printCousinPrimes(int start, int end);
int main()
{int n=5;
cout<<"The sum of the first "<<n<<" primes= "<<sumPrimes(n)<<endl;
printCousinPrimes(1,25);
printCousinPrimes(25,1);
system("pause");
return 0;
}
int sumPrimes(int n)
{int i,j,factors,sum=0,count=0;
for(j=1;count<n;j++)
{factors=0;
for(i=1;i<=j;i++)
if(j%i==0)
factors++;
if(factors==2)
{ sum+=j;
count++;
}
}
cout<<endl;
return sum;
}
void printCousinPrimes(int start, int end)
{int i,j,factors,temp,prev[50],count=0,k;
if(start>end)
{temp=start;
start=end;
end=temp;
}
cout<<"Cousin Primes between "<<start<<" and "<<end<<endl;
for(j=start;j<=end;j++)
{factors=0;
for(i=2;i<j;i++)
if(j%i==0)
factors++;
if(factors==0&&j>1)
{
for(k=0;k<count;k++)
{if(j-prev[k]==4)
cout<<"("<<prev[k]<<", "<<j<<") ";
}
prev[count++]=j;
}
}
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.