Cipolla's algorithm: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 75: Line 75:
* [[Tonelli-Shanks algorithm]]
* [[Tonelli-Shanks algorithm]]
<br><br>
<br><br>

=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}


<syntaxhighlight lang=11l>F convertToBase(n, b)
<syntaxhighlight lang="11l">F convertToBase(n, b)
I (n < 2)
I (n < 2)
R [n]
R [n]
Line 135: Line 134:
Roots of 8219 mod 10007: []
Roots of 8219 mod 10007: []
</pre>
</pre>

=={{header|C}}==
=={{header|C}}==
{{trans|FreeBasic}}
{{trans|FreeBasic}}
<syntaxhighlight lang=c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdint.h>
#include <stdint.h>
#include <stdio.h>
#include <stdio.h>
Line 346: Line 344:
Find solution for n = 665165880 and p = 1000000007
Find solution for n = 665165880 and p = 1000000007
Solution found: x1 = 524868305, x2 = 475131702</pre>
Solution found: x1 = 524868305, x2 = 475131702</pre>

=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Numerics;
using System.Numerics;


Line 434: Line 431:
(791399408049, 208600591990, True)
(791399408049, 208600591990, True)
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, True)</pre>
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, True)</pre>

=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<syntaxhighlight lang=D>import std.bigint;
<syntaxhighlight lang="d">import std.bigint;
import std.stdio;
import std.stdio;
import std.typecons;
import std.typecons;
Line 530: Line 526:
Tuple!(BigInt, "x", BigInt, "y", bool, "b")(791399408049, 208600591990, true)
Tuple!(BigInt, "x", BigInt, "y", bool, "b")(791399408049, 208600591990, true)
Tuple!(BigInt, "x", BigInt, "y", bool, "b")(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true)</pre>
Tuple!(BigInt, "x", BigInt, "y", bool, "b")(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true)</pre>

