• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

euclidean algorithm

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello! Can anyone help me to transform this code from python to java?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Fori Mix
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic