[SOLVED] Implementation of Euclidian algorithm

Get help using Construct 2

Post » Mon Oct 31, 2016 10:22 pm

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:
Image
Pretty simple, huh? One would think, that's easily implementable.

This ismy current setup:
Image
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?
Last edited by randomly on Mon Oct 31, 2016 11:59 pm, edited 1 time in total.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091

Post » Mon Oct 31, 2016 10:58 pm

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.
B
94
S
33
G
114
Posts: 5,359
Reputation: 73,779

Post » Mon Oct 31, 2016 11:58 pm

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) :
Code: Select all
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.

Image


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.
"We can't solve problems by using the same kind of thinking we used when we created them."
- Albert Einstein
B
27
S
11
G
8
Posts: 528
Reputation: 7,091


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 22 guests