ExerciseAndrew / Algorithms

2 stars 2 forks source link

Euclidean GCD #1

Open csp256 opened 4 years ago

csp256 commented 4 years ago

Assignment to inp_{a,b} in function is unnecessary.

Should this function return a value or print it?

Can you write it in an iterative, non-recursive form?

ExerciseAndrew commented 4 years ago

non-recursive... def nonr_gcd(a,b):

if a<b:
    a+=b
    b=a-b
    a-=b
if (b==0):
    return a
while (a % b != 0):
    a+=b
    b=a-b
    a-=b
    b%=a
return b

thing = nonr_gcd(20, 462) print(thing)

ExerciseAndrew commented 4 years ago

def it_gcd(a, b): while b != 0: j = b b = a % b a = j return a

first = input("First number:\n")

second = input("Second number:\n")

print("GCD is [" + it_gcd(first, second) + "].\n")

print(it_gcd(45, 697080))

I couldn't make the input output work and now I am starting to think that it had to do with header files.