CIT 591 Assignment 2: Number Personalities
Fall 2013, David Matuszek

Purposes of this assignment

General idea of the assignment

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, not pronic, deficient
    2   prime, unhappy, not triangular, not square, smug, honest, pronic, deficient
    3   prime, unhappy, triangular, not square, not smug, honest, not pronic, deficient
    4   composite, unhappy, not triangular, square, not smug, honest, not pronic, deficient
    5   prime, unhappy, not triangular, not square, smug, dishonest, not pronic, deficient
    6   composite, unhappy, triangular, not square, not smug, dishonest, pronic, perfect



  1. Create an object NumberPersonalities in a file NumberPersonalities.scala.

  2. Within the NumberPersonalities object, declare a variable as val limit = 100.

  3. Within the NumberPersonalities object, write a def main(args: Array[String]) method to test the numbers 1 through limit, inclusive, and print out the results in the form shown above. In main, do not assume that limit is 100, but write your program to handle any positive Int. This makes it easy to change the limit later.
  4. A predicate is a method that returns a Boolean value (true or false). Within the NumberPersonalities object, write the following predicates:

    And also write the following method:

  1. We will be doing computer testing of your methods. This means that each of the required methods must have the correct name (correctly spelled and correctly capitalized), the correct number and types of parameters, and the correct return type.

Prime and composite numbers

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.

Programming note. You can test whether a number n is prime by dividing it by each of the numbers 2 through n-1, and testing if the remainder is zero (use the % operator). A zero remainder means n was evenly divisible by that divisor, hence is composite.

Happy and unhappy numbers

Repeatedly apply the following procedure to a number:

  1. Square each of the digits of the number.
  2. Add the squares together.

If by doing this you eventually get to 1, then the number is happy.

For example, if you start with 19:

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.

Programming note. You can get the last digit of a number n with the expression (n % 10). You can subsequently discard this digit by doing an "integer division" by 10; when you get a zero, there are no more digits.

Example: Taking the number 1024:

                1024 % 10 = 4
1024 / 10 = 102, 102 % 10 = 2
 102 / 10 = 10,   10 % 10 = 0
  10 / 10 = 1,     1 % 10 = 1
   1 / 10 = 0

Triangular numbers

stacks of ballsA triangular number is a number of the form 1 + 2 + 3 + ... + n, 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....

Programming note. You can test if a number is triangular by subtracting the number 1 from it, then subtracting 2, then subtracting 3, etc. If at some point you get zero, then the starting number is triangular; but if you first get a negative number, then the number is not triangular.

    6 - 1 = 5
    5 - 2 = 3
    3 - 3 = 0
and the number is triangular.

Square numbers

A square number Balls in a squareis a number of the form 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.)

Programming note. To test if a number is square, do the following. For each of the odd numbers 1 + 3 + 5 + 7 + ..., subtract that number, and see if you get zero (square) or a negative number (not square).

Smug 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.

Programming note. To test if a number is smug, do the following. For each of the square numbers (which you can get as 1*1, 2*2, 3*3, etc.), subtract that number, and see if the remainder is also a square number. You can stop if the remainder is less than the square number you subtracted.

Honest and dishonest numbers

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 n/k = k (using integer division), but k*k is not n. A number is honest if it is not dishonest.

Programming note. To find k, try the numbers 1, 2, 3, ... until you find a k such that either n/k = k or n/k < k.

Pronic numbers

A number is pronic if it is the product of two consecutive numbers. The first few pronic numbers are 2 (1*2), 6 (2*3), 12 (3*4), 20 (4*5), etc.

Programming note. Starting with k = 1, compute k * (k + 1) until the result either equals or exceeds the number you are testing.

Deficient, perfect, and abundant numbers

Every positive number is either deficient, perfect, or abundant. To determine which it is, add up all the positive numbers (including 1) that divide it evenly.

For example, 24 is evenly divisible by 1, 2, 3, 4, 6, 8, and 12. Because 1+2+3+4+6+8+12 = 36, and 36 > 24, the number 24 is abundant.

A fundamental rule of good programming is the DRY Principle: Don't Repeat Yourself! In each of three methods (isDeficient, isPerfect, and isAbundant), you need to compute the sum of the divisors of a number. It's poor style to write this same code in all three methods; instead, write a sumOfPositiveDivisorsOf method that each of the three methods can use.

Due date:

Turn in your .scala file by 6am, Friday Sept. 13. As only one file is required, there is no need to zip it.