Wondering if anybody could explain the following code as detailed as possible. S
ID: 3622079 • Letter: W
Question
Wondering if anybody could explain the following code as detailed as possible. Specially the Function.# include <cmath> // This library enable the use of sqrt.
# include <iostream>
using namespace std;
void primenum(long double); // Prototype...
int c = 0;
int main()
{
long double x = 0;
cout<<" This program will generate all prime numbers up to the"
<<" number you have entered below... ";
cout<<" Please enter a number: ";
cin>> x;
cout<<" Here are all the prime numbers up to "<<x<<". ";
primenum(x); //function invocation...
cout<<endl<<" There are "<<c
<<" prime numbers less than or equal to "<<x<<". ";
return 0;
}
// This function will determine the primenumbers up to num.
void primenum(long double x)
{
bool prime = true; // Calculates the square-root of 'x'
int number2;
number2 =(int) floor (sqrt (x));
for (int i = 1; i <= x; i++){
for ( int j = 2; j <= number2; j++){
if ( i!=j && i % j == 0 ){
prime = false;
break;
}
}
if (prime){
cout <<" "<<i<<" ";
c += 1;
}
prime = true;
}
}
Explanation / Answer
please rate - thanks
I'm assumning someone else did this code and you are trying to understand it. I've made some changes to the function to make it clearer (it could still be clearer, but means rewritting everything) and commented the changed function
# include <cmath> // This library enable the use of sqrt.
# include <iostream> //this needed for standard io
using namespace std;
void primenum(long double); // Prototype for prime number recieves a long double, returns nothing
int c = 0; //this global variable counts how many numbers you've found
int main()
{
long double x = 0;
cout<<" This program will generate all prime numbers up to the"
<<" number you have entered below... ";
cout<<" Please enter a number: ";
cin>> x;
cout<<" Here are all the prime numbers up to "<<x<<". ";
primenum(x); //function invocation...
cout<<endl<<" There are "<<c
<<" prime numbers less than or equal to "<<x<<". ";
return 0;
}
// This function will determine the primenumbers up to num.
void primenum(long double x)
{
bool prime ;
int number2;
for (int i = 1; i <= x; i++){ //check every number from 1 to number input to see if it's prime
{prime=true; //start by assuming the number is prime
number2 =(int) floor (sqrt (i)); //mathematically to check if a number is prime you only have to check
//if it has factors from 2 to sqrt of the number
for ( int j = 2; j <= number2; j++){ //check to see if the number has a factor from 2 to sqrt of i
if ( i % j == 0 ){ //does j go into i evenly (is j a factor of i)
prime = false; //if it is the number has factors other then 1 and itself so it's
break; //not prime, no longer need to check if i has any more factors
} // so exit j(inner) loop and go to next iteration of i
}
}
if (prime){ //when finish checking all values of j as factor of x, if no factors found
cout <<" "<<i<<" "; //prime is still true, output i, and count the number as prime
c += 1;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.