-6

In "Great Mathematical problems -- Vision of infinity", page 18 Ian Stewart referred Euclid's proposition 2, Book VII of Element which is a very elementary method of finding Greatest common divisor. I quote "It works by repeatedly subtracting the smaller number from the larger one, then applying a similar process to the resulting remainder and the smaller number, and continuing until there is no remainder." The example is with 630 and 135. 135 is repeadedly "subtracted"from 630 (495, 360, 225) and finally obtain 90 which is less than 135. So the numbers are inverted and 90 is repeatedly subtracted from 135 to have finally, 45. Then 45 is subtracted from 90 and finally obtain 0 yielding 45 the gcd. This is sometimes called Euclidean Algorithm of finding gcd.

To teach a beginner (a child of 10) I need to write the code in python. There should not be any function definition, neither it should use any other mathematical operation than subtraction. I want to use if/while/else/elif/continue/break. There should be provision that if three numbers (or more) are given, the whole program must be repeated deciding the smaller one. Earlier chain on gcd does not look the algorithm from this perspective.

Biplab
  • 1
  • 3
  • 4
    I believe there are thousands of implementations of the (Extended) Euclidean Algorithm in Python around. Which ones did you check? What was wrong with them? – Andrea Corbellini Jul 24 '16 at 18:42
  • Well, `gcd` (as well as `sin`, `cos`, `max`, `min`, etc) _should_ probably be a function. In math you write `gcd(1,3,5)`, which is just a function call. – ForceBru Jul 24 '16 at 18:45
  • I mentioned my specific requirements of the algorithm -- whatever be the name of it. I stick to it because of pedagogical reasons. I found none which met it. Those available around concern "division" rather than "subtraction" as mentioned in the first paragraph. I will be glad if any reference can be made -- Andrea Corbelini – Biplab Jul 25 '16 at 07:52

1 Answers1

0

A typical fast solution to the gcd algorithm would be some iterative version like this one:

def gcd(x, y):
    while y != 0:
        (x, y) = (y, x % y)

    return x

In fact, in python you'd just use directly the gcd function provided by fractions module.

And if you wanted such function to deal with multiple values you'd just use reduce:

reduce(gcd,your_array)

Now, it seems you want to constrain yourself to use only loops+substractions so one possible solution to deal with x,y positive integers would be this:

def gcd_unopt(x, y):
    print x,y

    while x!=y:
        while x>y:
            x -= y
            print x,y
        while y>x:
            y -= x
            print x,y

    return x

print reduce(gcd_unopt,[630,135])

Not sure why you wanted to avoid using functions, gcd is a function by definition, even so, that's simple, just get rid of the function declaration and use the parameters as global variables, for instance:

x = 630
y = 135

print x,y

while x!=y:
    while x>y:
        x -= y
        print x,y
    while y>x:
        y -= x
        print x,y
BPL
  • 9,632
  • 9
  • 59
  • 117
  • Thanks. But, the program simply returns the values entered. Moreover, does this mean that this simple algorithm cannot be implemented in python without using "function definition" as was my initial intention? In C this can be done with a few lines with two nested while. – Biplab Jul 25 '16 at 17:01
  • @Biplab I've edited my comment. Not sure what you meant by "the program simply returns the values entered" though. In any case, the answer provides some prints to show the child how the unoptimized algorithm executes. – BPL Jul 25 '16 at 19:16
  • Thanks BPL. It serves my purpose, indeed it offers more. – Biplab Jul 28 '16 at 15:47
  • @Biplab Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers, and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation) – BPL Jul 28 '16 at 18:17