CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Oct 2002
    Location
    Right Behind you
    Posts
    238

    simple math loop program

    I'm new to c++ and pretty new to programming in general. I'm trying to create a program that simply finds the number between 1 and 1000 with the most divisors that produce no remainder.

    I'm not asking anyone to write the program for me but I don't even know where to start except that I will probably need a while loop and a modulus operator. Any help would appreciated.

    Thanks in advance.

    Code:
    #include <iostream.h>
    
    main() {
           int currNum;
           int count;
           
           for(currNum=1; currNum<=1000; currNum++) {
                         
                         }
                         
                         return 0;
                         
                         }
    thats all i have right now
    Last edited by morrowasted; May 18th, 2007 at 12:14 PM.
    "3 out of every 4 people make up 75% of the population"
    ---------------------------------------------
    "If the World didn't suck, we would all fall off"
    ---------------------------------------------
    "No pancake is so flat that it doesnt have two sides"
    ---------------------------------------------

  2. #2
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: simple math loop program

    It would help if you start by developing an algorithm to solve a problem. You can write the algorithm in plain English. Once you have the algorithm in place, it would be relatively easy to convert it into C++.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: simple math loop program

    Underdog hit the nail right on the head. First understand what you want to do. Only when you are sure you could do the exercise on paper, should you start to think about translating it into code.

    Also take a look at my signature below for important information....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Oct 2002
    Location
    Right Behind you
    Posts
    238

    Re: simple math loop program

    this is what i have so far...

    Code:
    //Project 8-3
    //Divisors.CPP
    //By Gregory Bradshaw
    
    #include <iostream.h>
    
    main() {
           int currNum;
           int count;
           int count2;
           count = 0 ; 
           
           
           for(currNum=1; currNum<=1000; currNum++) {
                         for(count2=1; count2<=currNum; count2++) {
                                       if(currNum % count2 == 0) {
                                                  count++;
                                                  }
                                       
                                       }
                                       cout <<  currNum << ": " << count << endl;
                                      
                         }
                        
                         return 0;
                         
                         }

    but the math is wrong or something because the program is saying 1000 has 7069 divisors with no remainder
    "3 out of every 4 people make up 75% of the population"
    ---------------------------------------------
    "If the World didn't suck, we would all fall off"
    ---------------------------------------------
    "No pancake is so flat that it doesnt have two sides"
    ---------------------------------------------

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: simple math loop program

    Quote Originally Posted by morrowasted
    but the math is wrong or something because the program is saying 1000 has 7069 divisors with no remainder
    Question, how many divisors with no remainders (in total) do all the numbers between 1 and 1000 have????? Start counting...

    ...or think about what I asked, and look at your code.

    [hint: one line is in a very wrong place for your question, but is in the right place for mine....]
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: simple math loop program

    I've tried my best to explain the algorithm through comments:
    Code:
    #include <iostream.h>
    #include <memory.h>
    
    ///*
    //	Find an interger between [1-1000] with most divisors.
    //*/
    int main(int,char*) {
    	const unsigned int max_range = 1000; // defines the highest number in the range requested
    	unsigned int divisors[max_range+1]; // array that stores the count of divisors for all numbers between [0-max_range]
    	memset(divisors,0,sizeof(unsigned int)*(max_range+1)); // initialize all counts to zero divizors
    	unsigned int max_divisors = 0; // stores the max number of divisors found; none, initially
    	unsigned int idx_solution = 0; // stores the number with most divisors; zero, initially
    	register unsigned int divisor = 2; // current divisor; first prime, initially
    	unsigned int half_range = max_range / 2; // compute half range
    	while (divisor <= half_range) { // loop within half range of possible divisors
    		register unsigned int has_divisor = divisor << 1; // number that divides to value of 'divisor'
    		while (has_divisor <= max_range) { // increase count for all numbers that divide with 'divisor'
    			++divisors[has_divisor];
    			has_divisor += divisor; // step to next number that divides with 'divisor'
    		}
    		++divisor; // increment possible divisor
    	}
    	register unsigned int idx = max_range; // loop counter for finding max value in 'divisors' array
    	while (idx) { // loop backwards within range and test against last max number of divisors
    		if (max_divisors < divisors[idx]) { // got more divisors than previously
    			max_divisors = divisors[idx]; // save new max divisors value
    			idx_solution = idx; // save the number with most divisors
    		}
    		--idx; // decrement counter; when becomes zero, the loop will finish
    	}
    	cout <<  idx_solution << ": " << max_divisors << endl; // printout solution
    	return 0;
    }
    Regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  7. #7
    Join Date
    Oct 2002
    Location
    Right Behind you
    Posts
    238

    Re: simple math loop program

    Quote Originally Posted by Bornish
    I've tried my best to explain the algorithm through comments:
    Code:
    #include <iostream.h>
    #include <memory.h>
    
    ///*
    //	Find an interger between [1-1000] with most divisors.
    //*/
    int main(int,char*) {
    	const unsigned int max_range = 1000; // defines the highest number in the range requested
    	unsigned int divisors[max_range+1]; // array that stores the count of divisors for all numbers between [0-max_range]
    	memset(divisors,0,sizeof(unsigned int)*(max_range+1)); // initialize all counts to zero divizors
    	unsigned int max_divisors = 0; // stores the max number of divisors found; none, initially
    	unsigned int idx_solution = 0; // stores the number with most divisors; zero, initially
    	register unsigned int divisor = 2; // current divisor; first prime, initially
    	unsigned int half_range = max_range / 2; // compute half range
    	while (divisor <= half_range) { // loop within half range of possible divisors
    		register unsigned int has_divisor = divisor << 1; // number that divides to value of 'divisor'
    		while (has_divisor <= max_range) { // increase count for all numbers that divide with 'divisor'
    			++divisors[has_divisor];
    			has_divisor += divisor; // step to next number that divides with 'divisor'
    		}
    		++divisor; // increment possible divisor
    	}
    	register unsigned int idx = max_range; // loop counter for finding max value in 'divisors' array
    	while (idx) { // loop backwards within range and test against last max number of divisors
    		if (max_divisors < divisors[idx]) { // got more divisors than previously
    			max_divisors = divisors[idx]; // save new max divisors value
    			idx_solution = idx; // save the number with most divisors
    		}
    		--idx; // decrement counter; when becomes zero, the loop will finish
    	}
    	cout <<  idx_solution << ": " << max_divisors << endl; // printout solution
    	return 0;
    }
    Regards,
    thanks for that. i actually figured out my own (much sloppier) version that I think I will use because the prof. might be suspicious about all that stuff we havent covered in class yet

    here is my version:

    Code:
    //Project 8-3
    //Divisors.CPP
    //By Gregory Bradshaw
    
    #include <iostream.h>
    
    main() {
           int currNum;
           int count;
           int count2;
           count = 0 ; 
           int g;
           int numCount[1000];
           int winner;
           winner = 1;
           
           for(currNum=1; currNum<=1000; currNum++) {
                          count = 0;
                         for(count2=1; count2<=currNum; count2++) {
                                       if(currNum % count2 == 0) {
                                                  count++;
                                       
                                                  }
                                       
                                       }
                                       numCount[currNum] = count;
                                       if (numCount[currNum] > numCount[winner]) {
                                                              winner = currNum;
                                                              }
                                       
                         }
                         cout << "Winner: " << winner << " with " << numCount[winner] << " factors.";
                         cin >> g;
                         return 0;
                         
                         }
    your code is helpful for the future though


    edit: our codes are giving the same number for the winner, 840, but my program is saying there are 32 factors and yours is saying there are 30 factors
    Last edited by morrowasted; May 20th, 2007 at 10:15 PM.
    "3 out of every 4 people make up 75% of the population"
    ---------------------------------------------
    "If the World didn't suck, we would all fall off"
    ---------------------------------------------
    "No pancake is so flat that it doesnt have two sides"
    ---------------------------------------------

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: simple math loop program

    Quote Originally Posted by morrowasted
    I think I will use because the prof. might be suspicious about all that stuff we havent covered in class yet
    Does your professor cover things he isn't supposed to teach -- because they're incorrect?
    Code:
    #include <iostream.h>
    Incorrect. The correct header is <iostream>.
    Correction:
    Code:
    #include <iostream>
    using namespace std;
    More incorrect things here:
    Code:
    main() {
    Incorrect. In C++, functions must return a value. Since this is the main() function, it must return an int. Correction:
    Code:
    int main()
    I would say that 85% to 90% of student posts on CodeGuru contains incorrect C++ that have been "taught" by the teacher. So you're not the only one that has these mistakes.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2002
    Location
    Right Behind you
    Posts
    238

    Re: simple math loop program

    i fixed all those things, ill remember them for the future, but my program is still returning a different answer than the other program
    "3 out of every 4 people make up 75% of the population"
    ---------------------------------------------
    "If the World didn't suck, we would all fall off"
    ---------------------------------------------
    "No pancake is so flat that it doesnt have two sides"
    ---------------------------------------------

  10. #10
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: simple math loop program

    Your code is counting 1 and the number itself as factors.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  11. #11
    Join Date
    Jun 2004
    Location
    India
    Posts
    432

    Re: simple math loop program

    And on a side note, C++ array indexing is 0 based. Your code will access the 1001th element in the numCount array.
    Say no to supplying ready made code for homework/work assignments!!

    Please rate this post!

  12. #12
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: simple math loop program

    @morrowasted: did you figure out yet how to remove 1 and the number itself from the count of divisors?
    PS: I knew you'll write your own solution, that's why I gave you a different one, based on a method used to find prime numbers, called "Crivello di Eratostene" (description in italian: http://it.wikipedia.org/wiki/Crivello_di_Eratostene).
    Regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

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

Featured