Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

4.16 Verify the conditional distribution for Xt|Xt in Example 4.9.

Answered: 1 week ago