# Erroneous answer in greatest divisor algorithm

This is a discussion on Erroneous answer in greatest divisor algorithm within the C Programming forums, part of the General Programming Boards category; Hi All, I am writing a program to find the number between 1 and 100 that has the greatest number ...

1. ## Erroneous answer in greatest divisor algorithm

Hi All,

I am writing a program to find the number between 1 and 100 that has the greatest number of distinct divisors. Below is my code for the same. It is returning an erroneous answer, i.e. 8 for greatest number of distinct divisors and 4 as the number for which the greatest divisors are occurring:

Please point out modifications and mistakes. Thank you.

Code:
```/*program to find a number in the range 1 to 100 that has the largest number of distinct divisors*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//this loop counts the number of distinct divisors

int main()
{
int countstore[100]={0},result,i,j,num,halfnum,count=1,index;
for(num=4;num<=100;num++)
{
halfnum=floor(num/2);
count=1;
for(i=2;i<=halfnum;i++)
{
if(num%i==0)
{
count+=1;
}
}
for(j=1;j<=97;j++)
{
countstore[j]=count;
}
}

//result stores the number of distinct divisors, index stores the number for which the greatest distinct divisors have been found

result=0;
for(i=1;i<=97;i++)
{
if(result<countstore[i])
{
result=countstore[i];
index=i+3;
}
}
printf("the integer between 0 and 100 with %d number of distinct divisors is %d",result,index);
system("pause");
return 0;
}```

2. On line 12, what is the type of the parameter to floor() function? What is the type of the expression num/2?

What is the purpose of the loop on lines 21 through 24?

What is the purpose of the variable index?

3. FYI: Throwing away almost working code and starting from scratch is usually a bad idea.

Edit: I have no plans to help you anymore; you do not seem to be learning from me.
I will let one of the better people try to explain how to program to you.
I have no idea how to get across the ideas that functions are good to a person who does not see it without me telling them.

Tim S.

4. @Stahta,

I haven't started from scratch:

1. This is another problem, the previous one in the thread "Erroneous answer in algorithm problem" is solved, thanks to you.
2. I earlier incorporated functions in this program, but later realized that since it is a single iteration problem, with known input and single output, I won't be needing functions.

@pheininger

On line 12, what is the type of the parameter to floor() function? What is the type of the expression num/2?

1. The type of parameter to the floor function is integer, actually I realized that I don't need the floor function for conversion to integer as dividing by 2 automatically returns and integer answer.

What is the purpose of the loop on lines 21 through 24?

2. To store the count of number of exact divisor of each integer between 4 and 100. For example, for integer 4 the count should be 2. I just realized that i should be incrementing the index without using the for loop.

What is the purpose of the variable index?

3. Index stores the number for which the greatest number of distinct divisors have been found.

5. ...
@pheininger

On line 12, what is the type of the parameter to floor() function? What is the type of the expression num/2?

1. The type of parameter to the floor function is integer, ...

No, the type of the parameter to the floor() function is float. Your expression is an integer.

What is the purpose of the loop on lines 21 through 24?
2. To store the count of number of exact divisor of each integer between 4 and 100. For example, for integer 4 the count should be 2.

I do not think the loop on lines 21 through 24 does anything close to what you said is your goal. It's purpose seems to be to store the current value of count in positions 1 through 97 of the countstore[ ] array . I cannot think of any reason why you would want to do that.

I just realized that i should be incrementing the index without using the for loop.

I do not know why you need to "be incrementing the index" but yes are right about "without using the for loop." Or that specific for loop.

What is the purpose of the variable index?

3. Index stores the number for which the greatest number of distinct divisors have been found.

I am not sure you are wrong, but I am not sure you are right. Since you seem to be indexing into countstore[ ] with two different bases.

Another set of questions:

How many positions are there in the countstore[ ] array?

What is the smallest index into the countstore[ ] array?

What is the largest index into the countstore[ ] array?