d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 100 Fg To Find Out What's Wrong
Add Reply New Topic New Poll
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Apr 20 2013 01:48am
This is my program for project euler #23 -- http://projecteuler.net/problem=23
Quote
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 is 4179871, but my program is showing 3796959, can anyone see why?


Quote
#include <iostream>
#include <cmath>

using namespace std;

int hold[28124];
int abundant[7000] = {0};

bool abundance(int x){      <-- this function shows if number is abundant, works as intended
    int counter = 0;
    int counter2 = 0;
    for (int i = 2; i < sqrt(x); i++){
        if (x % i == 0){
            counter += i;
            counter += x / i;
          }
    }
    int y = sqrt(x);
    if (x % y == 0){
            counter += sqrt(x);
        }
    if (counter > x){
        return true;
    } else {
        return false;
    }
}

int main()
{
    int counter = 0;                 
    for (int i = 0; i < 28124; i++){        <-- fills hold[0] ... hold[28123] with numbers 0 ... 28123, by default all numbers aren't sums of 2 abundant numbers (up to 28123 )
        hold[i] = i;
    }
    for (int j = 10; j < 28124; j++){    <-- fills array abundant with abundant numbers, works just fine (12, 18, 20, 24, 30, 36, 40, etc.)
        if (abundance(j) == true){                final value = 28122, no need for more because 28122 + 12 is already more than 28123
            abundant[counter] = j;
            counter++;
        }
    }

    for (int m = 0; m < 7000; m++){    <-- adds every possible abundant number combination, 12 + 12, 12 + 18 ... 12 + 28122 --- 18 + 12, 18 + 18 ... 18 _ 28122, etc
        for (int n = 0; n < 7000; n++){
            if (abundant[m]+abundant[n] < 28124){
                hold[abundant[m]+abundant[n]] = 0;  <-- for every sum, sets the corresponding element in hold[ ] to 0, any left over CAN'T be the sum of 2 abundant numbers
            }
        }
    }
    long long counter2 = 0;                            <-- lastly, just add all the values together like the program asks
    for (int x = 0; x < 28124; x++){
        counter2 += hold[x];
    }
    cout << counter2 << endl;
}


This post was edited by Foxic on Apr 20 2013 01:52am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Apr 20 2013 05:19pm
so why 7000?
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Apr 20 2013 09:01pm
Sorry that was a mistake, for (int m = 0; m < 7000; m++) (int n = 0; n < 7000; n++) should be m < counter, n < counter instead. But that doesn't solve my problem.
Also counter only goes up to 6919 so having more doesn't do anything.

This post was edited by Foxic on Apr 20 2013 09:03pm
Member
Posts: 30,717
Joined: Sep 19 2007
Gold: 254.00
Apr 20 2013 09:06pm
your abundance function is missing 1 as a divisor
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Apr 20 2013 10:41pm
Quote (J_B @ Apr 20 2013 10:06pm)
your abundance function is missing 1 as a divisor


You're right, but that still gives me the same answer, 3797954. I just change counter = 0 to counter = 1, because every number is divisible by 1. Unfortunately didn't help :(
Member
Posts: 30,717
Joined: Sep 19 2007
Gold: 254.00
Apr 21 2013 12:06am
Code
#include <iostream>

enum { ABUNDMAX = 28124, ABUNDMIN = 12, ABUNDVALSMAX = 7000 };

bool isAbundant(int x){
int divSum = 1;
for (int i = 2; i <= x/2; i++){
 if (x % i == 0){
  divSum += i;
 }
}

return divSum > x;
}

int main()
{
int isAbundSum[ABUNDMAX] = {};
int abundantNums[ABUNDVALSMAX] = {};

int abundVals = 0;
for (int j = ABUNDMIN; j < ABUNDMAX; j++){
 if (isAbundant(j)){
  abundantNums[abundVals++] = j;
 }
}

for (int m = 0; m < abundVals; m++){  
 for (int n = m; n < abundVals; n++){
  int sum = abundantNums[m] + abundantNums[n];
  if (sum < ABUNDMAX){
   isAbundSum[sum] = 1;
  }
 }
}

long long nonAbundSum = 0;                      
for (int x = 0; x < ABUNDMAX; x++){
 nonAbundSum += (1 - isAbundSum[x]) * x;
}

std::cout << nonAbundSum << std::endl;
}


I made it purdy
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Apr 21 2013 12:26am
I still have to find the problem in mine though :(
Member
Posts: 2,769
Joined: Dec 24 2009
Gold: 14.00
Apr 22 2013 03:08am
Found out the problem, don't need help anymore
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll