Cipolla's algorithm: Difference between revisions

m
m (Automated syntax highlighting fixup (second round - minor fixes))
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 2,076:
return (x1[0],-x1[0]%p)
 
print (f"Roots of 2 mod 7: " +str({cipolla(2, 7)}")
print (f"Roots of 8218 mod 10007: " +str({cipolla(8218, 10007)}")
print (f"Roots of 56 mod 101: " +str({cipolla(56, 101)}")
print (f"Roots of 1 mod 11: " +str({cipolla(1, 11)}")
print (f"Roots of 8219 mod 10007: " +str({cipolla(8219, 10007)}")
</syntaxhighlight>
{{out}}
 
{{out}}
<pre>Roots of 2 mod 7: (4, 3)
<pre>
<pre>Roots of 2 mod 7: (4, 3)
Roots of 8218 mod 10007: (9872, 135)
Roots of 56 mod 101: (37, 64)
Line 2,090 ⟶ 2,091:
Roots of 8219 mod 10007: ()
</pre>
 
=={{header|Racket}}==
{{trans|EchoLisp}}
Line 2,442 ⟶ 2,444:
Roots of 881398088036 are (791399408049 208600591990) mod 1000000000039
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151</pre>
 
Simpler implementation:
 
<syntaxhighlight lang="ruby">func cipolla(n, p) {
 
kronecker(n, p) == 1 || return nil
 
var (a, ω) = (
0..Inf -> lazy.map {|a|
[a, submod(a*a, n, p)]
}.first_by {|t|
kronecker(t[1], p) == -1
}...
)
 
var r = lift(Mod(Quadratic(a, 1, ω), p)**((p+1)>>1))
 
r.b == 0 ? r.a : nil
}</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
Line 2,529 ⟶ 2,551:
{{libheader|Wren-big}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./dynamic" for Tuple
 
var Point = Tuple.create("Point", ["x", "y"])
Line 2,600 ⟶ 2,622:
[82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true]
</pre>
 
=={{header|zkl}}==
{{trans|EchoLisp}}
9,486

edits