Quantcast

Maximum PC

It is currently Thu Apr 17, 2014 9:42 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Euclid's Algorithm (In Python)
PostPosted: Mon Jan 07, 2008 1:17 pm 
Little Foot
Little Foot

Joined: Fri Jan 04, 2008 2:57 pm
Posts: 121
This is the right section to post this right? If not then sorry but does anybody know how I would go about using Euclid's algorithm in python I mean how would I do that.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jan 07, 2008 3:10 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
A. This is the right section
B. There are several ways to code it. This wikipedia article seems like it would be a good place to start.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jan 08, 2008 11:32 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
We should also point out that we don't do homework here.

We answer specific questions and provide advice and trouble shooting .. that's it. Along with witty dialogue about programming concepts and trivia.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Jan 08, 2008 12:13 pm 
Little Foot
Little Foot

Joined: Fri Jan 04, 2008 2:57 pm
Posts: 121
Yea well I diddn,t mean for someone to tell me the code I just wanted some sources on how to and smartcat gave me a good one.


Thanks


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 8:24 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Ah! I misunderstood.

Here is another: www.google.com


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 3:09 am 
Little Foot
Little Foot

Joined: Fri Jan 04, 2008 2:57 pm
Posts: 121
I searched with it but couldn,t find anything realy helpful. So.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jan 18, 2008 11:54 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
Why don't you go ahead and post what you have so far and maybe we can you some suggestions on how to improve it.


Top
  Profile  
 
 Post subject: heres my version
PostPosted: Wed Feb 06, 2008 2:53 pm 
8086
8086

Joined: Thu Aug 09, 2007 2:30 pm
Posts: 53
Anyway, here's what I came up with:
Code:
#Euclid common factor
def cfactor(a,b):
  if a > b:
    while b != 0:
      achange = b
      bchange = a%b
      a = achange
      b = bchange
    return a
  else:
    return "a must be greater than b"

#Gets numbers
a = input("a = ")
b = input("b = ")

#Prints common factor
print "The commmon factor is:"
print cfactor(a,b)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Feb 06, 2008 4:28 pm 
Little Foot
Little Foot

Joined: Fri Jan 04, 2008 2:57 pm
Posts: 121
That seems to work unless it,s the wrong answer. I diddn,t come up with anything because I am still learning python :oops: just so you know I don,t start a topic then I stop watching it.


Top
  Profile  
 
 Post subject: Re: heres my version
PostPosted: Thu Feb 07, 2008 12:45 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
wiskitbiscuit wrote:
Anyway, here's what I came up with:

Python implementations are always nice. =)

I would recommend a couple small changes...
- rename the function gcd; gcd is the normal naming convention
- swap a and b if b > a
- use tmp instead of achange and bchange

Code:
#Euclid common factor
def gcd(a,b):
  if b > a:
    tmp = a
    a = b
    b = tmp
  while b != 0:
    tmp = b
    b = a%b
    a = tmp
  return a


Whose has an implementation in another language (besides Java, C#, C or C++).

And what about the least common multiple algorithm?


Top
  Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 6:26 pm 
8086
8086

Joined: Thu Aug 09, 2007 2:30 pm
Posts: 53
Oh. Now that I look at that, it makes quite a bit of sense. That's one of the reasons I love/hate programming - it's the simple stuff that gets you.


Top
  Profile  
 
 Post subject: Re: heres my version
PostPosted: Sat May 31, 2008 7:03 am 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
Gadget wrote:
wiskitbiscuit wrote:
Anyway, here's what I came up with:

Python implementations are always nice. =)

I would recommend a couple small changes...
- rename the function gcd; gcd is the normal naming convention
- swap a and b if b > a
- use tmp instead of achange and bchange


FYI, nice feature of python here -- one line swaps ;)

Code:
#Euclid common factor
def gcd(a,b):
  if b > a:
    (a, b) = (b, a)
  while b != 0:
    (a, b) = (b, a%b)
  return a


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group