How to stop iteration when some condition is met?

sanjyutu shared this question 2 months ago
Answered

I want to repeat calling function until I get some return value. How can I stop iteration or loop by some condition?


I am trying to visually show the Euclidean algorithm. The logic is to repeat Mod[large, small] until you get 0. So I wrote:


IterationList[Mod[large, small], large, small, {1071, 462}, 10]


We will get 0 as 5th element in this case. But I would like to let the user to change numbers and we might not get the answer in 10 loops. So how can I write a script to iterate until Mod[large, small] returns 0?


Best regards,

Best Answer
photo

Bonsoir,

j'ai fais ça un peu vite, et c'est pas très malin de demander l'itération d'une itération !

Sans la commande Itération[...] qui a trop tendance à renvoyer une indéfinition, ça marche mieux...

Comments (11)

photo
1

I think it's not possible to stop the IterationList().

But you can write:

RemoveUndefined(IterationList(Mod(large, small), large, small, {1071, 462}, 1000)) --> {1071, 462, 147, 21, 0}

(I think it's quick enough [about 15ms for the above constellation + sometime 15ms for Java-garbage-collection, independent what and how you do something])

photo
1

Thank you.


I was able to verify that I was not missing While command.

photo
photo
1

Hi,

...

photo
1

Cool trick! I think I can use this technique to get the list I need.


Thank you.

photo
1

Bonjour Patrick,

photo
1

Bonsoir,

j'ai fais ça un peu vite, et c'est pas très malin de demander l'itération d'une itération !

Sans la commande Itération[...] qui a trop tendance à renvoyer une indéfinition, ça marche mieux...

photo
1

Yes, now it's works (I think).

Note:

the count of recursions is limited: min about 100, max about 300 (depending on: I don't know, but I think depending on free memory).

Because scripts are not precompiled this recursion need more time than IterationList() (about factor 50), but this is the most time not significant (exception for example: realtime-simulation)

But it's possible to do more complex things with this recursion-methode.

Complex things in IterationList() is also possible but it's need to call a specific userTool in <expression>.

photo
2

to look the needed time see t0 and t1 and script in button start.

And by the way: for this specific case I think IterationList() with about 50 (fix) iterations is better (less code, more transparent and (not important) faster (less 1ms))

photo
1

Hi Patrick,


Thanks. New file works much better. Now I can just copy your code and use it.

photo
1

Hi rami,


Thank you for the clarification. It cleared some of my confusion.


Fixing the number of iterations to some large enough number would be better for almost of the cases. But you have to deal with the anxiousness that someday you might bump into a case which you don't get the answer in that fixed number of iterations. In real life it won't happen for 99.999...% of the cases.

photo
photo
1

I do not unsderstand why it fails for next calculus.

help me to understand

© 2019 International GeoGebra Institute