Results 1 to 13 of 13
 08152012, 08:53 PM #1Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
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
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
So the is the problem in resetting the bufferDivisor bufferInt or in the for loop..
 08152012, 09:45 PM #2
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.
 08162012, 03:06 AM #3
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.

Re: Counting Divisors from 1 to 10 000
my calculations show:
Number 9990 and had 31 divisors
 08162012, 06:23 PM #5Senior Member
 Join Date
 Oct 2011
 Location
 Sweden
 Posts
 124
 Rep Power
 0

 08172012, 01:57 AM #7
Re: Counting Divisors from 1 to 10 000
I'd suggest the OP redo his algorithm and test it with a smaller number like 10 or 20 until it generates the right results.
If you don't understand my response, don't ignore it, ask a question.

 08172012, 03:59 PM #9Member
 Join Date
 Oct 2011
 Location
 Tromsų
 Posts
 54
 Rep Power
 0
Re: Counting Divisors from 1 to 10 000
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
Java Code:if(bufferDivisors >= divisor ){
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
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
 08172012, 04:54 PM #10
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.
 08202012, 04:23 PM #11Senior Member
 Join Date
 Oct 2011
 Location
 Sweden
 Posts
 124
 Rep Power
 0
Re: Counting Divisors from 1 to 10 000
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 = p_{1}^{a1} * p_{2}^{a2} * p_{3}^{a3} * p_{4}^{a4} * p_{k}^{ak}
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! =)

Re: Counting Divisors from 1 to 10 000
@Zyril. thanks, works great...
 08202012, 10:25 PM #13Senior Member
 Join Date
 Oct 2011
 Location
 Sweden
 Posts
 124
 Rep Power
 0
Similar Threads

Counting Words
By Shyamz1 in forum New To JavaReplies: 9Last Post: 03032011, 01:12 PM 
Counting words
By Wasley in forum New To JavaReplies: 9Last Post: 01302011, 10:57 PM 
problems with counting
By worsethenu in forum New To JavaReplies: 4Last Post: 12062010, 02:33 PM 
Counting help
By jksmithson in forum New To JavaReplies: 1Last Post: 11062009, 02:43 AM 
Counting characters
By Tiff89 in forum New To JavaReplies: 10Last Post: 12122008, 09:21 AM
Bookmarks