I am trying to do Euler Problem 23.
The first block of code generates a list of abundant numbers and saves them to an array called abundant, while storing all the integers in an array called numbers.
The next block zeros the array numbers at the sum of two abudant numbers, leaving a list of non abundant numbers.
Somewhere in this block the abundant array is becoming corrupted, but as far as I know I am not assigning anything to the abundant array in this block.
Can someone indicate where this assignment is taking place.
I've searched the web and several forums but to no avail.
The first few lines on the printout shows that the fifth element of the abundant array changes from 30 to zero sometime during the first pass.
Code:
0 12, 0 12 24
0 12, 1 18 30
0 12, 2 20 32
0 12, 3 24 36
0 12, 4 30 42
0 12, 5 36 48
0 12, 6 40 52
1 18, 0 12 30
1 18, 1 18 36
1 18, 2 20 38
1 18, 3 24 42
1 18, 4 0 18
1 18, 5 36 54
1 18, 6 40 58
2 20, 0 12 32
Code:
# include <stdio.h>
# include <time.h>
int MAX=40; //20161;
int properdivisors(int n){
int i,sum=0;
for (i=1;i<n;i++){
if(n%i==0){sum+=i;}
}return sum;
}
int abundantnos()
{
int i,j=0,k,l;
int sum=0;
int abundant[MAX];
int numbers[MAX];
for (i=1;i<=MAX;i++)
{
numbers[i]=i;
if(properdivisors(i)>i)
{
abundant[j++]=i;
}
}
/* j holds the number of abundant numbers found */
for(i=0;i<j;i++)
{
for(k=0;k<j;k++){
printf("%3d\t%5d, %3d\t%5d\t%6d\n",i,abundant[i],k,abundant[k],abundant[i]+abundant[k] );
l=abundant[i]+abundant[k];
numbers[l]=0;
}
}
for(i=1;i<=MAX;i++)
{
if(numbers[i]!=0)
{
sum+=numbers[i];
}
}
printf("the sum of all the positive integers which cannot\n"
"be written as the sum of two abundant numbers is %d\n",sum);
}
int main()
{
clock_t begin, end;
double time_spent;
begin = clock();
abundantnos();
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf(" and took %f seconds",time_spent);
}