0 Favourites

[SOLVED] Implementation of Euclidian algorithm

  • Hey everyone,

    I'm a bit ashamed.

    Tried to port the Euclidian algorithm (to calculate the greatest common divisor) to C2.

    I'm failing on the "While" condition since the program freezes as soon as I call the function.

    Apparently, I don't seem to really understand or use the "While" condition appropriately.

    This is the Euclidian algorithm according to Wikipedia:

    Pretty simple, huh? One would think, that's easily implementable.

    This ismy current setup:

    This causes a freeze for some reason.

    Notes: each time the function is called, b is supposed to stay the same (9) and a is variable.

    (And yes, I know that I could compress the code, but for overview's sake, I kept it like that)

    Any ideas how I should use the "While" condition correctly and why it causes a freeze in its current state?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • You have it correct, but looking at the algorithm I think a and b need to be both greater than zero.

    As it is with a=0 and b=9

    the "while" starts with 0 <> 9

    and since "a>b" isn't true with "0>9" then the else is run that subtracts 0 from 9

    And it repeats the while. Notice a and b are unchanged for the second iteration so you have an infinite loop.

  • Actually, a isn't 0 since it gets the value from the Function param.

    But I figured out what the issue was. Very specific one.

    In case you are interested what the problem was (if not, just skip) :

    That algorithm is part of a translation of the contents of 9 by 9 TextBoxes into an Array.
    I wanted to keep the optical order of the TextBox contents and translate them logically into the Array.
    
    In order to do so, I had to translate the IID's to X and Y coordinates in my Array.
    The thing that was causing the freeze was me forgetting to filter the TextBoxes which are empty (value 0), so the algorithm doesn't get applied to them. (And doesn't get freezed in the process)
    
    So what I added was a simple picking condition and that was it. Phew.[/code:10ryr9r8]
    [img="http://i.imgur.com/ZQAWElC.png"]
    
    Anyways, thank you very much for telling me that my implementation of the Algorithms was correct, so I didn't bother around with fixing that but could instead focus on the important stuff.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)