Square form factorization: Difference between revisions

trimmed FreeBasic
(→‎{{header|Raku}}: Additional solution)
(trimmed FreeBasic)
Line 205:
 
//reduce indefinite form
#define rho(a, b, c) { \
t = ac; ac = ca; ca = t; t = b; \
q = (rN + b) / a; \
b = q * a - b; \
c += q * (t - b); }
 
//initialize
#define rhoin(a, b, c) { \
rho(a, b, c) h = b; \
c = (mN - h * h) / a; }
 
#define gcd(a, b) while (b) { \
t = a % b; a = b; b = t; }
 
Line 234:
 
while (m[k]) {
if (k && (N % m[k])==0) return m[k];
//check overflow m * N
if (N > MxN / m[k]) break;
Line 386:
declare sub rho ()
'reduce indefinite form
declare function issqrissq (byref r as ulong) as integer
'return -1 if c is square, set r:= sqrt(c)
declare sub qform (byref g as string, byval t as integer)
Line 412:
'signal to end all threads
 
dim shared as integerubyte q64q1024(631023), q63q3465(62), q55(543464)
'quadratic residue tables
 
Line 433:
#endmacro
 
function bqf.issqrissq (byref r as ulong) as integer
if q63q1024(c modand 631023) andalso q55q3465(c mod 553465) then
issqr = 0
'>98.6% non-squares filtered
if q64(c and 63) then
r = culng(sqr(c))
if q63(c mod 63) andalso q55(c mod 55) then
if r * r = c then return -1
'>98% non-squares filtered
r = culng(sqr(c))
if r * r = c then return -1
end if
end if
issqrissq = 0
end function
 
Line 552 ⟶ 550:
 
P.rho
if (i and 1) = 0 andalso P.issq(r) then
'square form found
 
if PQ.issqrpro(P, r) then
'square form found
 
if QP.proqform("P", ri) then
end if 'inverse square formroot
A.b =-P.b: A.c = r
A.qformrhoin("A",): j) = 1
A.qform("A", j)
 
P.qform("P", i)do
'inversesearch squareambiguous rootcycle
A.bt =-P.b: A.c = rb
rhoin(A).rho: j += 1
A.qform("A", j)
 
doif A.b = t then
'searchsymmetry ambiguous cyclepoint
t = A.bqform("A", j)
red(f, A.rho: j += 1a)
if f = 1 then exit do
 
if A.bflag = t then-1
'symmetryfactor pointfound
end A.qform("A", j)if
loop until red(f, A.a)flag
if f = 1 then exit do
 
end if ' proper flag = -1square
end if ' evensquare indicesform
'factor found
end if
loop until flag
 
end if ' proper square
end if ' square form
end if ' even indices
 
if flag then exit for
Line 601 ⟶ 596:
data 7091569
data 13290059
data 23515517
data 42854447
data 223553581
Line 635 ⟶ 631:
 
'tabulate quadratic residues
for t = 0 to 311540
s = t * t
q64q1024(s and 631023) =-1
q63q3465(s mod 633465) =-1
q55(s mod 55) =-1
next t
 
Line 663 ⟶ 658:
flag = 0
 
for t = 1 to tx + 1 step 2
h(if t) =< threadcreate(@squfof,tx @a(t))then
h(t) = threadcreate(@squfof, @a(t))
next t
end if
 
squfof(@a(0t - 1))
f = a(0t - 1).f
 
if ft =< 1tx then f = a(t).f
squfof(@a(0))
threadwait(h(t))
if f = 1 then f = a(t).f
end if
 
if f > 1 then exit for
f = a(0).f
for t = 1 to tx
threadwait(h(t))
if f = 1 then f = a(t).f
next t
 
Line 716 ⟶ 715:
N = 13290059
f = 3119 N/f = 4261
 
N = 23515517
f = 53 N/f = 443689
 
N = 42854447
Line 774 ⟶ 776:
f = 343242169 N/f = 13435662733
 
total time: 0.02279050170462 s
</pre>