=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(lib 'struct)
(lib 'struct)
(lib 'types)
(lib 'types)
Line 605: Line 600:


</pre>
</pre>

=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
===The function===
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
// Cipolla's algorithm. Nigel Galloway: June 16th., 2019
// Cipolla's algorithm. Nigel Galloway: June 16th., 2019
let Cipolla n g =
let Cipolla n g =
Line 623: Line 617:
</syntaxhighlight>
</syntaxhighlight>
===The Task===
===The Task===
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
let test=[(10I,13I);(56I,101I);(8218I,10007I);(8219I,10007I);(331575I,1000003I);(665165880I,1000000007I);(881398088036I,1000000000039I);(34035243914635549601583369544560650254325084643201I,10I**50+151I)]
let test=[(10I,13I);(56I,101I);(8218I,10007I);(8219I,10007I);(331575I,1000003I);(665165880I,1000000007I);(881398088036I,1000000000039I);(34035243914635549601583369544560650254325084643201I,10I**50+151I)]
test|>List.iter(fun(n,g)->match Cipolla n g with Some r->printfn "Cipolla %A %A -> %A (%A) check %A" n g r (g-r) ((r*r)%g) |_->printfn "Cipolla %A %A -> has no result" n g)
test|>List.iter(fun(n,g)->match Cipolla n g with Some r->printfn "Cipolla %A %A -> %A (%A) check %A" n g r (g-r) ((r*r)%g) |_->printfn "Cipolla %A %A -> has no result" n g)
Line 639: Line 633:
Real: 00:00:00.089, CPU: 00:00:00.090, GC gen0: 2, gen1: 0
Real: 00:00:00.089, CPU: 00:00:00.090, GC gen0: 2, gen1: 0
</pre>
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
{{trans|Sidef}}
{{trans|Sidef}}
{{works with|Factor|0.99 2020-08-14}}
{{works with|Factor|0.99 2020-08-14}}
<syntaxhighlight lang=factor>USING: accessors assocs interpolate io kernel literals locals
<syntaxhighlight lang="factor">USING: accessors assocs interpolate io kernel literals locals
math math.extras math.functions ;
math math.extras math.functions ;


Line 705: Line 698:
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151
</pre>
</pre>

=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
===LongInt version===
===LongInt version===
Had a close look at the EchoLisp code for step 2.
Had a close look at the EchoLisp code for step 2.
Used the FreeBASIC code from the Miller-Rabin task for prime testing.
Used the FreeBASIC code from the Miller-Rabin task for prime testing.
<syntaxhighlight lang=freebasic>' version 08-04-2017
<syntaxhighlight lang="freebasic">' version 08-04-2017
' compile with: fbc -s console
' compile with: fbc -s console
' maximum for p is 17 digits to be on the save side
' maximum for p is 17 digits to be on the save side
Line 920: Line 912:
===GMP version===
===GMP version===
{{libheader|GMP}}
{{libheader|GMP}}
<syntaxhighlight lang=freebasic>' version 12-04-2017
<syntaxhighlight lang="freebasic">' version 12-04-2017
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,092: Line 1,084:
Find solution for n = 34035243914635549601583369544560650254325084643201 and p = 10^50 + 151
Find solution for n = 34035243914635549601583369544560650254325084643201 and p = 10^50 + 151
Solution found: x1 = 17436881171909637738621006042549786426312886309400, x2 = 82563118828090362261378993957450213573687113690751</pre>
Solution found: x1 = 17436881171909637738621006042549786426312886309400, x2 = 82563118828090362261378993957450213573687113690751</pre>

=={{header|Go}}==
=={{header|Go}}==
===int===
===int===
Implementation following the pseudocode in the task description.
Implementation following the pseudocode in the task description.
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,169: Line 1,160:
===big.Int===
===big.Int===
Extra credit:
Extra credit:
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,236: Line 1,227:
17436881171909637738621006042549786426312886309400
17436881171909637738621006042549786426312886309400
</pre>
</pre>

=={{header|J}}==
=={{header|J}}==


Based on the echolisp implementation:
Based on the echolisp implementation:


<syntaxhighlight lang=J>leg=: dyad define
<syntaxhighlight lang="j">leg=: dyad define
x (y&|)@^ (y-1)%2
x (y&|)@^ (y-1)%2
)
)
Line 1,278: Line 1,268:
Task examples:
Task examples:


<syntaxhighlight lang=J> 10 cipolla 13
<syntaxhighlight lang="j"> 10 cipolla 13
6 7
6 7
56 cipolla 101
56 cipolla 101
Line 1,295: Line 1,285:
34035243914635549601583369544560650254325084643201x cipolla (10^50x) + 151
34035243914635549601583369544560650254325084643201x cipolla (10^50x) + 151
17436881171909637738621006042549786426312886309400 82563118828090362261378993957450213573687113690751</syntaxhighlight>
17436881171909637738621006042549786426312886309400 82563118828090362261378993957450213573687113690751</syntaxhighlight>

=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
{{works with|Java|8}}
{{works with|Java|8}}
<syntaxhighlight lang=Java>import java.math.BigInteger;
<syntaxhighlight lang="java">import java.math.BigInteger;
import java.util.function.BiFunction;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Function;
Line 1,416: Line 1,405:
(791399408049, 208600591990, true)
(791399408049, 208600591990, true)
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true)</pre>
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true)</pre>

=={{header|jq}}==
=={{header|jq}}==
{{trans|Wren}}
{{trans|Wren}}
Line 1,422: Line 1,410:


A Point is represented by a numeric array of length two (i.e. [x,y]).
A Point is represented by a numeric array of length two (i.e. [x,y]).
<syntaxhighlight lang=jq># To take advantage of gojq's arbitrary-precision integer arithmetic:
<syntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);


Line 1,511: Line 1,499:
[82563118828090362261378993957450213573687113690751,17436881171909637738621006042549786426312886309400,true]
[82563118828090362261378993957450213573687113690751,17436881171909637738621006042549786426312886309400,true]
</pre>
</pre>


=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Perl}}
{{trans|Perl}}
<syntaxhighlight lang=julia>using Primes
<syntaxhighlight lang="julia">using Primes


function legendre(n, p)
function legendre(n, p)
Line 1,572: Line 1,558:
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151.
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151.
</pre>
</pre>

=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=scala>// version 1.2.0
<syntaxhighlight lang="scala">// version 1.2.0


import java.math.BigInteger
import java.math.BigInteger
Line 1,654: Line 1,639:
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true)
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true)
</pre>
</pre>

