Thread: Erroneous answer in algorithm problem

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

    Erroneous answer in algorithm problem

    Hi All,

    I am writing a program to find the smallest positive integer that has n or more divisors. My program gives me correct answer for input values upto 4 but it produces erroneous answers for input values above 4. For example, it gives 10 as integer having 5 divisors).

    Please point out errors or modifications. Below is the program:

    Code:
    /*program to find smallest positive integer that has n or more divisors excluding itself*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int main()
    {
    int n,i,posinteger=1,count=0,halfposinteger=0;
    printf("enter the number of divisors");
    scanf("%d",&n);
    if(n<=4)
    {
            for(i=1;i<=n;i++)
            {
                posinteger*=i;
            }
    }
    else
    {
        posinteger=8;
        while(count!=n)
        {
            posinteger+=1;
            halfposinteger=floor(posinteger/2);
            for(i=1;i<=halfposinteger;i++)
            {
                 if(posinteger%i==0)
                 {
                        count+=1;            
                 }
            }
        }   
    }
    printf("the smallest positive integer with %d divisors is %d",n,posinteger);
    system("pause");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest stating the output that is correct; because I think I misread your question.

    Ignore the rest of my post, I think I am solving a different problem than the one you asked.

    In case you did not realize it this is a subset of Prime factor problem. Prime factor - Wikipedia, the free encyclopedia

    FYI: I really think you need to use user defined functions to solve this problem in a readable manner.

    Edit: I just realized this problem is a one line program; write down the answers to the first 5 cases; do you see a pattern.

    Tim S.
    Last edited by stahta01; 05-25-2012 at 10:46 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

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's probably because you shouldn't use floor.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Every time you test a new number you have to reset count back to zero.

    After major re-write of your code I got this output.

    FYI: the number 1 has no whole number divisors when 1 is not counted.

    Code:
    the smallest positive integer with 1 divisors is 2
    the smallest positive integer with 2 divisors is 4
    the smallest positive integer with 3 divisors is 6
    the smallest positive integer with 4 divisors is 12
    the smallest positive integer with 5 divisors is 12
    the smallest positive integer with 6 divisors is 24
    the smallest positive integer with 7 divisors is 24
    the smallest positive integer with 8 divisors is 36
    the smallest positive integer with 9 divisors is 48
    Test framework I used; I have no plans to post my smallestWithDifferentDivisors function.
    Code:
    /*program to find smallest positive integer that has n or more different divisors excluding itself*/
    #include<stdio.h>
    #include<stdlib.h>
    
    int smallestWithDifferentDivisors(unsigned int  minCount);
    
    int main() {
        int i=0;
        int result=0;
    
        for (i=1; i<10; i++) {
            result = smallestWithDifferentDivisors(i);
            printf("the smallest positive integer with %d divisors is %d\n",i,result);
        }
        return 0;
    }
    Tim S.
    Last edited by stahta01; 05-25-2012 at 12:02 PM.
    "...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

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Hi Stahta,

    Resetting count solves my problem. Thank you.

    Quote Originally Posted by stahta01 View Post
    Every time you test a new number you have to reset count back to zero.

    After major re-write of your code I got this output.

    FYI: the number 1 has no whole number divisors when 1 is not counted.

    Code:
    the smallest positive integer with 1 divisors is 2
    the smallest positive integer with 2 divisors is 4
    the smallest positive integer with 3 divisors is 6
    the smallest positive integer with 4 divisors is 12
    the smallest positive integer with 5 divisors is 12
    the smallest positive integer with 6 divisors is 24
    the smallest positive integer with 7 divisors is 24
    the smallest positive integer with 8 divisors is 36
    the smallest positive integer with 9 divisors is 48
    Test framework I used; I have no plans to post my smallestWithDifferentDivisors function.
    Code:
    /*program to find smallest positive integer that has n or more different divisors excluding itself*/
    #include<stdio.h>
    #include<stdlib.h>
    
    int smallestWithDifferentDivisors(unsigned int  minCount);
    
    int main() {
        int i=0;
        int result=0;
    
        for (i=1; i<10; i++) {
            result = smallestWithDifferentDivisors(i);
            printf("the smallest positive integer with %d divisors is %d\n",i,result);
        }
        return 0;
    }
    Tim S.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Hi Stahta,

    My program gives the following results to me:

    the smallest positive integer with 1 distinct divisors is 1
    the smallest positive integer with 2 distinct divisors is 2
    the smallest positive integer with 3 distinct divisors is 6
    the smallest positive integer with 4 distinct divisors is 12
    the smallest positive integer with 5 distinct divisors is 12
    the smallest positive integer with 6 distinct divisors is 24
    the smallest positive integer with 7 distinct divisors is 24
    the smallest positive integer with 8 distinct divisors is 36
    the smallest positive integer with 9 distinct divisors is 48


    Below is my modified code:
    Code:
    /*program to find smallest positive integer that has n or more distinct divisors excluding itself*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    int smallestWithDistinctDivisors(unsigned int  minCount);
    int main()
    {
        int i=0;
        int result=0;
     
        for (i=1; i<10; i++)
        {
            result = smallestWithDistinctDivisors(i);
            printf("the smallest positive integer with %d distinct divisors is %d\n",i,result);
        }
        system("pause");
        return 0;
    }
    
    int smallestWithDistinctDivisors(unsigned int  minCount)
    {
    int n,i,posinteger=1,count=0,halfposinteger=0;
    if(minCount<4)
    {
            for(i=1;i<=minCount;i++)
            {
                posinteger*=i;
            }
    }
    else
    {
        posinteger=8;
        while(count<minCount)
        {
            count=1;           
            posinteger+=1;
            halfposinteger=floor(posinteger/2);
            for(i=2;i<=halfposinteger;i++)
            {
                 if(posinteger%i==0)
                 {
                        count+=1;            
                 }
            }
        }   
    }
    return posinteger;
    }
    Thanks for your help.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What are the divisors for 1 and 2.

    For one the divisor is 1; but, your rules says you can not count 1. Therefore count is zero.
    For two the divisors are 1 and 2; but, your rules says you can not count 2. Therefore count is one.

    Tim S.
    "...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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Erroneous answer in reciprocal number algorithm
    By abhishekcoder in forum C Programming
    Replies: 4
    Last Post: 05-06-2012, 01:05 AM
  2. Erroneous answer in algorithm problem
    By abhishekcoder in forum C Programming
    Replies: 13
    Last Post: 05-05-2012, 02:09 PM
  3. an answer raises another problem...
    By Dorky King in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 03:11 PM
  4. stupid problem. answer = 0.0
    By spydrvnm in forum C Programming
    Replies: 6
    Last Post: 09-26-2004, 10:53 AM
  5. problem with answer going to default?
    By Patrick1234 in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2002, 09:11 AM

Tags for this Thread