0% found this document useful (0 votes)
5 views1 page

Javanotes5 99

The document explains how to use a for loop in Java to count the divisors of a positive integer N. It outlines an algorithm that tests each number from 1 to N to determine if it is a divisor and counts the total number of divisors found. Additionally, it introduces an improved version of the program that tracks the number of tested divisors and outputs a dot for every million tested to indicate progress.

Uploaded by

dsstudent05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Javanotes5 99

The document explains how to use a for loop in Java to count the divisors of a positive integer N. It outlines an algorithm that tests each number from 1 to N to determine if it is a divisor and counts the total number of divisors found. Additionally, it introduces an improved version of the program that tracks the number of tested divisors and outputs a dot for every million tested to indicate progress.

Uploaded by

dsstudent05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

3.4.

THE FOR STATEMENT 83

3.4.2 Example: Counting Divisors


Let’s look at a less trivial problem that can be solved with a for loop. If N and D are positive
integers, we say that D is a divisor of N if the remainder when D is divided into N is zero.
(Equivalently, we could say that N is an even multiple of D.) In terms of Java programming, D
is a divisor of N if N % D is zero.
Let’s write a program that inputs a positive integer, N, from the user and computes how
many different divisors N has. The numbers that could possibly be divisors of N are 1, 2, . . . , N.
To compute the number of divisors of N, we can just test each possible divisor of N and count
the ones that actually do divide N evenly. In pseudocode, the algorithm takes the form
Get a positive integer, N, from the user
Let divisorCount = 0
for each number, testDivisor, in the range from 1 to N:
if testDivisor is a divisor of N:
Count it by adding 1 to divisorCount
Output the count
This algorithm displays a common programming pattern that is used when some, but not all,
of a sequence of items are to be processed. The general pattern is
for each item in the sequence:
if the item passes the test:
process it
The for loop in our divisor-counting algorithm can be translated into Java code as
for (testDivisor = 1; testDivisor <= N; testDivisor++) {
if ( N % testDivisor == 0 )
divisorCount++;
}
On a modern computer, this loop can be executed very quickly. It is not impossible to run
it even for the largest legal int value, 2147483647. (If you wanted to run it for even larger
values, you could use variables of type long rather than int.) However, it does take a noticeable
amount of time for very large numbers. So when I implemented this algorithm, I decided to
output a dot every time the computer has tested one million possible divisors. In the improved
version of the program, there are two types of counting going on. We have to count the number
of divisors and we also have to count the number of possible divisors that have been tested.
So the program needs two counters. When the second counter reaches 1000000, the program
outputs a ’.’ and resets the counter to zero so that we can start counting the next group of one
million. Reverting to pseudocode, the algorithm now looks like
Get a positive integer, N, from the user
Let divisorCount = 0 // Number of divisors found.
Let numberTested = 0 // Number of possible divisors tested
// since the last period was output.
for each number, testDivisor, in the range from 1 to N:
if testDivisor is a divisor of N:
Count it by adding 1 to divisorCount
Add 1 to numberTested
if numberTested is 1000000:
print out a ’.’
Let numberTested = 0
Output the count

You might also like