*
The moose likes Beginning Java and the fly likes euclidean algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "euclidean algorithm" Watch "euclidean algorithm" New topic
Author

euclidean algorithm

Fori Mix
Greenhorn

Joined: Dec 10, 2012
Posts: 2
Hello! Can anyone help me to transform this code from python to java?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Welcome to the Ranch.

Did you try writing this code in Java yourself? If yes, then please show us your Java code. Is there anything specific that you're having trouble with?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Fori Mix
Greenhorn

Joined: Dec 10, 2012
Posts: 2
Jesper de Jong wrote:Welcome to the Ranch.

Did you try writing this code in Java yourself? If yes, then please show us your Java code. Is there anything specific that you're having trouble with?



I am not really familiar with python. I understand that the line "g, y, x = egcd(b % a, a)" gives value to 3 variables (g,y,x) with one return command. I don't know how to do this in java and my biggest problem is the recursion. How can i give values to g, y and x in java?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

Java does not allow you to return more than one value from a method.

So, your first step is to understand what the code above is doing. Not in terms of Java, not in terms of Python...but in English. Try describing the algorithm in words a 10-year old child could understand.

ONLY when you can do that should you consider writing a single line of Java.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

Fori Mix wrote:I understand that the line "g, y, x = egcd(b % a, a)" gives value to 3 variables (g,y,x) with one return command. I don't know how to do this in java and my biggest problem is the recursion. How can i give values to g, y and x in java?


Actually, that isn't really what the Python code is doing - Python, like Java, only returns a single value. Unlike Java, Python will automatically 'unpack' certain data types (Tuples) when it detects commas on the left side of the assignment.

So what egcd(a,b) does is return a datatype called a Tuple. A Tuple is like an array - it is a fixed size sequence. The sequence is unpacked so that the variables g, y, and x get the zeroth, first, and second value in the returned Tuple.

The key point to recognize a Tuple is the presence of Commas, though they usually are surrounded by parenthesis as well. The code has this return value:

That is like saying this:

It is returning a Tuple of length 3 (a 3-tuple).


Steve
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: euclidean algorithm
 
Similar Threads
Is there a URL that explains[ y = x++ + ++x; ] ?
Colour picker, MouseDown(Event, int x, int y) Depreciation Help!
2D array: Find an array of characters
Passing objects by reference or value???
Thread DeadLock