Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <math.h>
- #include <vector>
- #include <algorithm>
- #define N 10000
- using namespace std;
- // Auxiliary Functions
- vector <int> findDivisors(unsigned int n){
- vector <int> divisors = vector <int> ();
- divisors.push_back(1);
- for(int i=2; i<=n/2; i++){
- if(n % i == 0){
- divisors.push_back(i);
- }
- }
- return divisors;
- }
- bool isPrime(unsigned int n){
- // Prime numbers have only 2 divisors: 1 and n
- // If I use the above function, the returned vector will contain only 1 divisor, specifically only the number 1
- // Because I don't push back n as a divisor of number n
- vector <int> divisors = findDivisors(n);
- if(divisors.size() == 1){
- return true;
- }
- else{
- return false;
- }
- }
- int main()
- {
- /*
- // Check the function
- for(int i=2; i<20; i++){
- vector <int> v = vector <int> ();
- v = findDivisors(i);
- cout << i << " ----> ";
- for(unsigned int j=0; j<v.size(); j++){
- cout << v[j] << " ";
- }
- cout << endl;
- }
- */
- vector <int> filioi = vector <int> ();
- for(int i=2; i<=N; i++){
- // First, I have to find the divisors of teach number i that I scan with the for-loop
- vector <int> divisors1 = findDivisors(i);
- int sum1 = 0;
- for(unsigned int j=0; j<divisors1.size(); j++){
- sum1 += divisors1[j];
- }
- // I will find the divisors of the sum1
- // But first I have to check if it is prime
- // Because if it is prime, I will have only 1 divisor (number 1)
- if(isPrime(sum1)){
- continue;
- }
- vector <int> divisors2 = findDivisors(sum1);
- int sum2 = 0;
- for(unsigned int j=0; j<divisors2.size(); j++){
- sum2 += divisors2[j];
- }
- // Check if sum2 (sum of divisors of sum1) is equal to the number i
- bool flag1 = (i == sum2);
- bool flag2 = (i == sum1);
- bool pairHasBeenInserted = false;
- if(filioi.size() != 0){
- for(unsigned int j=0; j<filioi.size(); j++){
- if(i == filioi[j]){
- pairHasBeenInserted = true;
- break;
- }
- }
- }
- //cout << i << " ----> " << "flag1 = " << flag1 << ", flag2 = " << flag2 << endl;
- // Now, I have to check if I have a perfect number
- // In this case, i = sum2 but also i = sum1, because I have a perfect number and I don't want to
- // include this case in my problem
- if(flag1 == true && flag2 == false && pairHasBeenInserted == false){
- filioi.push_back(i);
- filioi.push_back(sum1);
- }
- }
- // The process for finding the pairs of filioi numbers has finished
- cout << "Filioi Arithmoi: " << endl;
- int counter = 0;
- for(unsigned int i=0; i<filioi.size(); i+=2){
- counter++;
- cout << "Pair " << counter << ": " << filioi[i] << ", " << filioi[i+1] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement