Assignment
2: Number Personalities Fall 2006, David Matuszek |
For each of the numbers 1 through 100, determine some characteristics of those numbers. Print out the numbers, one per line, along with a list of its characteristics (on the same line).
Your output should look approximately like this:
1 composite, happy, triangular, square, not smug, honest 2 prime, unhappy, not triangular, not square, smug, honest 3 prime, unhappy, triangular, not square, not smug, honest 4 composite, unhappy, not triangular, square, not smug, honest 5 prime, unhappy, not triangular, not square, smug, dishonest ...
Declare a variable as
and write your program to test the numbers 1
through limit
,
inclusive. In the rest of the program, do not assume that limit
is 100, but write your program to handle any positive number (at least, any that can be represented as an int
). This makes it easy to change the limit later.
Also test your program with two or three large numbers (say, five or six digits), to make sure that your program can handle them.
This is a good program in which to use boolean methods (methods that return
a value of true
or false
). While you need to learn
the syntax of methods eventually, for now you should probably let Eclipse help
you. Simply write a statement that uses the boolean method properly,
such as
if(isPrime(i)) System.out.println(i + " is prime.");
Since the method isPrime
does not exist yet, Eclipse will give
you a red X in the left margin. Right-click on it, and Eclipse will offer to
create the method for you. Let it. Since Eclipse doesn't know what you want
the method to actually do, it creates a method that just returns false
.;
it's up to you to change the method to return the correct value.
I've written this assignment so that it's easy for you and your partner to
divide up the work between you. However, you still have to colloborate on writing
the main
method, which should call the various other methods and
print their results. Also, it's your responsibility to read your partner's
code and make sure it's correct. Remember, you both get the same grade on this
assignment.
A positive number is prime if its only positive divisors are itself
and one. For example, 7
is prime because it is evenly divisible
by 1
and 7
, but not by any number in between (in particular,
it is not evenly divisible by 2
, 3
, 4
,
5
, or 6
).
A positive number is composite if it is not prime. For example,
10
is composite because it is evenly divisible by 2
and
5
; 12
is composite because it is evenly divisible by
2
, 3
, 4
, and 6
. As a special case,
1
is considered to be composite.
Repeatedly apply the following procedure to a number:
If by doing this you eventually get to 1
, then the number is happy.
For example, if you start with 19
:
1^{2} + 9^{2} = 1 + 81 = 82
8^{2} + 2^{2} = 64 + 4 = 68
6^{2} + 8^{2} = 36 + 64 = 100
1^{2} + 0^{2} + 0^{2} = 1 + 0 + 0 = 1
and the number is happy.If instead you get into an infinite loop, the number is unhappy. So if your program runs forever, the number is unhappy. This isn't a very useful test, however. Fortunately, every unhappy number will eventually get into this cycle:
4, 16, 37, 58, 89, 145, 42, 20, 4, ...
so if you get any one of these numbers, say 4
, then you can stop
and conclude that the number is unhappy.
A triangular number is a number of the form
,
for some positive n
. These numbers are called triangular because,
if you have that many objects, you can arrange them in an equilateral triangle
(see figure).
The first few triangular numbers are 1, 3, 6, 10, 15
....
1 + 3 + 5 + 7 + ... + n
,
for some odd positive n
.
(The figure should help you understand why this definition is the same as the usual definition of square numbers.)
A number is smug if it is the sum of two square numbers. The first few
smug numbers are 2
(1+1), 5
(1+4), 8
(4+4), 10
(1+9), 13
(4+9), etc.
A number is dishonest if it "pretends" to be a square number,
but isn't one. Specifically, n is dishonest if there is a number k
such
that
(using integer division), but
k*k
is not n
. A number is honest if it is not dishonest.
Turn in your .java
file by midnight, Thursday Sept. 21. As only one file is required, there is no need to zip it.