Results 1 to 3 of 3

Thread: Logic problem with

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Location
    New England USA
    Posts
    13

    Angry

    I'm using Visual C++ 6.0. The following is the code supposed to display all "perfect numbers" up to 1000, and also display their divisors. The given definition of a perfect number is: Any number whose divisors add up to that number (not including itself). The number 6 is an example - its divisors are 1, 2, and 3. And 1+2+3=6.

    It's giving me prime numbers only (I'll use this for my next assignment, which is to display prime numbers!). I can't find the error in the logic, I've tweaked it a few different ways. There are no compile errors, so at least I know I got the syntax right this time.

    Here's the code (I can't get the boards to accept the formatting so it'll all line up on the left):

    #include <iostream.h>

    int main()
    {
    for (int i=2; i<1000; i++) { //use i as counter, start it at 2. Go up to 1000 by 1's.
    int sumDiv = 0;{ //start sumDiv with 0 value.
    for (int d=1; d<i; d++){ //use d as divisor counter, start at 1. Increase by
    //1's as long as its value is less than i.
    if ( (i%d)==0) //if divisor, divided by i, results in no remainder,
    sumDiv+=i; //then add the divisor value to sumDiv variable.
    }
    if (sumDiv==i){ //if sumDiv has same value as i, then print
    cout<<i<<" is a perfect number!"<<endl; //the statement to the left
    cout<<endl;
    }
    }

    cout<<"Proper Divisors"<<endl;
    for (int d=1; d<i; d++){ //begins loop to set up output of divisors
    if ( (i%d)==0){ //mods to check if divisor is real divisor of i
    if (i==1)
    cout<<d<<", ";
    }
    }
    cout<<"are the proper divisors!"<<endl<<endl;
    }
    cout<<endl;
    return 0;
    }

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No idea about the algorithm, but to format code enclose it in [code][/code] tags.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Try This for the top part of the algorithm:
    Code:
    #include <iostream.h> 
    #include <math.h>
    int main() 
    { 
    for (int i=2; i<1000; i++)
    { 
        int sumDiv = 0;
        for (int d=1; d<pow(i,.5); d++)
        { 
    	if ( (i%d)==0)
            {
                if (d!=1)
    		sumDiv+=(i/d);
    	    sumDiv+=d; //then add the divisor value to sumDiv variable. 
             }
        } 
        if (sumDiv==i)
        { //if sumDiv has same value as i, then print 
    	cout << i << " is a perfect number!"<<endl; //the statement to the left 
    	cout << endl;
        }
        //cout << i << endl;
        
    } 
    
    }
    You were adding the number(i) to sumDiv when you should have been adding d. I also optimized the code a little by only going to the square root of the number and taking both the numbers from the product.

    [Edited by Negative0 on 11-16-2000 at 04:08 PM]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width