Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************
- Student Name: Samantha Eaton
- Prof/section: Bielajew/300
- GSI section: cat/306
- File: hwk5b.cpp
- Purpose: To find the "perfect" numbers
- ***************************************************/
- #include <iostream>
- #include <cmath>
- using namespace std;
- //function that finds the prime number.
- bool perfectNumber(int A)
- { int c = 0, N = 0, B;
- //uses equation to find prime number.
- B = static_cast<int>(pow((float)2,(float)A) -1.0);
- bool isPrime, isPerfect=0;
- for (int i = 2; i < B; i = i + 1)
- {
- if (B%i == 0)
- {
- isPrime = 1;
- c = c + 1;
- }
- }
- //else is prime
- if (c == 0)
- {
- isPrime = 0;
- }
- //Perfect number.
- if (isPrime == 1)
- {
- N= static_cast<int>( pow((float)2,(float)(A - 1.0)) )*
- static_cast<int>(( pow((float)2,(float)A) - 1.0) );
- //even number.
- if (N%2 == 0)
- {
- isPerfect = 1;
- }
- }
- return isPerfect;
- }
- void printPerfect(int N)
- {
- cout << N << " is a perfect number" << endl;
- cout << N << " = " << "1";
- //Finds divisable numbers.
- for (int i = 2; i < N; i = i + 1)
- {
- if (N%i == 0)
- {
- cout << " + " << i;
- }
- }
- cout << endl;
- return;
- }
- /***************************************************
- Purpose: Find the first 5 perfect numbers
- Recieves: nothing
- Returns: outputs perfect numbers and what they are made of
- ****************************************************/
- int main(void)
- {
- bool Perfect;
- int N = 0, count = 5, A = 1;
- while (count <= 1)
- {
- Perfect = perfectNumber(A);
- if (Perfect==1)
- {
- N= static_cast<int>( pow((double)2,((double)A - 1.0)) )*
- static_cast<int>( ( pow((double)2,(double)A) - 1.0) );
- count = count + 1;
- printPerfect(N);
- }
- A = A + 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement