Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java In this assignment you will write a Java program that prompts the user for two positive integers, then prints out the greatest common divisor

Java

In this assignment you will write a Java program that prompts the user for two positive integers, then prints out the greatest common divisor of the two numbers. Your program will check its input, and will repeatedly prompt the user until appropriate values are entered. The main program control construct used in this program will be the loop (while, do-while, or for).

The greatest common divisor (GCD) of two positive integers is, as the name implies, the largest integer that evenly divides both numbers. For instance the GCD of 531 and 300 is 3. One way to see this is to list all divisors of both numbers, determine the set of common divisors, then determine the largest element in that set.

Divisors of 531: {1, 3, 9, 59, 177, 531}

Divisors of 300: {1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300}

Common Divisors of 531 and 300: {1, 3} It follows that the GCD of 531 and 300 is 3.

Another way to find the GCD is to simply compare the prime factorizations of the two numbers. In this case 531 3359 and 300 22355 , so clearly 3 divides both numbers, while no number larger than 3 does. The problem with these methods is that for large integers, it is a very time consuming and computationally intensive task to determine the prime factorization or the full set of divisors. Around 300 BC, Euclid of Alexandria discovered a very efficient algorithm for the determination of the GCD. This algorithm is known universally today as the Euclidean Algorithm. It uses the operation of integer division, which takes two positive integers as input, and produces two integers as output.

Recall that if a and b are positive integers, then the quotient q and remainder r of a upon division by b are uniquely determined by the two conditions: a b q r and 0 r b . We call a the dividend, b the divisor, q the quotient and r the remainder. If r 0 we say that b divides evenly into a, or simply that b divides a. (Note that the word divisor is used differently here than in the preceding paragraph, in which it meant a number which divides evenly into another. The remainder need not be zero in general.) For instance, dividing 531 by 300 yields a quotient of 1 and remainder of 231, since 531 3001 231.

We now illustrate the Euclidean Algorithm on the two numbers 531 and 300. Divide the larger number by the smaller, getting a quotient and remainder: 531 3001 231

Divide the preceding divisor by the preceding remainder: 300 2311 69

Divide the preceding divisor by the preceding remainder: 231 693 24

Divide the preceding divisor by the preceding remainder: 69 242 21

Divide the preceding divisor by the preceding remainder: 24 2113

Divide the preceding divisor by the preceding remainder: 21 37 0

The process halts when we reach a remainder of 0. The GCD is then the last non-zero remainder, which is in this case is 3. Observe that on each iteration, we discard the dividend and quotient, and save only the divisor and remainder for the next step, on which they become dividend and divisor respectively. Note also that the process must eventually terminate, since on each iteration the remainder is less than the divisor ( 0 r b ), hence the sequence of remainders is strictly decreasing and must reach zero.

We illustrate on another example: find the GCD of 675 and 524. We begin by dividing the larger by the smaller.

675 5241151

5241513 71

151 71 29

71 97 8

9 811

8 18 0

We stop when we reach a remainder of 0, and conclude that the GCD of 675 and 524 is 1. (Check this result by working out and comparing the prime factorizations of 675 and 524.) If youd like to see proof of the correctness of Euclids Algorithm, take Discrete Mathematics CMPE 16. In this project you are to write a Java program that implements the Euclidean Algorithm described above. It should be clear at this point that your program must use one of Javas iterative control structures (i.e. a while loop, do-while loop, or a for loop.) Recall that if a and b are int variables storing positive values, then the expression a % b evaluates to the integer remainder of a upon division by b. Recall also that the quotient is never used, so one need only declare int variables for the dividend, divisor, and remainder. On each iteration you should compute the remainder from the dividend and divisor, update the values of dividend and divisor, then go to the next iteration. The loop repetition condition should simply be that the remainder is greater than zero. In addition, your program will carry out a robust interaction with the user to check for correct input. In particular, if the user enters anything other a positive integer, your program will continue to prompt for more input. You must design appropriate loops to carry out these checks. Format your program output to coincide with the sample runs below.

% java GCD

Enter a positive integer: 531

Enter another positive integer: 300

The GCD of 531 and 300 is 3 % java GCD

Enter a positive integer: 300

Enter another positive integer: 531

The GCD of 300 and 531 is 3 % java GCD

Enter a positive integer: -57

Please enter a positive integer: a;lsdkjf

Please enter a positive integer: 531

Enter another positive integer: qopweiru

Please enter a positive integer: 123.456

Please enter a positive integer: z.x,mvn

Please enter a positive integer: 300 The GCD of 531 and 300 is 3

Observe that the user may enter the numbers in any order, i.e. smallerlarger, or largersmaller. Note also that the program prompts separately for the two inputs, and that non-positive integers, double values, and non-numeric strings are rejected until a positive integer is entered. It is recommended that you first write a program that does no input checking of any kind, and just computes the GCD of two positive 3 integers correctly. Only when this phase is complete, should you implement the input checks described above. See examples on the website illustrating use of the break and continue commands for hints on how to implement these checks.

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_2

Step: 3

blur-text-image_3

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

Accounting And Auditing Research And Databases Practitioner's Desk Reference

Authors: Thomas R. Weirich, Natalie Tatiana Churyk, Thomas C. Pearson

1st Edition

1118334426, 978-1118334423

More Books

Students also viewed these Databases questions

Question

1. Signs and symbols of the map Briefly by box ?

Answered: 1 week ago

Question

Types of physical Maps?

Answered: 1 week ago

Question

Explain Intermediate term financing in detail.

Answered: 1 week ago