Just some tips to make for faster and more concise code:
In your bool abundant(int):
1 2 3
|
if(factors_sum > num)
return true;
return false;
|
Also do not check all the factors in the number. Just check all the numbers within a range equal to the square root of the number you are checking. i.e.
line 56:
for(int i = 1; i < sqrt(num) ;)
Or
for(int i = 1; i < pow(num, 0.5) ;)
I don't understand what you are doing in line 20. Why are you setting all the previous values to -2 wouldn't this just in the end fill every spot in the array with -2 except for the last value stored?
A few more words:
1. The question is asking you to find the sum of all the numbers
below 28123 which cannot be written as the sum of 2 abundant numbers.
2. So if I were solving this question, I would first get all the abundant numbers who their sum is less than 28123. i.e. fill an array with 24, 32, 36, 40, 48, etc.
3. Then I would create a nested for-loop. The first for-loop in the nest will go through the values 1-28123 and the for-loop below will go through the array of abundant sums.
-if the value in the first for-loop is not equal to the value in the one below, continue adding the numbers from the first for-loop
-else skip that number and continue
Also try using dynamically allocated arrays:
int *Array = new int[1];
and you can resize it as you wish, you can use this function if you want:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
int * Resize(int arr[], int length, int val)
{
int * array2 = new int [length + 1];
for (int J = 0; J < length; ++J)
array2[J] = arr[J];
array2[length] = val;
delete[] arr;
arr = NULL;
return array2;
}
|
ex
Array = Resize(Array, size, num)