Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project Euler #23, can't find the issue in program

Link : http://projecteuler.net/problem=23

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


The answer to the problem is 4179871, my program shows 3797954.

First and foremost, I make a function to fill array abundant[ ] with all abundant numbers below 28124. This works perfectly fine, because I googled abundant numbers and they match up exactly with my array.

Secondly, I have another array with all numbers 1-28123, I assume ALL of them "cannot be written as the sum of two abundant numbers." These are all written into array hold[ ].

Finally, I get rid of the numbers that CAN be written as the sum of two abundant numbers, by adding all numbers in abundant[ ] with all numbers in abundant[ ], and setting that value of hold[ ] to 0. (hold[abundant[0 to n]+abundant[0 to n]] = 0) Add all remaining numbers in hold[ ], I only get 3797954

I know this program isn't very efficient, since it adds all abundant numbers to all abundant numbers, but it should work just fine. What's wrong with it?


#include <iostream>
#include <cmath>

using namespace std;

int hold[28124];
int abundant[7000]; //hold abundant numbers, there are only 6919 abundant numbers below 28123

bool abundance(int x){  //returns true if x is abundant
    int counter = 1;    //holds "proper divisors" of numbers, default by 1 because every int is divisible by 1
    for (int i = 2; i < sqrt(x); i++){   //finds all divisors 2 - sqrt(n)
        if (x % i == 0){
            counter += i;
            counter += x / i;
        }
    }
    int y = sqrt(x);   
    if (x % y == 0){   //adds sqrt(n) if its modulus to n is 0
            counter += sqrt(x);
        }
    if (counter > x){
        return true;
    } else {
        return false;
    }
}

int main()
{
    int counter = 0;
    for (int i = 0; i < 28124; i++){ //assumes every number cannot be written as the sum of two abundant numbers,
        hold[i] = i;                 //goes up to 28123 because "it can be shown that all integers greater
    }                                //than 28123 can be written as the sum of two abundant numbers." - project euler
    for (int j = 10; j < 28124; j++){ 
        if (abundance(j) == true){  //copies all abundant numbers up to 28123 to abundant[]
            abundant[counter] = j;
            counter++;
        }
    }

    for (int m = 0; m < counter; m++){  //adds all numbers in abundant[], with all numbers in abundant[]
        for (int n = 0; n < counter; n++){
            if (abundant[m]+abundant[n] < 28124){
                hold[abundant[m]+abundant[n]] = 0; //sum of the abundant numbers in hold[] is set to 0
            } //hold[] now holds all numbers that cannot be written as the sum of 2 abundant numbers
        }
    }
    int counter2 = 0;
    for (int x = 0; x < 28124; x++){
        counter2 += hold[x];
    }
    cout << counter2 << endl;

}
like image 467
xyz Avatar asked Apr 21 '13 05:04

xyz


1 Answers

The problem is in your abundance function, specifically this part:

int y = sqrt(x);   
if (x % y == 0){   //adds sqrt(n) if its modulus to n is 0
    counter += sqrt(x);
}

x % (int)sqrt(x) == 0 does not imply that sqrt(x) is an integer. A simple counterexample is 2. sqrt(2) is about 1.414, or just 1 as an integer. But 2 % 1 == 0, even though it isn't the square root.

So to fix your code, change that part to:

int y = sqrt(x);   
if (y * y == x){   //adds sqrt(n) if sqrt(n) is an integer
    counter += y;
}

And you will get the correct results.

like image 199
Blender Avatar answered Oct 19 '22 05:10

Blender