Message Boards Message Boards

0
|
3043 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Finding the Order of A Number Modulo 17

Posted 9 years ago

I'm trying to write a code to find a number in the field of integers Mod 17 which has order 16. This is my first time using Mathematica and I feel really lost in the syntax (my file is attached).

My approach is to start with the number 2 and just see if 2^2 (mod 17) is equivalent to 1. If not, I increase the power by one and check it again. I keep doing this until 2^p (mod 17) is equivalent to 1, and then I check to see if p == 16. If not, I just start the whole process over again with 3, and I do this until I find a number which is equivalent to 1 for the first time when taken to the power 16. Where am I going wrong with my code?

Edit: I've uploaded a more current file but I'm still not sure what is wrong.

Attachments:
POSTED BY: Kevin Nickerson
3 Replies

Yes, k needs to be incremented whether we print something or not, also p needs to go back to 2 for each new k.

k = 2;
While[k < 16, p = 2; While[Mod[k^p, 17] =!= 1, p++];
 If[p == 16, Print[k]]; k++]

A simpler way might be

In[2]:= Select[Range[16], FreeQ[Mod[#^Range[15], 17], 1] &]

Out[2]= {3, 5, 6, 7, 10, 11, 12, 14}

or just use the built-in function

In[3]:= PrimitiveRootList[17]

Out[3]= {3, 5, 6, 7, 10, 11, 12, 14}
POSTED BY: Ilian Gachevski

Where am I going wrong with my code?

You've got lots of basic syntax error. May be you should start with small example first in order to learn the basic syntax., You need ";" to separate statements in M. You can't write

k = 2;
p = 2
While[k < 17,
 p = 1
   While[GF[17][{k}]^p =!= 1,
    p ++
    ]
   If[p == 16, 
    Print [k]
     Break[]
    ];
 k++
 ]

notice there is no ";" after the p=1 and no ";" after the ] there., You also need ";" after the Print.

It is important to know the difference between "," and ";" in Mathematica. I have not tried to run your code, just an observation. Also the GF[17][{k}]^p looks strange but I never used this function. Have you tried to test this on its own first to make sure you get the syntax correct?

POSTED BY: Nasser M. Abbasi

My new file has fixes to a lot of these syntax errors. The new code I have looks like:

k = 2;
p = 2;
While[k < 17,
 While[(GF[17][{k}])^p =!= GF[17][{1}], p ++];
 If[p == 16, Print [k], k++];
 ]

This seems to be stuck in an infinite loop because it started printing out a bunch of 3s (and I do know that 3 is the first number with order 16 mod 17). I figure this is because the (k++) is in the "else" part of the If statement, but I'm not sure where to put this (k++) in the code so that it will end appropriately.

POSTED BY: Kevin Nickerson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract