ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 06 Nov 2013 18:39:29 +0100primitive roothttps://ask.sagemath.org/question/10709/primitive-root/I am writing a program to find the primitive root.
In the lecture we have given that
x is a primitive root in F_p, where p a prime number, if
x^((p-1)/pi) is not 1. (With pi the prime factors of p-1).
So here my programm:
p = 468889
R = IntegerModRing(p)
factor(p-1)
#gives: p-1 = 2^3 * 3 * 7 * 2791
list = [2,3,7,2791]
c=True
(I changed the for loop:)
for x in range(1,p):
for pi in list:
a = (p-1)/pi
if a == int(a):
k = R(x^a)
if k==1:
c=False
else:
print k
else:
pass
if c==True:
print x
else:
print c, x
But still there is no result. Can anyone help? I don't see the problem.
Thanks a lot!Wed, 06 Nov 2013 17:41:16 +0100https://ask.sagemath.org/question/10709/primitive-root/Answer by 00Luca00 for <p>I am writing a program to find the primitive root.
In the lecture we have given that</p>
<pre><code>x is a primitive root in F_p, where p a prime number, if
x^((p-1)/pi) is not 1. (With pi the prime factors of p-1).
</code></pre>
<p>So here my programm:</p>
<pre><code>p = 468889
R = IntegerModRing(p)
factor(p-1)
#gives: p-1 = 2^3 * 3 * 7 * 2791
list = [2,3,7,2791]
c=True
(I changed the for loop:)
for x in range(1,p):
for pi in list:
a = (p-1)/pi
if a == int(a):
k = R(x^a)
if k==1:
c=False
else:
print k
else:
pass
if c==True:
print x
else:
print c, x
</code></pre>
<p>But still there is no result. Can anyone help? I don't see the problem.
Thanks a lot!</p>
https://ask.sagemath.org/question/10709/primitive-root/?answer=15670#post-id-15670I found my mistake. In the end there was no error, only, that the programm did not give any primitive root. That's because the
c=True
Must lie inside the first loop. Otherwise it was all the time False if it did not work out once. So the correct version is:
for x in range(1,p):
c=True
for pi in list:
a = (p-1)/pi
if a == int(a):
k = R(x^a)
if k==1:
c=False
else:
print k
else:
pass
if c==True:
print 'LĂ¶sung:', x
break
else:
print c, xWed, 06 Nov 2013 18:39:29 +0100https://ask.sagemath.org/question/10709/primitive-root/?answer=15670#post-id-15670Answer by tmonteil for <p>I am writing a program to find the primitive root.
In the lecture we have given that</p>
<pre><code>x is a primitive root in F_p, where p a prime number, if
x^((p-1)/pi) is not 1. (With pi the prime factors of p-1).
</code></pre>
<p>So here my programm:</p>
<pre><code>p = 468889
R = IntegerModRing(p)
factor(p-1)
#gives: p-1 = 2^3 * 3 * 7 * 2791
list = [2,3,7,2791]
c=True
(I changed the for loop:)
for x in range(1,p):
for pi in list:
a = (p-1)/pi
if a == int(a):
k = R(x^a)
if k==1:
c=False
else:
print k
else:
pass
if c==True:
print x
else:
print c, x
</code></pre>
<p>But still there is no result. Can anyone help? I don't see the problem.
Thanks a lot!</p>
https://ask.sagemath.org/question/10709/primitive-root/?answer=15669#post-id-15669Can you tell us which error you got (I did not get one for a long time, then i stopped the loop) ?
Moreover, in the first pass through the loop (x = 1, p = 2), `c` get the value `False`, and it will never become `True` again, because it is not reset in the loop. So, your code will not print anything !
Also, instead of writing:
if c==True:
you can simply write:
if c:
because `c` is a boolean, which has value `True` or `False`.
Also, you can remove:
else:
pass
Wed, 06 Nov 2013 18:18:03 +0100https://ask.sagemath.org/question/10709/primitive-root/?answer=15669#post-id-15669