Question
Ruby Program Task: Write a program that asks the user to enter a number and then prints out whether or not that number is prime.
Ruby Program
Task: Write a program that asks the user to enter a number and then prints out
whether or not that number is prime. If it is not prime, the program must print out
each divisor of the number.
Discussion: A number is prime if it is divisible only by itself and 1. For example,
7 is a prime number because no other numbers besides 7 and 1 evenly divide 7. 6 is
not a prime number because its divisors are 1, 2, 3, and 6.
There are many ways to determine if a number, n, is prime or not. The simplest
way is to start with 1 and count up to n while keeping track of which numbers
evenly divide n:
isPrime?(n)
factors = empty array
for i = 2 to n-1
if i evenly divides n
add i to factors
if factors array is empty
return false
else
return factors
Prompt the user to enter a number, and then determine if the number is prime or not. If the number is not prime, then you are to print out the number along with its factors, otherwise print out an indication that the number is prime.
Must be contained in a Ruby method
Then, you must utilize that Ruby method in the main portion of your program by calling it upon an input number being entered
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started