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 ...
Create an object
NumberPersonalities in a file
NumberPersonalities object, declare a variable as
NumberPersonalitiesobject, write a
def main(args: Array[String])method to test the numbers
limit, inclusive, and print out the results in the form shown above. In
main, do not assume that
limitis 100, but write your program to handle any positive
Int. This makes it easy to change the limit later.
mainshould probably be the last method you write, after you have written and tested all the methods that it will need to call.
false). Within the
NumberPersonalitiesobject, write the following predicates:
isPrime(n: Int): Boolean
isHappy(n: Int): Boolean
isTriangular(n: Int): Boolean
isSquare(n: Int): Boolean
isSmug(n: Int): Boolean
isHonest(n: Int): Boolean
isPronic(n: Int): Boolean
isDeficient(n: Int): Boolean
isPerfect(n: Int): Boolean
isAbundant(n: Int): Boolean
And also write the following method:
sumOfPositiveDivisorsOf(n: Int): Int
nin each of the above). You can use whatever parameter names you like. For these methods, however,
nis as good a name as any.
Booleanin all but one of the above methods), and you don't have to specify it.
A positive number is prime if its only positive divisors are itself
and one. For example,
7 is prime because it is evenly divisible
7, but not by any number in between (in particular,
it is not evenly divisible by
A positive number is composite if it is not prime. For example,
10 is composite because it is evenly divisible by
12 is composite because it is evenly divisible by
6. As a special case,
1 is considered to be composite.
Programming note. You can test whether a number
nis prime by dividing it by each of the numbers
n-1, and testing if the remainder is zero (use the
%operator). A zero remainder means
nwas evenly divisible by that divisor, hence is 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
12 + 92 = 1 + 81 = 82
82 + 22 = 64 + 4 = 68
62 + 82 = 36 + 64 = 100
12 + 02 + 02 = 1 + 0 + 0 = 1and 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.
Programming note. You can get the last digit of a number
nwith the expression
. You can subsequently discard this digit by doing an "integer division" by 10; when you get a zero, there are no more digits.
(n % 10)
Example: Taking the number
1024 % 10 = 4(stop)
1024 / 10 = 102, 102 % 10 = 2
102 / 10 = 10, 10 % 10 = 0
10 / 10 = 1, 1 % 10 = 1
1 / 10 = 0
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
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 = 5and the number is triangular.
5 - 2 = 3
3 - 3 = 0
, for some odd positive
1 + 3 + 5 + 7 + ... + n
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).
A number is smug if it is the sum of two square numbers. The first few
smug numbers are
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.
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*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
ksuch that either
n/k = k
n/k < k
A number is pronic if it is the product of two consecutive numbers. The first few pronic numbers are
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.
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.
24 is evenly divisible by
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 (
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.
Turn in your
.scala file by 6am, Friday Sept. 13. As only one file is required, there is no need to zip it.