Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. // Dave's Prime Number Finder
  2. //
  3. // Source File : primeNumberTheorem.cpp
  4. // Author : David J Beaty
  5. // Date :
  6. // Course : CS161 (Summer Term)
  7. // Sources : Classmate Jesse Wagner
  8.  
  9.  
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <math.h>
  13.  
  14. using namespace std;
  15.  
  16. bool isPrime (long long n);
  17. double primeGen (long long n);
  18.  
  19. int main () {
  20.  
  21. // **** PART I ****
  22.  
  23. // initialize variables
  24. int i = 3;
  25. int j = 1;
  26.  
  27. cout << "| 2 | ";
  28. // generate odd integers > 1 as candidates to be prime
  29. while (i <= 541) {
  30. // for each candidate integer, test whether it
  31. // is prime. if it is, display it.
  32. if(isPrime(i)) {
  33. if(j%10 == 0){
  34. cout << endl;
  35. }
  36. j ;
  37. cout << "| " << setw(3) << i << " | ";
  38. }
  39. // adding two each time so as only to generate odd #s
  40. i = 2;
  41.  
  42. }
  43.  
  44. cout << endl << endl;
  45.  
  46. // **** PART II ****
  47.  
  48. // initialize variables
  49. int max = 100;
  50. double sumLogs = 0;
  51. double ratio = 0.0;
  52.  
  53. cout << fixed << setprecision(5)
  54. << setw(15) << "Sum of logs" << setw(11) << 'n'
  55. << setw(9) << "Ratio" << endl;
  56. // generate odd integers > 1 as candidates to be prime
  57. while (max <= 10000000) {
  58. // generate output
  59. sumLogs = primeGen(max);
  60. ratio = sumLogs / max;
  61. cout << setw(15) << sumLogs << setw(11) << max
  62. << setw(9) << ratio << endl;
  63. // call max while loop
  64. max *= 10;
  65.  
  66. }
  67.  
  68. getchar();
  69. getchar();
  70. return 0;
  71. }
  72.  
  73. // isPrime function (checks whether candidate is prime)
  74. bool isPrime (long long n) {
  75. int i = 2;
  76. int max = 0;
  77. // max is the square root of the candidate int
  78. max = (int)sqrt((long double) n);
  79.  
  80. while (i <= max) {
  81. if(n%i == 0) {
  82. return false;
  83. }
  84. i ;
  85. }
  86. return true;
  87. }
  88.  
  89. // function that runs the numbers through isPrime and
  90. double primeGen (long long n) {
  91. int i = 3;
  92. double currentSum = log(2.0);
  93.  
  94. while (i <= n) {
  95. // for each candidate integer, test whether it
  96. // is prime. if it is, display it.
  97. if(isPrime(i)) {
  98. // get the log and add it to running total
  99. currentSum = log((double) i);
  100. }
  101. // adding two each time so as only to generate odd #s
  102. i = 2;
  103. }
  104.  
  105. return currentSum;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement