Thread: Erroneous answer in greatest divisor algorithm

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    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. #2
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    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. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: Throwing away almost working code and starting from scratch is usually a bad idea.

    Erroneous answer in algorithm problem

    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.
    Last edited by stahta01; 05-26-2012 at 07:48 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    @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.

    Thank you for your help!

    @pheininger

    Below are the answers to your question:

    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.

    Thanks for your help.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    ...
    @pheininger

    Below are the answers to your question:

    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Erroneous answer in algorithm problem
    By abhishekcoder in forum C Programming
    Replies: 6
    Last Post: 05-26-2012, 07:25 AM
  2. Erroneous answer in reciprocal number algorithm
    By abhishekcoder in forum C Programming
    Replies: 4
    Last Post: 05-06-2012, 01:05 AM
  3. Erroneous answer in algorithm problem
    By abhishekcoder in forum C Programming
    Replies: 13
    Last Post: 05-05-2012, 02:09 PM
  4. The greatest common divisor (GCD) help..
    By damha in forum C Programming
    Replies: 4
    Last Post: 04-09-2011, 05:18 AM
  5. Greatest Common Divisor.....
    By muran_pling in forum C++ Programming
    Replies: 10
    Last Post: 12-18-2006, 05:02 AM

Tags for this Thread