Module 01 Project
In this PA you will write a few Ruby methods to practice Ruby syntax and explore the standard Ruby classes.
Project starter files:
factors(n) — must return an Array of all the factors of n, in order and
without duplicates. The result must be  if n < 1. For examples,
factors(12) == [1,2,3,4,6,12].
primes(n) — must return an Array of all prime numbers less than or equal to n,
in order and without duplicates. primes must return  if n < 2. For examples,
primes(10) == [2,3,5,7].
prime_factors(n) — must return an Array of all prime factors of n, in order and
prime_factors must return  if n < 2. For examples,
prime_factors(1) ==  and
prime_factors(12) == [2,3].
perfects(n) — must return an Array of all perfect numbers less than or equal
to n, in order and without duplicates. A perfect number is one whose factors sum
to twice the number. For example,
perfects(10) ==  (because the factors of 6
are 1, 2, 3, and 6, and these sum to 12).
pythagoreans(n) — must return an Array of Arrays of Pythagorean triples whose
elements are all less than or equal to n, in order without duplicates, and such
that each triple is ordered. A Pythagorean triple is three positive integers (x,
y, z) such that x2 + y2 = z2. For example,
pythagoreans(15) == [[3,4,5], [5,12,13], [6,8,10], [9,12,15]].
The Integer iterator
upto() is quite useful in solving these problems.
Alternatively, you can use Ranges. The Array
select method is also useful.
You may assume that all of the
n parameters are instances of the
class or one of its descendants.
Some test routines are included in
test.rb. However, your submission will be
graded based on additional tests not provided to you, so you should write your
own test cases as well. You may need to install the unit test package (
install test-unit) if your distribution of Ruby does not come pre-loaded with
For this assignment, you may NOT use any classes or methods that are not in the
Ruby Core (i.e., you shouldn’t
require anything). If you are unsure about a
particular class or method, you can consult the documentation. Also, we will
test your submission using Ruby 2.6 so you shouldn’t depend on any features
added after that version.
Your program must contain implementations of the five methods described above.
You must name your Ruby script file
p1.rb. You must put your name in a comment
at the top of the file. As usual, decompose your methods to make them more
readable, use good names, indent properly (indentation is conventionally two
spaces in Ruby), and so on. You will partly be graded on the readability of your
code but mostly on whether it works.
p1.rb to the appropriate assignment on Canvas by the deadline.