## GCD with 4

This is the place for queries that don't fit in any of the other categories.

### GCD with 4

Hello everyone,

I've assigment that requries a function to be created to find gcd of four numbers, you may see below for details (Using the Euclid's algorithm) the function does not expect the negative integers and less than 4 numbers

any hints that you give may help

DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: GCD with 4

DopplerEffect wrote:Hello everyone,

I've assigment that requries a function to be created to find gcd of four numbers, you may see below for details (Using the Euclid's algorithm) the function does not expect the negative integers and less than 4 numbers

any hints that you give may help

You haven't provided any code or provided any error messages, so the best I can do is make a couple of generic hints.

1. The gcd(-4, 12) is the same as gcd(4,12). Negative integers should be converted to positive integers.

2. How are you handling different number of inputs? Can you write a function that returns a tuple (or list) of the arguments, even if the number of arguments varies?

casevh
casevh

Posts: 114
Joined: Sat Feb 09, 2013 7:35 am

### Re: GCD with 4

I can write this function for two variables, but the variables when get 4 thinks are getting complicated too
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: GCD with 4

If you can find the gcd() of two numbers, then you can find the gcd() of three numbers by finding the gcd() of the first two numbers, and then finding the gcd() of that result with the third number. Something like:

Code: Select all
`gcd(a,b,c) = gcd(gcd(a,b),c)`

If you want more help, you will need to show the code that you have written.
casevh

Posts: 114
Joined: Sat Feb 09, 2013 7:35 am

### Re: GCD with 4

Code: Select all
`def gcd(a, b):    while b != 0 :        a, b = b, a%b    print a`
Last edited by DopplerEffect on Sun Feb 17, 2013 10:25 pm, edited 1 time in total.
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: GCD with 4

DopplerEffect wrote:def gcd(a, b):
while b != 0 :
a, b = b, a%b
print a

If you enter ANY forum on the site, you'll see a topic titled New Users, Read This.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: GCD with 4

Code: Select all
`def gcd(a, b):   while b != 0 :      a, b = b, a%b   print a`

You should use a return statement (return a) instead of a print statement (print a). That will make any call to your gcd function get replaced with the return value. Then you can use the trick casevh is talking about in his last post.
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801

Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

### Re: GCD with 4

I can not write that trick thats the point, can you write it down ?
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: GCD with 4

By the way i am deeply sorry about my mistake stranac
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: GCD with 4

Code: Select all
` while b != 0 and d !=0 :        a, b = b, a%b       k=a              c, d = d, c%d       t=c              k, t = t, k%t              print k     `

gcd (5, 6, 6,3) when i run this i get 3 as result where is the mistake ?
DopplerEffect

Posts: 9
Joined: Sun Feb 17, 2013 8:40 pm

### Re: GCD with 4

DopplerEffect wrote:
Code: Select all
` while b != 0 and d !=0 :        a, b = b, a%b       k=a              c, d = d, c%d       t=c              k, t = t, k%t              print k     `

gcd (5, 6, 6,3) when i run this i get 3 as result where is the mistake ?

You need to use three "while ...:" loops; one for each of the pairs of integers (a,b), (c,d), and (k,t).
casevh

Posts: 114
Joined: Sat Feb 09, 2013 7:35 am

### Re: GCD with 4

Just write gcd for 2 numbers first.
Then do
Code: Select all
`reduce(gcd2, 4numbers)`

in which gcd2 is the gcd function for 2 numbers.

Edit: In python3, you need to import reduce first from functools.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw

Posts: 82
Joined: Tue Feb 12, 2013 8:30 pm