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.

/*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;
}

Commented where I thought necessary - untested

/*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>

int main()
{
    //note: excludes divisors of 1 and the number itself

    int countstore[101] = {0};
    int result, i, num, halfnum, count, index;

    for (num = 4; num <= 100; num++)
    {
        halfnum = num/2;
        count = 0;                      
        for (i = 2; i <= halfnum; i++)
        {
            if(num % i == 0)
            {
                count += 1;
            }
        }
        // store
        countstore[num] = count;                
    }

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

    result = 0;
    //note: this does not contain all possible results as numbers with equal greatest amount of divisors
    //      will not be included.

    for (i = 4; i <= 100; i++)
    {
        if (result < countstore[i])
        {
            result = countstore[i];
            index = i;
        }
    }

    printf("the integer between 0 and 100 with %d number of distinct divisors is %d\n\n", result, index);

    system("pause");

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.