Cipolla's algorithm: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 79:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F convertToBase(n, b)
I (n < 2)
R [n]
Line 125:
print(‘Roots of 56 mod 101: ’cipolla(56, 101))
print(‘Roots of 1 mod 11: ’cipolla(1, 11))
print(‘Roots of 8219 mod 10007: ’cipolla(8219, 10007))</langsyntaxhighlight>
 
{{out}}
Line 138:
=={{header|C}}==
{{trans|FreeBasic}}
<langsyntaxhighlight lang=c>#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Line 327:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Find solution for n = 10 and p = 13
Line 348:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>using System;
using System.Numerics;
 
Line 424:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>(6, 7, True)
Line 437:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=D>import std.bigint;
import std.stdio;
import std.typecons;
Line 520:
writeln(c("881398088036", "1000000000039"));
writeln(c("34035243914635549601583369544560650254325084643201", ""));
}</langsyntaxhighlight>
{{out}}
<pre>Tuple!(BigInt, "x", BigInt, "y", bool, "b")(6, 7, true)
Line 532:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang=scheme>
(lib 'struct)
(lib 'types)
Line 584:
(assert (mod= n (* x x) p)) ;; checking the result
(printf "Roots of %d are (%d,%d) (mod %d)" n x (% (- p x) p) p))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 609:
===The function===
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<langsyntaxhighlight lang=fsharp>
// Cipolla's algorithm. Nigel Galloway: June 16th., 2019
let Cipolla n g =
Line 621:
|_->let n,i=Seq.item α fE in ((Πn*n+Πi*i*b)%g)
if fN 1I n ((g-1I)/2I) g<>1I then None else Some(fL 1I 0I 0 ((g+1I)/2I))
</syntaxhighlight>
</lang>
===The Task===
<langsyntaxhighlight lang=fsharp>
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)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 643:
{{trans|Sidef}}
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang=factor>USING: accessors assocs interpolate io kernel literals locals
math math.extras math.functions ;
 
Line 693:
[ 2dup - [I Roots of ${3} are (${1} ${0}) mod ${2}I] ]
[ [I No solution for (${}, ${})I] ] if* nl
] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 710:
Had a close look at the EchoLisp code for step 2.
Used the FreeBASIC code from the Miller-Rabin task for prime testing.
<langsyntaxhighlight lang=freebasic>' version 08-04-2017
' compile with: fbc -s console
' maximum for p is 17 digits to be on the save side
Line 896:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Find solution for n = 10 and p = 13
Line 920:
===GMP version===
{{libheader|GMP}}
<langsyntaxhighlight lang=freebasic>' version 12-04-2017
' compile with: fbc -s console
 
Line 1,067:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Find solution for n = 10 and p = 13
Line 1,096:
===int===
Implementation following the pseudocode in the task description.
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,158:
fmt.Println(c(8219, 10007))
fmt.Println(c(331575, 1000003))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,169:
===big.Int===
Extra credit:
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,228:
fmt.Println(&R1)
fmt.Println(&R2)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,241:
Based on the echolisp implementation:
 
<langsyntaxhighlight lang=J>leg=: dyad define
x (y&|)@^ (y-1)%2
)
Line 1,274:
assert. 'not a valid square root'
end.
)</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight lang=J> 10 cipolla 13
6 7
56 cipolla 101
Line 1,294:
208600591990 791399408049
34035243914635549601583369544560650254325084643201x cipolla (10^50x) + 151
17436881171909637738621006042549786426312886309400 82563118828090362261378993957450213573687113690751</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
{{works with|Java|8}}
<langsyntaxhighlight lang=Java>import java.math.BigInteger;
import java.util.function.BiFunction;
import java.util.function.Function;
Line 1,406:
System.out.println(c("34035243914635549601583369544560650254325084643201", ""));
}
}</langsyntaxhighlight>
{{out}}
<pre>(6, 7, true)
Line 1,422:
 
A Point is represented by a numeric array of length two (i.e. [x,y]).
<langsyntaxhighlight 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);
 
Line 1,499:
;
 
exercise</langsyntaxhighlight>
{{out}}
<pre>
Line 1,515:
=={{header|Julia}}==
{{trans|Perl}}
<langsyntaxhighlight lang=julia>using Primes
 
function legendre(n, p)
Line 1,561:
println(r > 0 ? "Roots of $n are ($r, $(p - r)) mod $p." : "No solution for ($n, $p)")
end
</langsyntaxhighlight>{{out}}
<pre>
Roots of 10 are (6, 7) mod 13.
Line 1,575:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang=scala>// version 1.2.0
 
import java.math.BigInteger
Line 1,641:
println(c("881398088036", "1000000000039"))
println(c("34035243914635549601583369544560650254325084643201", ""))
}</langsyntaxhighlight>
 
