Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. /***************************************************
  2. Student Name: Samantha Eaton
  3. Prof/section: Bielajew/300
  4. GSI section: cat/306
  5. File: hwk5b.cpp
  6. Purpose: To find the "perfect" numbers
  7. ***************************************************/
  8.  
  9. #include <iostream>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. //function that finds the prime number.
  15. bool perfectNumber(int A)
  16. { int c = 0, N = 0, B;
  17.  
  18. //uses equation to find prime number.
  19. B = static_cast<int>(pow((float)2,(float)A) -1.0);
  20.  
  21. bool isPrime, isPerfect=0;
  22.  
  23. for (int i = 2; i < B; i = i + 1)
  24. {
  25. if (B%i == 0)
  26. {
  27. isPrime = 1;
  28. c = c + 1;
  29. }
  30. }
  31.  
  32. //else is prime
  33. if (c == 0)
  34. {
  35. isPrime = 0;
  36. }
  37.  
  38. //Perfect number.
  39. if (isPrime == 1)
  40. {
  41. N= static_cast<int>( pow((float)2,(float)(A - 1.0)) )*
  42. static_cast<int>(( pow((float)2,(float)A) - 1.0) );
  43.  
  44. //even number.
  45. if (N%2 == 0)
  46. {
  47. isPerfect = 1;
  48. }
  49. }
  50. return isPerfect;
  51. }
  52.  
  53.  
  54. void printPerfect(int N)
  55. {
  56. cout << N << " is a perfect number" << endl;
  57. cout << N << " = " << "1";
  58.  
  59. //Finds divisable numbers.
  60. for (int i = 2; i < N; i = i + 1)
  61. {
  62. if (N%i == 0)
  63. {
  64. cout << " + " << i;
  65. }
  66. }
  67. cout << endl;
  68.  
  69. return;
  70. }
  71. /***************************************************
  72. Purpose: Find the first 5 perfect numbers
  73. Recieves: nothing
  74. Returns: outputs perfect numbers and what they are made of
  75. ****************************************************/
  76. int main(void)
  77. {
  78. bool Perfect;
  79. int N = 0, count = 5, A = 1;
  80. while (count <= 1)
  81. {
  82. Perfect = perfectNumber(A);
  83. if (Perfect==1)
  84. {
  85. N= static_cast<int>( pow((double)2,((double)A - 1.0)) )*
  86. static_cast<int>( ( pow((double)2,(double)A) - 1.0) );
  87. count = count + 1;
  88. printPerfect(N);
  89. }
  90. A = A + 1;
  91. }
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement