Open main menu

CDOT Wiki β

Changes

DPS915/M-N-M

1,293 bytes added, 10:45, 7 March 2013
Assignment 2
=== Assignment 2 ===
 
<h3>Source code for prime number generator we will be putting on the gpu</h3>
<pre>
 
# 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<<"\n This program will generate all prime numbers up to the"
<<"\n number you have entered below...\n";
cout<<"\n Please enter a number: ";
cin>> x;
cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
primenum(x); //function invocation...
cout<<endl<<"\nThere are "<<c
<<" prime numbers less than or equal to "<<x<<".\n\n";
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;
}
getchar();
}
 
</pre>
 
=== Assignment 3 ===
1
edit