Results 1 to 13 of 13
Like Tree4Likes
  • 1 Post By Junky
  • 1 Post By Zyril
  • 1 Post By Norm
  • 1 Post By Zyril

Thread: Counting Divisors from 1 to 10 000

  1. #1
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Cool Counting Divisors from 1 to 10 000

    Making a program that is counting the Divisors from 1 to 10 000.
    My thoughts on it.
    Loop 1, i, go trough 1 to 10 000.
    Loop 2, j, take the current number from loop 1, and then use i%j, check if the result is 0, and then store the highest divisor count.

    here is the code:

    Java Code:
    public class Divisors {
    	public static void main(String[] args) {
    		// Setting up Variables
    		int bufferInt = 0;
    		int bufferDivisors = 0;
    		int integer = 0;
    		int divisor = 0;
    		int howManyNumbers = 10000;
    		
    		for (int i = 1; i<=howManyNumbers;i++){ // loop 1, counting 1 to 1000
    			for (int j = 1; j<=i; j++){ // loop 2, divisor checks
    				
    				if (i%j == 0){ 
    					bufferInt = i;
    					bufferDivisors++;
    					
    					if(bufferDivisors > divisor ){ // saving the higest divisor count resetting the buffers
    						divisor = bufferDivisors;
    						integer = bufferInt;
    						bufferDivisors = 0;
    						bufferInt = 0;
    						System.out.println("Number "+integer+" and had "+divisor+" divosors");
    						
    					}// end if
    				}// end if
    				
    			}//end for2
    		}// end for1
    			
    	} // end main
    }// end Divisors class
    Here are the last lines of the output.

    Java Code:
    Number 9452 and had 419 divosors
    Number 9494 and had 420 divosors
    Number 9535 and had 421 divosors
    Number 9576 and had 422 divosors
    Number 9616 and had 423 divosors
    Number 9658 and had 424 divosors
    Number 9699 and had 425 divosors
    Number 9740 and had 426 divosors
    Number 9780 and had 427 divosors
    Number 9823 and had 428 divosors
    Number 9864 and had 429 divosors
    Number 9905 and had 430 divosors
    Number 9946 and had 431 divosors
    Number 9990 and had 432 divosors
    Doupt that is correct at all. Since if I'm not wrong, then 9990 got 32 divisors not 432.
    So the is the problem in resetting the bufferDivisor bufferInt or in the for loop..

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Counting Divisors from 1 to 10 000

    Have you tested the code with smaller numbers? What was the results?

    Have you tried debugging the code? One way is to add println statements to print out the values of variables as the code executes so you can see what the computer i doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Counting Divisors from 1 to 10 000

    One issue I can see is that bufferDivisors is only reset to zero inside a nested if statement. If either statement is false, bufferDivisors is not reset to zero. It should always be reset between subsequent numbers.
    Juukamen likes this.

  4. #4
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    my calculations show:

    Number 9990 and had 31 divisors

  5. #5
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    Quote Originally Posted by .paul. View Post
    my calculations show:

    Number 9990 and had 31 divisors
    That cannot be correct. The mathematical formula gives 32 divisors. Is your code perhaps omitting the first divisor, 1?
    .paul. likes this.

  6. #6
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    Quote Originally Posted by Zyril View Post
    That cannot be correct. The mathematical formula gives 32 divisors. Is your code perhaps omitting the first divisor, 1?
    no the last divisor... the OP's code is lucky to be 432 as the counts seem to be sequential.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Counting Divisors from 1 to 10 000

    I'd suggest the OP re-do his algorithm and test it with a smaller number like 10 or 20 until it generates the right results.
    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    Quote Originally Posted by Zyril View Post
    That cannot be correct. The mathematical formula gives 32 divisors. Is your code perhaps omitting the first divisor, 1?
    as a matter of interest, what is the formula you used?

  9. #9
    Juukamen is offline Member
    Join Date
    Oct 2011
    Location
    Tromsų
    Posts
    54
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    Quote Originally Posted by Junky View Post
    One issue I can see is that bufferDivisors is only reset to zero inside a nested if statement. If either statement is false, bufferDivisors is not reset to zero. It should always be reset between subsequent numbers.
    Here is the changes
    Java Code:
    for (int i = 1; i<=howManyNumbers;i++){ // loop 1, counting 1 to 1000
    bufferInt = 0;
    bufferDivisors = 0;
    for (int j = 1; j<=i; j++){ // loop 2, divisor checks
    and for the fun of it, I made a change in the IF statment
    Java Code:
    if(bufferDivisors >= divisor ){
    Here is the outut
    Java Code:
    Number 7560 and had 61 divosors
    Number 7560 and had 62 divosors
    Number 7560 and had 63 divosors
    Number 7560 and had 64 divosors
    Number 9240 and had 64 divosors
    The complete program if it will help others in the furure.
    Java Code:
    public class Divisors {
    	public static void main(String[] args) {
    		// Setting up Variables
    		int bufferInt = 0;
    		int bufferDivisors = 0;
    		int integer = 0;
    		int divisor = 0;
    		int howManyNumbers = 10000;
    		
    		for (int i = 1; i<=howManyNumbers;i++){ // loop 1, counting 1 to 1000
    			bufferInt = 0;
    			bufferDivisors = 0;
    			for (int j = 1; j<=i; j++){ // loop 2, divisor checks
    				
    				if (i%j == 0){ 
    					bufferInt = i;
    					bufferDivisors++;
    					
    					if(bufferDivisors >= divisor ){ // saving the higest divisor count resetting the buffers
    						divisor = bufferDivisors;
    						integer = bufferInt;
    						System.out.println("Number "+integer+" and had "+divisor+" divosors");
    						
    					}// end if
    				}// end if
    				
    			}//end for2
    		}// end for1
    			
    	} // end main
    }// end Divisors class

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Counting Divisors from 1 to 10 000

    Is the output correct? It seems strange that the count of divisors goes up by one for each number.

    For easier way to verify the testing set howManyNumbers to 20.

    Comment on loop variables: i & j are not very descriptive. How about:
    Java Code:
            for (int aNbr = firstNbr; aNbr <= lastNbr; aNbr++){ // loop 1, counting 1 to 1000
      
                for (int aDvsr = 1; aDvsr <= aNbr; aDvsr++){    // loop 2, divisor checks
    
                    if (aNbr % aDvsr == 0){
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    Quote Originally Posted by .paul. View Post
    as a matter of interest, what is the formula you used?
    Sorry for not replying earlier to you, I forgot about this thread. Also I apologize for posting an "off topic" reply, even though it has some correlation to the subject of the thread itself.

    This is the method that my algebra teacher taught us when we needed to do some statistical analysis. The method gives you only the numbers of divisors, not which numbers.

    Take the number that you want to find the NUMBER of divisors from.

    Let's start with 80.
    Use prime factorization to form this number.

    2 * 40 = 80
    2 * 2 * 20 = 80
    2 * 2 * 2 * 10 = 80
    2 * 2 * 2 * 2 * 5 = 80 (All factors are PRIME!)

    We can now write this as 2^4 * 5^1, right?
    The exponents are 4 and 1. Add one to each of these exponents and then multiply them. This gives us new exponents 5 and 2, and multiplied together it's 10. Therefor, the number of divisors of 80 is 10.

    ---

    Now, if we want to apply this to a larger number such as 9990, one must be able to remember how many exponents you have, and also be quite good at seeing what number you can divide something with.

    Let's try:

    2 * 4995 = 9990
    Since we can't divide 4995 further on with 2, we go up in prime factors to 3.
    2 * 3 * 1665 = 9990.
    2 * 3 * 3 * 555 = 9990
    2 * 3 * 3 * 3 * 185 = 9990
    Since we can't divide 185 further on with 3, we go up in prime factors to 5.
    2 * 3 * 3 * 3 * 5 * 37 = 9990 (All factors are PRIME!)

    We have 2^1 * 3^3 * 5^1 * 37^1, which gives us the exponents of 1, 3, 1 and 1. Add one to each --> 2, 4, 2, 2. Multiply them together, you get 32. This is the number of divisors of 9990.

    To sum it up, this gives you the formula:

    n = p1a1 * p2a2 * p3a3 * p4a4 * pkak

    Then the number of divisors is:

    Dn = (a1 + 1)(a2 + 1)(a3 + 1)(a4 + 1)...(ak + 1)

    --

    When you've done it a few times it's actually quite easy. Just know your math and multiplication and you will be fine! There are cheats to this aswell! =)
    .paul. likes this.

  12. #12
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    @Zyril. thanks, works great...

  13. #13
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Counting Divisors from 1 to 10 000

    You are welcome!

    If you like math, I think it's quite cool! :P

Similar Threads

  1. Counting Words
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 03-03-2011, 01:12 PM
  2. Counting words
    By Wasley in forum New To Java
    Replies: 9
    Last Post: 01-30-2011, 10:57 PM
  3. problems with counting
    By worsethenu in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 02:33 PM
  4. Counting help
    By jksmithson in forum New To Java
    Replies: 1
    Last Post: 11-06-2009, 02:43 AM
  5. Counting characters
    By Tiff89 in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 09:21 AM

Tags for this Thread

Posting Permissions

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