=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>ClearAll[Cipolla]
<syntaxhighlight lang="mathematica">ClearAll[Cipolla]
Cipolla[n_, p_] := Module[{ls, omega2, nn, a, r, s},
Cipolla[n_, p_] := Module[{ls, omega2, nn, a, r, s},
ls = JacobiSymbol[n, p];
ls = JacobiSymbol[n, p];
Line 1,704: Line 1,688:
{791399408049,208600591990,True}
{791399408049,208600591990,True}
{82563118828090362261378993957450213573687113690751,17436881171909637738621006042549786426312886309400,True}</pre>
{82563118828090362261378993957450213573687113690751,17436881171909637738621006042549786426312886309400,True}</pre>

=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|bignum}}
{{libheader|bignum}}
<syntaxhighlight lang=Nim>import options
<syntaxhighlight lang="nim">import options
import bignum
import bignum


Line 1,798: Line 1,781:
(791399408049, 208600591990)
(791399408049, 208600591990)
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400)</pre>
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400)</pre>

=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
{{libheader|ntheory}}
{{libheader|ntheory}}
<syntaxhighlight lang=perl>use bigint;
<syntaxhighlight lang="perl">use bigint;
use ntheory qw(is_prime);
use ntheory qw(is_prime);


Line 1,861: Line 1,843:
Roots of 665165880 are (475131702, 524868305) mod 1000000007
Roots of 665165880 are (475131702, 524868305) mod 1000000007
Roots of 881398088036 are (791399408049, 208600591990) mod 1000000000039</pre>
Roots of 881398088036 are (791399408049, 208600591990) mod 1000000000039</pre>

=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Phix/mpfr}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,974: Line 1,955:
{"82563118828090362261378993957450213573687113690751","17436881171909637738621006042549786426312886309400","true"}}
{"82563118828090362261378993957450213573687113690751","17436881171909637738621006042549786426312886309400","true"}}
</pre>
</pre>

=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=PicoLisp># from @lib/rsa.l
<syntaxhighlight lang="picolisp"># from @lib/rsa.l
(de **Mod (X Y N)
(de **Mod (X Y N)
(let M 1
(let M 1
Line 2,049: Line 2,029:
(82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400)
(82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400)
</pre>
</pre>

=={{header|Python}}==
=={{header|Python}}==
<syntaxhighlight lang=python>
<syntaxhighlight lang="python">
#Converts n to base b as a list of integers between 0 and b-1
#Converts n to base b as a list of integers between 0 and b-1
#Most-significant digit on the left
#Most-significant digit on the left
Line 2,111: Line 2,090:
Roots of 8219 mod 10007: ()
Roots of 8219 mod 10007: ()
</pre>
</pre>

=={{header|Racket}}==
=={{header|Racket}}==
{{trans|EchoLisp}}
{{trans|EchoLisp}}


<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(require math/number-theory)
(require math/number-theory)
Line 2,200: Line 2,178:
Roots of 881398088036 are (208600591990,791399408049) (mod 1000000000039)
Roots of 881398088036 are (208600591990,791399408049) (mod 1000000000039)
Roots of 34035243914635549601583369544560650254325084643201 are (17436881171909637738621006042549786426312886309400,82563118828090362261378993957450213573687113690751) (mod 100000000000000000000000000000000000000000000000151)</pre>
Roots of 34035243914635549601583369544560650254325084643201 are (17436881171909637738621006042549786426312886309400,82563118828090362261378993957450213573687113690751) (mod 100000000000000000000000000000000000000000000000151)</pre>

=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
Line 2,206: Line 2,183:
{{trans|Sidef}}
{{trans|Sidef}}


<syntaxhighlight lang=raku line># Legendre operator (𝑛│𝑝)
<syntaxhighlight lang="raku" line># Legendre operator (𝑛│𝑝)
sub infix:<│> (Int \𝑛, Int \𝑝 where 𝑝.is-prime && (𝑝 != 2)) {
sub infix:<│> (Int \𝑛, Int \𝑝 where 𝑝.is-prime && (𝑝 != 2)) {
given 𝑛.expmod( (𝑝-1) div 2, 𝑝 ) {
given 𝑛.expmod( (𝑝-1) div 2, 𝑝 ) {
Line 2,277: Line 2,254:
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151
</pre>
</pre>

=={{header|Sage}}==
=={{header|Sage}}==
{{works with|Sage|7.6}}
{{works with|Sage|7.6}}
<syntaxhighlight lang=sage>
<syntaxhighlight lang="sage">
def eulerCriterion(a, p):
def eulerCriterion(a, p):
return -1 if pow(a, int((p-1)/2), p) == p-1 else 1
return -1 if pow(a, int((p-1)/2), p) == p-1 else 1
Line 2,349: Line 2,325:
False
False
</pre>
</pre>

=={{header|Scala}}==
=={{header|Scala}}==
===Imperative solution===
===Imperative solution===
<syntaxhighlight lang=Scala>object CipollasAlgorithm extends App {
<syntaxhighlight lang="scala">object CipollasAlgorithm extends App {
private val BIG = BigInt(10).pow(50) + BigInt(151)
private val BIG = BigInt(10).pow(50) + BigInt(151)


Line 2,407: Line 2,382:
}</syntaxhighlight>
}</syntaxhighlight>
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/QQBsMza/3 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/NEP5hOWmSBqqpwmF30LpUA Scastie (JVM)].
{{Out}}See it running in your browser by [https://scalafiddle.io/sf/QQBsMza/3 ScalaFiddle (JavaScript, non JVM)] or by [https://scastie.scala-lang.org/NEP5hOWmSBqqpwmF30LpUA Scastie (JVM)].

=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Go}}
{{trans|Go}}
<syntaxhighlight lang=ruby>func cipolla(n, p) {
<syntaxhighlight lang="ruby">func cipolla(n, p) {


legendre(n, p) == 1 || return nil
legendre(n, p) == 1 || return nil
Line 2,468: Line 2,442:
Roots of 881398088036 are (791399408049 208600591990) mod 1000000000039
Roots of 881398088036 are (791399408049 208600591990) mod 1000000000039
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151</pre>
Roots of 34035243914635549601583369544560650254325084643201 are (82563118828090362261378993957450213573687113690751 17436881171909637738621006042549786426312886309400) mod 100000000000000000000000000000000000000000000000151</pre>

=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<syntaxhighlight lang=vbnet>Imports System.Numerics
<syntaxhighlight lang="vbnet">Imports System.Numerics


Module Module1
Module Module1
Line 2,552: Line 2,525:
(791399408049, 208600591990, True)
(791399408049, 208600591990, True)
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, True)</pre>
(82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, True)</pre>

=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
<syntaxhighlight lang=ecmascript>import "/big" for BigInt
<syntaxhighlight lang="ecmascript">import "/big" for BigInt
import "/dynamic" for Tuple
import "/dynamic" for Tuple


Line 2,628: Line 2,600:
[82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true]
[82563118828090362261378993957450213573687113690751, 17436881171909637738621006042549786426312886309400, true]
</pre>
</pre>

=={{header|zkl}}==
=={{header|zkl}}==
{{trans|EchoLisp}}
{{trans|EchoLisp}}
Uses lib GMP (GNU MP Bignum Library).
Uses lib GMP (GNU MP Bignum Library).
<syntaxhighlight lang=zkl>var [const] BN=Import("zklBigNum"); //libGMP
<syntaxhighlight lang="zkl">var [const] BN=Import("zklBigNum"); //libGMP
fcn modEq(a,b,p) { (a-b)%p==0 }
fcn modEq(a,b,p) { (a-b)%p==0 }
fcn Legendre(a,p){ a.powm((p - 1)/2,p) }
fcn Legendre(a,p){ a.powm((p - 1)/2,p) }
Line 2,663: Line 2,634:
return(x,(p-x)%p);
return(x,(p-x)%p);
}</syntaxhighlight>
}</syntaxhighlight>
<syntaxhighlight lang=zkl>foreach n,p in (T(
<syntaxhighlight lang="zkl">foreach n,p in (T(
T(10,13),T(56,101),T(8218,10007),T(8219,10007),T(331575,1000003),
T(10,13),T(56,101),T(8218,10007),T(8219,10007),T(331575,1000003),
T(665165880,1000000007),T(881398088036,1000000000039),
T(665165880,1000000007),T(881398088036,1000000000039),