Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dave's Prime Number Finder
- //
- // Source File : primeNumberTheorem.cpp
- // Author : David J Beaty
- // Date :
- // Course : CS161 (Summer Term)
- // Sources : Classmate Jesse Wagner
- #include <iomanip>
- #include <iostream>
- #include <math.h>
- using namespace std;
- bool isPrime (long long n);
- double primeGen (long long n);
- int main () {
- // **** PART I ****
- // initialize variables
- int i = 3;
- int j = 1;
- cout << "| 2 | ";
- // generate odd integers > 1 as candidates to be prime
- while (i <= 541) {
- // for each candidate integer, test whether it
- // is prime. if it is, display it.
- if(isPrime(i)) {
- if(j%10 == 0){
- cout << endl;
- }
- j ;
- cout << "| " << setw(3) << i << " | ";
- }
- // adding two each time so as only to generate odd #s
- i = 2;
- }
- cout << endl << endl;
- // **** PART II ****
- // initialize variables
- int max = 100;
- double sumLogs = 0;
- double ratio = 0.0;
- cout << fixed << setprecision(5)
- << setw(15) << "Sum of logs" << setw(11) << 'n'
- << setw(9) << "Ratio" << endl;
- // generate odd integers > 1 as candidates to be prime
- while (max <= 10000000) {
- // generate output
- sumLogs = primeGen(max);
- ratio = sumLogs / max;
- cout << setw(15) << sumLogs << setw(11) << max
- << setw(9) << ratio << endl;
- // call max while loop
- max *= 10;
- }
- getchar();
- getchar();
- return 0;
- }
- // isPrime function (checks whether candidate is prime)
- bool isPrime (long long n) {
- int i = 2;
- int max = 0;
- // max is the square root of the candidate int
- max = (int)sqrt((long double) n);
- while (i <= max) {
- if(n%i == 0) {
- return false;
- }
- i ;
- }
- return true;
- }
- // function that runs the numbers through isPrime and
- double primeGen (long long n) {
- int i = 3;
- double currentSum = log(2.0);
- while (i <= n) {
- // for each candidate integer, test whether it
- // is prime. if it is, display it.
- if(isPrime(i)) {
- // get the log and add it to running total
- currentSum = log((double) i);
- }
- // adding two each time so as only to generate odd #s
- i = 2;
- }
- return currentSum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement