loop: stop if value repeats
Show older comments
In my case this is based on
N = Number
G = Guess
x = 1:N
R= mod(G^x,N)
if R repeats I want the calculation mod(G^x,N) to stop and tell me the steps P that it needed to to get to R again
for example if R = 1, 4 3, 2, 1 -stop, P = 4
2 Comments
Niklas Kurz
on 24 Jan 2021
Jan
on 24 Jan 2021
The question is not clear yet. This makes it hard to find an answer.
Accepted Answer
More Answers (1)
Bob Thompson
on 21 Jan 2021
I'm assuming you have all four of these commands within some kind of loop. Without the loop I have not tested what I'm going to suggest adding.
To do what you're asking you need to record all the values of R, this can be done by indexing R. If you aren't running an integer for loop, starting at 1, then you'll need to add a counter for each time you loop.
Then you just need an if check to determine if the loop needs to be broken.
N = Number
G = Guess
x = 1:N
R(count) = mod(G^x,N)
if sum(R==R(count))>1
P = length(R)-1;
break
end
end
3 Comments
Niklas Kurz
on 22 Jan 2021
Edited: Niklas Kurz
on 22 Jan 2021
Bob Thompson
on 22 Jan 2021
Mmm, I see where you're getting the multiple values for R from now.
I'm not sure why you want to stop the 'loop' if you don't actually have one. Do you just want the first index of R which contains a duplicate?
Niklas Kurz
on 23 Jan 2021
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!