Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given two positive integers, m and n, calculate their greatest common divisor (gcd). For example, gcd(2, 3)-1, ged(2, 10)-2, gcd(25, 35) 5, and ged(205, 305)

image text in transcribed
Given two positive integers, m and n, calculate their greatest common divisor (gcd). For example, gcd(2, 3)-1, ged(2, 10)-2, gcd(25, 35) 5, and ged(205, 305) 5. Write a complete Java program that calculates ged(m, n), for any positive integers m and n. A trivial approach is the so-called 'brute-force', that is, it starts from min(m, n) down to 1, to check if a number is common divisor for both m and n, if so, it is the greatest common divisor. A more efficient approach is the famous Euclid's algorithm. Here are some hints and snippets of Java code. /I r is the remainder of tl divided by t2 while (r !-0) t tl t2; r- t1 % t2; // When r is o, t2 is the greatest common divisor between t1 and t2 return t2; This algorithm can be extended for negative integers by considering the absolute value of m and n, that is, tl Math.abs (m) i t2 Math.abs (n) A more interesting approach that involves thinking recursively' is the recursive method, that is, gcd(m, n)n, gcd(m, n) = gcd(n, m % n), otherwise. frn % n = 0

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

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Discuss the history of human resource management (HRM).

Answered: 1 week ago