{{out}}
Line 1,656:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[Cipolla]
Cipolla[n_, p_] := Module[{ls, omega2, nn, a, r, s},
ls = JacobiSymbol[n, p];
Line 1,694:
Cipolla[665165880, 1000000007]
Cipolla[881398088036, 1000000000039]
Cipolla[34035243914635549601583369544560650254325084643201, 10^50 + 151]</langsyntaxhighlight>
{{out}}
<pre>{6,7,True}
Line 1,708:
{{trans|Kotlin}}
{{libheader|bignum}}
<langsyntaxhighlight lang=Nim>import options
import bignum
 
Line 1,787:
let sols = c(n, p)
if sols.isSome: echo sols.get()
else: echo "No solutions."</langsyntaxhighlight>
 
{{out}}
Line 1,802:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use bigint;
use ntheory qw(is_prime);
 
Line 1,852:
$r ? printf "Roots of %d are (%d, %d) mod %d\n", $n, $r, $p-$r, $p
: print "No solution for ($n, $p)\n"
}</langsyntaxhighlight>
{{out}}
<pre>Roots of 10 are (6, 7) mod 13
Line 1,865:
{{trans|Kotlin}}
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<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>
Line 1,960:
<span style="color: #0000FF;">?{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cipolla</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
Obviously were you to use that in anger, you would probably rip out a few mpz_get_str() and return NULL/false rather than "0"/"false", etc.
{{out}}
Line 1,977:
=={{header|PicoLisp}}==
{{trans|Go}}
<langsyntaxhighlight lang=PicoLisp># from @lib/rsa.l
(de **Mod (X Y N)
(let M 1
Line 2,037:
(println (ci 665165880 1000000007))
(println (ci 881398088036 1000000000039))
(println (ci 34035243914635549601583369544560650254325084643201 (+ (** 10 50) 151)))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,051:
 
=={{header|Python}}==
<langsyntaxhighlight lang=python>
#Converts n to base b as a list of integers between 0 and b-1
#Most-significant digit on the left
Line 2,102:
print "Roots of 1 mod 11: " +str(cipolla(1,11))
print "Roots of 8219 mod 10007: " +str(cipolla(8219,10007))
</syntaxhighlight>
</lang>
{{out}}
 
Line 2,115:
{{trans|EchoLisp}}
 
<langsyntaxhighlight lang=racket>#lang racket
 
(require math/number-theory)
Line 2,188:
(report-Cipolla 881398088036 1000000000039)
(report-Cipolla 34035243914635549601583369544560650254325084643201
100000000000000000000000000000000000000000000000151))</langsyntaxhighlight>
 
{{out}}
Line 2,206:
{{trans|Sidef}}
 
<syntaxhighlight lang=raku perl6line># Legendre operator (𝑛│𝑝)
sub infix:<│> (Int \𝑛, Int \𝑝 where 𝑝.is-prime && (𝑝 != 2)) {
given 𝑛.expmod( (𝑝-1) div 2, 𝑝 ) {
Line 2,265:
!! "No solution for ($n, $p)"
}
</syntaxhighlight>
</lang>
{{out}}
<pre>Roots of 10 are (6, 7) mod 13
Line 2,280:
=={{header|Sage}}==
{{works with|Sage|7.6}}
<langsyntaxhighlight lang=sage>
def eulerCriterion(a, p):
return -1 if pow(a, int((p-1)/2), p) == p-1 else 1
Line 2,334:
 
return sorted(out)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,352:
=={{header|Scala}}==
===Imperative solution===
<langsyntaxhighlight lang=Scala>object CipollasAlgorithm extends App {
private val BIG = BigInt(10).pow(50) + BigInt(151)
 
Line 2,405:
}
 
}</langsyntaxhighlight>
{{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}}==
{{trans|Go}}
<langsyntaxhighlight lang=ruby>func cipolla(n, p) {
 
legendre(n, p) == 1 || return nil
Line 2,458:
say "No solution for (#{n}, #{p})"
}
}</langsyntaxhighlight>
{{out}}
<pre>Roots of 10 are (6 7) mod 13
Line 2,471:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Imports System.Numerics
 
Module Module1
Line 2,542:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>(6, 7, True)
Line 2,557:
{{libheader|Wren-big}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight lang=ecmascript>import "/big" for BigInt
import "/dynamic" for Tuple
 
Line 2,615:
System.print(c.call("665165880", "1000000007"))
System.print(c.call("881398088036", "1000000000039"))
System.print(c.call("34035243914635549601583369544560650254325084643201", ""))</langsyntaxhighlight>
 
{{out}}
Line 2,632:
{{trans|EchoLisp}}
Uses lib GMP (GNU MP Bignum Library).
<langsyntaxhighlight lang=zkl>var [const] BN=Import("zklBigNum"); //libGMP
fcn modEq(a,b,p) { (a-b)%p==0 }
fcn Legendre(a,p){ a.powm((p - 1)/2,p) }
Line 2,662:
println("Roots of %d are (%d,%d) (mod %d)".fmt(n,x,(p-x)%p,p));
return(x,(p-x)%p);
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>foreach n,p in (T(
T(10,13),T(56,101),T(8218,10007),T(8219,10007),T(331575,1000003),
T(665165880,1000000007),T(881398088036,1000000000039),
Line 2,669:
BN(10).pow(50) + 151) )){
try{ Cipolla(n,p) }catch{ println(__exception) }
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits