GCD with 4

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

GCD with 4

Postby DopplerEffect » Sun Feb 17, 2013 8:46 pm

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

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

Re: GCD with 4

Postby casevh » Sun Feb 17, 2013 9:02 pm

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

thanks already


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: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: GCD with 4

Postby DopplerEffect » Sun Feb 17, 2013 9:07 pm

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

Postby casevh » Sun Feb 17, 2013 9:52 pm

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: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: GCD with 4

Postby DopplerEffect » Sun Feb 17, 2013 10:00 pm

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

Postby stranac » Sun Feb 17, 2013 10:03 pm

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.
Read it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1210
Joined: Thu Feb 07, 2013 3:42 pm

Re: GCD with 4

Postby ichabod801 » Sun Feb 17, 2013 10:05 pm

Please use code tags:

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.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: GCD with 4

Postby DopplerEffect » Sun Feb 17, 2013 10:20 pm

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

Postby DopplerEffect » Sun Feb 17, 2013 10:24 pm

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

Postby DopplerEffect » Sun Feb 17, 2013 11:06 pm

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

Postby casevh » Sun Feb 17, 2013 11:47 pm

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: 70
Joined: Sat Feb 09, 2013 7:35 am

Re: GCD with 4

Postby Somelauw » Fri Feb 22, 2013 11:23 pm

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: 73
Joined: Tue Feb 12, 2013 8:30 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests