6

1

I would like to find smallest x value for each m value such that `Mod[3^x, m] == 1`

, where `m`

is not multiple of 3.

Here is my try, which is not optimal for large `m`

, say `m >= 10000`

. How can I write a condition in `Table`

, so that whenever the condition `Mod[3^x, m] == 1`

holds, the computation breaks and proceeds to the next `m`

value?

A different approach would also be welcome.

```
range = 100;
m = Complement[Range[2, range], Range[3, range, 3]];
sol = ParallelTable[Mod[3^x, m[[i]] ] , {i, Length @ m}, {x, range}];
pos = FirstPosition[#, 1] & /@ sol;
Transpose @ {m, Flatten @ pos}
```

**Edit** Here is two different and faster method I found. First time I am using `While`

and `Break`

in my code :)

```
range = 10;
m = Complement[Range[2, range], Range[3, range, 3]];
val = (x = 1; While[x <= range, If[Mod[3^x, #] == 1, Break[]]; x++];
x) & /@ m;
Transpose@{m, val}
```

{{2, 1}, {4, 2}, {5, 4}, {7, 6}, {8, 2}, {10, 4}}

```
range = 10;
m = Complement[Range[2, range], Range[3, range, 3]];
val = Last@
Reap@Do[If[Mod[3^x, #] == 1, Sow@x; Break[]], {x, range}] & /@
m // Flatten;
Transpose@{m, val}
```

{{2, 1}, {4, 2}, {5, 4}, {7, 6}, {8, 2}, {10, 4}}

**Edit 2** I realized one can use `Throw`

and `Catch`

functions.

```
range = 10;
m = Complement[Range[2, range], Range[3, range, 3]];
val = Catch@Table[If[Mod[3^x, #] == 1, Throw@x], {x, range}] & /@ m
Transpose@{m, val}
range = 10;
m = Complement[Range[2, range], Range[3, range, 3]];
val = Catch@Do[If[Mod[3^x, #] == 1, Throw@x], {x, range}] & /@ m
Transpose@{m, val}
```

5

Lookup the function

– Carl Woll – 2018-03-17T21:19:56.237`MultiplicativeOrder`

Wow! I was not aware that function. Thanks.

`{#, MultiplicativeOrder[3, #]} & /@ m`

– OkkesDulgerci – 2018-03-17T21:26:59.843I think that Euler's Theorem may be helpful for this, it can be used to find an x quickly, but I'm not sure about the finding the lowest such x – Alex Li – 2018-03-18T02:57:28.027