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 件のコメント

Niklas Kurz
Niklas Kurz 2021 年 1 月 24 日
noone comes up with a solution?
Jan
Jan 2021 年 1 月 24 日
The question is not clear yet. This makes it hard to find an answer.

サインインしてコメントする。

 採用された回答

Jan
Jan 2021 年 1 月 24 日

1 投票

N = Number;
G = Guess;
x = 1:N;
R = nan(1, N);
found = N;
for k = 1:N
R(k) = mod(G^x(k), N);
if any(R(1:k-1) == R(k))
found = k;
break
end
end
R = R(1:found)

その他の回答 (1 件)

Bob Thompson
Bob Thompson 2021 年 1 月 21 日

0 投票

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 件のコメント

Niklas Kurz
Niklas Kurz 2021 年 1 月 22 日
編集済み: Niklas Kurz 2021 年 1 月 22 日
How comes the value inside the sum can get greater then one? Looks to me like logical 0 and 1. Also I didn't build an actual loop before that. Assume N and G are constants, how a functional loop would be written then? By indexing, do you mean a for loop?
Bob Thompson
Bob Thompson 2021 年 1 月 22 日
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
Niklas Kurz 2021 年 1 月 23 日
I want the loop to stop if it found a repeated number because I need it to count the period in order that I can continue processing wih it.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

タグ

質問済み:

2021 年 1 月 21 日

回答済み:

Jan
2021 年 1 月 24 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by