-
Notifications
You must be signed in to change notification settings - Fork 0
/
sidef-sqrtmod.pl
157 lines (116 loc) · 3.13 KB
/
sidef-sqrtmod.pl
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
#!/usr/bin/perl
# a(n) is the least number such that the n-th prime is the least coprime quadratic nonresidue modulo a(n).
# https://oeis.org/A306493
#b(p, k) = gcd(p, k)==1&&!issquare(Mod(p, k))
#a(n) = my(k=1); while(sum(i=1, n-1, b(prime(i), k))!=0 || !b(prime(n), k), k++); k
use 5.014;
use ntheory qw(:all);
use Math::Sidef;
sub a {
my ($n) = @_;
my $p = nth_prime($n);
my @primes = @{primes($p-1)};
for(my $k = 1;;++$k) {
if (!((gcd($p, $k) == 1) && (!Math::Sidef::sqrtmod($p, $k))) ||
(vecany {
(gcd($_, $k) == 1) && (!Math::Sidef::sqrtmod($_, $k))
} @primes)
) {
}
else {
return $k;
}
}
}
#~ sub b {
#~ my ($n) = @_;
#~ my $p = nth_prime($n);
#~ my @primes = @{primes($p-1)};
#~ for(my $k = 1;;++$k) {
#~ if (!((gcd($p, $k) == 1) && !(kronecker($k, $p) == 0)) ||
#~ (vecany {
#~ (gcd($_, $k) == 1) && !(kronecker($k, $_) == 0)
#~ } @primes)
#~ ) {
#~ }
#~ else {
#~ return $k;
#~ }
#~ }
#~ }
#~ say b(5);
#~ __END__
foreach my $n(1..100) {
say "a($n) = ", a($n);
}
__END__
a(17) = 6924458
a(18) = 13620602
175244281
a(1) = 3
a(2) = 4
a(3) = 6
a(4) = 22
a(5) = 118
a(6) = 479
a(7) = 262
a(8) = 3622
a(9) = 5878
a(10) = 18191
a(11) = 24022
a(12) = 132982
a(13) = 296278
a(14) = 366791
a(15) = 1289738
a(16) = 4539478
a(17) = 6924458
a(18) = 13620602
a(19) = 32290442
a(20) = 175244281
^C
perl v.pl 560.52s user 0.18s system 99% cpu 9:22.30 total
[3, 4, 6, 22, 118, 479, 262, 3622, 5878, 18191, 24022, 132982, 296278, 366791, 1289738, 4539478, 6924458, 13620602, 32290442, 175244281]
# Daniel "Trizen" Șuteu
# Date: 30 October 2017
# https://github.com/trizen
# Find all the solutions to the congruence equation:
# x^2 = a (mod n)
# Defined for any values of `a` and `n` for which `kronecker(a, n) = 1`.
# When `kronecker(a, n) != 1`, for example:
#
# a = 472
# n = 972
#
# which represents:
# x^2 = 472 (mod 972)
#
# this algorithm is not able to find a solution, although there exist four solutions:
# x = {38, 448, 524, 934}
# Code inspired from:
# https://github.com/Magtheridon96/Square-Root-Modulo-N
use ntheory qw(:all);
sub foo {
my ($n) = @_;
my $p = nth_prime($n);
foreach my $k(2..1e5) {
my @a = sqrt_mod($p, $k);
}
}
my @a = sqrt_mod_n(19**2, 118);
say "@a";
#say foo(5);
__END__
my $p = nth_prime(5);
my $k = 118;
foreach my $n(1..$k) {
my @a = sqrt_mod_n($n**2, $k);
@a || next;
say "a($n) = ", join(' ', map{sprintf("[%s, %s]", $_, gcd($_, $p))} @a);
}
__END__
say join(' ', sqrt_mod_n(993, 2048)); #=> 369 1679 655 1393
say join(' ', sqrt_mod_n(441, 920)); #=> 761 481 209 849 531 251 899 619 301 21 669 389 71 711 439 159
say join(' ', sqrt_mod_n(841, 905)); #=> 391 876 29 514
say join(' ', sqrt_mod_n(289, 992)); #=> 417 513 975 79 913 17 479 575
# The algorithm works for arbitrary large integers
say join(' ', sqrt_mod_n(13**18 * 5**7 - 1, 13**18 * 5**7)); #=> 633398078861605286438568 2308322911594648160422943 6477255756527023177780182 8152180589260066051764557