/
hw3q3c.py
168 lines (152 loc) · 3.63 KB
/
hw3q3c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#! /usr/bin/env python
#
from timestamp import timestamp
def jacobi_symbol ( q, p ):
#*****************************************************************************80
#
## JACOBI_SYMBOL evaluates the Jacobi symbol (Q/P).
#
# Definition:
#
# If P is prime, then
#
# Jacobi Symbol (Q/P) = Legendre Symbol (Q/P)
#
# Else
#
# let P have the prime factorization
#
# P = Product ( 1 <= I <= N ) P(I)^E(I)
#
# Jacobi Symbol (Q/P) =
#
# Product ( 1 <= I <= N ) Legendre Symbol (Q/P(I))^E(I)
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 17 February 2015
#
# Author:
#
# John Burkardt
#
# Reference:
#
# Daniel Zwillinger,
# CRC Standard Mathematical Tables and Formulae,
# 30th Edition,
# CRC Press, 1996, pages 86-87.
#
# Parameters:
#
# Input, integer Q, an integer whose Jacobi symbol with
# respect to P is desired.
#
# Input, integer P, the number with respect to which the Jacobi
# symbol of Q is desired. P should be 2 or greater.
#
# Output, integer J, the Jacobi symbol (Q/P).
# Ordinarily, J will be -1, 0 or 1.
# -2, not enough factorization space.
# -3, an error during Legendre symbol calculation.
#
from i4_factor import i4_factor
from legendre_symbol import legendre_symbol
#
# P must be greater than 1.
#
if ( p <= 1 ):
print ( '' )
print ( 'JACOBI_SYMBOL - Fatal error!' )
print ( ' P must be greater than 1.' )
j = -2
return l
#
# Decompose P into factors of prime powers.
#
nfactor, factor, power, nleft = i4_factor ( p )
if ( nleft != 1 ):
print ( '' )
print ( 'JACOBI_SYMBOL - Fatal error!' )
print ( ' Not enough factorization space.' )
j = -2
return j
#
# Force Q to be nonnegative.
#
qq = q
while ( qq < 0 ):
qq = qq + p
#
# For each prime factor, compute the Legendre symbol, and
# multiply the Jacobi symbol by the appropriate factor.
#
j = 1
for i in range ( 0, nfactor ):
pp = factor[i]
l = legendre_symbol ( qq, pp )
if ( l < -1 ):
print ( '' )
print ( 'JACOBI_SYMBOL - Fatal error!' )
print ( ' Error during Legendre symbol calculation.' )
j = -3
j = j * l ** power[i]
return j
def jacobi_symbol_test ( ):
#*****************************************************************************80
#
## JACOBI_SYMBOL_TEST_TEST tests JACOBI_SYMBOL_TEST.
#
# Licensing:
#
# This code is distributed under the GNU LGPL license.
#
# Modified:
#
# 17 February 2015
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
ntest = 4
ptest = [ 3, 9, 10, 12 ]
print ( '' )
print ( 'JACOBI_SYMBOL_TEST' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' JACOBI_SYMBOL computes the Jacobi symbol' )
print ( ' (Q/P) which records whether Q is' )
print ( ' a quadratic residue modulo the number P.' )
for i in range ( 0, ntest ):
p = ptest[i]
print ( '' )
print ( ' Jacobi Symbols for P = %d' % ( p ) )
print ( '' )
for q in range ( 0, p + 1 ):
l = jacobi_symbol ( q, p )
print ( ' %6d %6d %6d' % ( p, q, l ) )
#
# Terminate.
#
print ( '' )
print ( 'JACOBI_SYMBOL_TEST' )
print ( ' Normal end of execution.' )
return
if ( __name__ == '__main__' ):
timestamp ( )
#print(jacobi_symbol(8,21))
a=int(input("a"))
n=int(input("n"))
#p=int(input("p"))
for j in range(a,n):
for i in range(2,n):
if jacobi_symbol(i*i,j)==1:
print(i)
break
timestamp ( )