### CS170, Homework 6

• Overview

• This homework will reinforce the concept of nested loops

You will write 2 nested loops in this homework assignment.

• Preparation

• Make a hw6 directory and copy over the prepared files:

 ``` mkdir ~/cs170/hw6 cp ~cs170002/share/hw6/*.java ~/cs170/hw6 cd ~/cs170/hw6 ```

• You can use BlueJ or gedit

If you use BlueJ from your dorm/apartment, it can be very slow...

If you use gedit, make sure you execute:

 ``` cd ~/cs170/hw6 ```

before running gedit.

You will need to use javac ... to compile and java ... to run the programs.

• Part 1: find all perfect numbers between 2 and 10,000

• In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself

Examples:

 The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. The next perfect number is 28, because the divisors of 28 are: 1, 2, 4, 7 and 14 (not including itself) and 1 + 2 + 4 + 7 + 14 = 28.

• Algorithm: to find all perfect numbers between 2 and 10,000

 ``` for ( n = 2; n <= 10000; n++ ) if ( n is a perfect number ) print n; ```

To determine if a number n is perfect, use a loop to compute the sum of its divisors and check if the sum is equal to n

• Assignment:

 Write the program Perfect.java that print the list of perfect numbers between 2 and 10,000.

• Where to put the algorithm:

 Use the file Perfect.java to write this algorithm.

You should know by now how to compile and run Java programs....

• Part 2: break a string up into maximal "non-decreasing" subsequences

• Let's define the following terminology:

 2 consecutive characters "xy" are in "non-decreasing" order if the character x is equal to the character y or x preceeds y alphabetically.

• Let's call an "non-decreasing" subsequence as a series of characters where all the characters are in "non-decreasing" ordering

Examples:

 ``` "aabcd" ( 'a' ≤ 'a' ≤ 'b' ≤ 'c' ≤ 'd' ) "bbbbtxyz" "abcdefghij" ```

The following are not "non-decreasing" subsequences:

 ``` "acb" ( 'c' > 'b' !!! ) "bhkxij" ( 'x' > 'i' !!! ) ```

• A string (any string) can be divided into a number of maximum (= longest possible) "non-decreasing" subsequences.

For example:

 ``` String: "abdhktcegkxklmwaabhxbbbc" ------^^^^^----^^^^^---- The first (longest possible) "increasing" subsequence = "abdhkt" The next (longest possible) "increasing" subsequence = "cegkx" The next (longest possible) "increasing" subsequence = "klmw" The next (longest possible) "increasing" subsequence = "aabhx" The final (longest possible) "increasing" subsequence = "bbbc" ```

• Assignment:

 Write the program MaxSubseq.java that reads in an input string and print the list of maximum (= longest possible) subsequences in the input string.

Hints:

 Find one maximum subsequence at a time ! Start at position 0 in the input string to find the first maximum subsequence The end of a maximum subsequence can be found if you find a character that is "out of order". Study the algorithm in this webpage: That algorithm breaks a line into words. The algorithm thsi you needs to write will also need to use 2 indices just like that algorithm.

• Where to put the algorithm:

 Use the file MaxSubseq.java to write this algorithm.

You should know by now how to compile and run Java programs....

• Turnin

• Use the following commands to turn in your homework:

 ``` cd ~/cs170/hw6 /home/cs170002/turnin-hw Perfect.java hw6 /home/cs170002/turnin-hw MaxSubseq.java hw6a ```