Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Example 1 Example 2 5 18 6 3 9 1 18 3 4 5 6 Programming language:python You are not allowed to use any Python

Example 1

Example 2

5

18

6

3

9

1

18

3

4

5

6

Programming language:python

You are not allowed to use any Python built-in methods or user-defined modules in your programs.

A lazy student is attending a very basic math class! And sometimes laziness enables people to find the quickest ways to get things done! Our student is given an arbitrary number of fractions, and the task is to find out the total sum of the fractions. And students in the class are allowed to get help from others to solve the problem. As you know, to add fractions with each other, one has to find the least common multiple of the denominators. The lazy student is not in the mood of calculating the least common multiple by hand! Your help is greatly needed by the lazy student to write a program, called gE^2kylcm, to calculate the least common multiple of a bunch of numbers! Lets explore the problem to know how to develop gE^2kylcm. The least common multiple (lcm) of two numbers can be calculated as:

lcm(a, b) = a.b/gcd(a, b)

where gcd(a, b) is the greatest common divisor. This means, in order to calculate lcm of two numbers, you need to calculate their greatest common divisor (gcd) first. In order to find out the greatest common divisor of two numbers, you have to use the fact that:

gcd(a, b) = gcd(b, r)

where r is the remainder of the division of a/b. Therefore, for example, in order to find out gcd(99, 36), by using this fact iteratively, you would have: gcd(99, 36) = gcd(36, 27) = gcd(27, 9) = gcd(9, 0). Hence, when the remainder gets zero, you would find out the gcd, which is 9 in this case. Now, that you learn about the algorithm of computing lcm it is time to develop gE^2kylcm:

gE^2kylcm first receives the number of denominators that you have to calculate their lcm. Lets call this number n which is always greater than 1.

Subsequently, gE^2kylcm receives the denominators in n separate lines. gE^2kylcm assumes that the user only enters positive integers (greater than 0) in this step.

Finally, gE^2kylcm will find out the least common multiple of all of the given numbers, and prints the result in a separate line.

Note that you should only work with integers -and not floats- to avoid overflowError. In computing least common multiple (lcm), make sure you use "//" operator -and not not "/" operator- for division tasks.

Two examples are shown in the above table. In example 1, the user asked the program to compute the lcm of 5 numbers (18,6,3,9,1) and the result is printed as 18. In example 2 the user asked the program to compute the lcm of 3 numbers (4,5,6) and the result is printed as 60. Note that your program input and output format should be exactly the same as the format of the examples shown in the above table.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions