CSCI 241 Labs: Lab 6
Going Loopy
There are 5 checkpoints ,
including the
clean-up checkpoint, in this lab.
Work together with your partner using just one of your accounts.
CHANGE WHO IS CONTROLLING THE COMPUTER AFTER EACH CHECKPOINT!
If you need help with any exercise, raise your hand.
Copy the directory
/home/student/Classes/Cs241/Labs/Lab06
to your account.
Change directories into Lab06
and start BlueJ by entering
bluej&.
This lab is much like last
week's lab, in that it emphasizes using loops and ifs to solve
interesting problems. The only difference is that the problems this
week are probably a bit more difficult, but then you have an additional
week of learning under your belts, too.
Finding Prime Numbers
Recall from high school math
that a prime number is one that can only be (evenly) divided by 1 and itself.
Open the PrimeNumber class. This file contains listing 5.15
found on page 189 of your text.
This program finds and prints the first 50 prime
numbers. Examine the code and run the program to see its output.
It can be improved in a number of ways - and guess what you get to do now!
Your tasks for this checkpoint:
- Modify the code so that the primes print in 10 columns, each 6 characters wide.
- The program only finds the
first 50 primes. Modify the program so that it finds and prints all
prime numbers less than 5,000. It should then print the total number of
primes it found.
Hint:
The final prime before 5,000 is 4,999.
Note: By default, BlueJ's terminal window will limit the lines of output you see,
but you can change that setting by choosing Options-Unlimited Buffering.
1
Be prepared to answer the following questions:
- Why does the inner for loop
start at 2, rather than 1?
- Why does the inner for loop
stop at number/2
instead of number?
- Could we reduce the upper
bound on divisor even further? Explain.
Hint: The answer is
yes. The real question is why.
- Explain why we use break to
exit the inner for loop. Can you modify the code so you do not use a break?
Perfect Numbers
Since we are working with number sequences, let's try another one:
A number is perfect if the sum of all its positive divisors, excluding
itself, add to be the original number.
For example, 6 is perfect
because 1 + 2 + 3 = 6.
Similarly, 28 is perfect because 1 + 2 + 4 + 7 + 14 = 28. Perfect
numbers are really pretty rare. There are only 4 of them less than
10,000.
Open the Perfect class. It contains a for loop that counts from 1 to
10,000. All that's missing is the code to find all the divisors of i
and sum them. Add this code.
Hint:
Finding the divisors of i is similar to what you did in checking to see
if a number was prime. The biggest difference is that we aren't going
to stop when we find one divisor. We need to find them all and sum them.
Complete the Perfect number program so that it find all perfect numbers
less than 10,000.
2
Show us your revised code and demonstrate how it runs. Be ready to answer the
following questions:
- We declared int sum = 0;
inside the outer for loop. Would it work just as well to place this
line before the loop? Explain why or why not.
- There are only 49 known
perfect numbers. The last one was discovered in 2016 and is 44,677,235
digits
long. That isn't the number! That is how many digits it contains! Do
you think there are more?
Computing PI
You all know that Pi is a constant whose value is approximately
3.141592653589793238462643383279502884197169399375105820974944592307816406286...
but that its digits go on forever.
Did you ever wonder how Pi is calculated? There are numerous ways to do so.
For this exercise, you will use an algorithm that works and is simple to program (but others
exist that converge faster than this one).
Here is the mathematical formula:
1 1 1 1 1 1
Pi = 4 * (1 - --- + --- - --- + --- - ... - ------- + ----------)
3 5 7 9 2*i+1 2*(i+1)+1
Open the ComputePi class.
Your sum variable should keep the sum of the fractions
seen in the parentheses in the above formula.
Each time the loop runs, you should add ONE fraction to that
sum. (Note: the sign variable is there to help you
change whether you are adding or subtracting the fraction. Don't
forget to update it at the end of the loop body, if you choose
to use it.)
As you work, feel free to "tweak" the existing code if you wish.
Change the limit on the for loop so that it runs through the loop
1,000,000 times.
Before you run it again, comment out the printf() statement that is inside the loop
for obvious reasons.
3
Show us this program when you are ready.
- How many digits were
accurately calculated when the loop ran only 100 times?
- How many digits were
accurately calculated with the loop ran 1,000,000 times?
Print Pattern
Open the PrintPattern class. It contains a complete program to print the pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
KEEP THIS CODE!
Add additional loops, so that it follows this
pattern with
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Now, add yet another loop, so that the entire
pattern is
1
1
2
1
2 3
1
2 3 4
1
2 3 4 5
1
2 3 4 5 6
1
2 3 4 5 6
1
2 3 4 5
1
2 3 4
1
2 3
1
2
1
1
1
2
1
2 3
1
2 3 4
1
2 3 4 5
1
2 3 4 5 6
1
2 3 4 5 6
1
2 3 4 5
1
2 3 4
1
2 3
1
2
1
1
1
2
1
2 3
1
2 3 4
1
2 3 4 5
1
2 3 4 5 6
1
2 3 4 5 6
1
2 3 4 5
1
2 3 4
1
2 3
1
2
1
4
Show us your completed program.
After the Lab
Return to Firefox, log onto Canvas and print the next
programming assignment, #5, if you haven't already.
Be sure to exit Firefox before you log out and leave the lab.
5
Show us that you have logged out, turned off
your monitor, cleaned up, and pushed in your chairs for this last
checkpoint.