Carmichael 3 strong pseudoprimes: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
m (syntax highlighting fixup automation)
Line 36: Line 36:
=={{header|11l}}==
=={{header|11l}}==
{{trans|D}}
{{trans|D}}
<lang 11l>F mod_(n, m)
<syntaxhighlight lang=11l>F mod_(n, m)
R ((n % m) + m) % m
R ((n % m) + m) % m
Line 67: Line 67:
I !is_prime(r) | (q * r) % (p - 1) != 1
I !is_prime(r) | (q * r) % (p - 1) != 1
L.continue
L.continue
print(p‘ x ’q‘ x ’r)</lang>
print(p‘ x ’q‘ x ’r)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 93: Line 93:
Uses the Miller_Rabin package from
Uses the Miller_Rabin package from
[[Miller-Rabin primality test#ordinary integers]].
[[Miller-Rabin primality test#ordinary integers]].
<lang Ada>with Ada.Text_IO, Miller_Rabin;
<syntaxhighlight lang=Ada>with Ada.Text_IO, Miller_Rabin;


procedure Nemesis is
procedure Nemesis is
Line 133: Line 133:
end if;
end if;
end loop;
end loop;
end Nemesis;</lang>
end Nemesis;</syntaxhighlight>


{{out}}
{{out}}
Line 150: Line 150:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Uses the Sieve of Eratosthenes code from the Smith Numbers task with an increased upper-bound (included here for convenience).
Uses the Sieve of Eratosthenes code from the Smith Numbers task with an increased upper-bound (included here for convenience).
<lang algol68># sieve of Eratosthene: sets s[i] to TRUE if i is prime, FALSE otherwise #
<syntaxhighlight lang=algol68># sieve of Eratosthene: sets s[i] to TRUE if i is prime, FALSE otherwise #
PROC sieve = ( REF[]BOOL s )VOID:
PROC sieve = ( REF[]BOOL s )VOID:
BEGIN
BEGIN
Line 190: Line 190:
OD
OD
FI
FI
OD</lang>
OD</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 223: Line 223:


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<syntaxhighlight lang=AWK>
# syntax: GAWK -f CARMICHAEL_3_STRONG_PSEUDOPRIMES.AWK
# syntax: GAWK -f CARMICHAEL_3_STRONG_PSEUDOPRIMES.AWK
# converted from C
# converted from C
Line 266: Line 266:
return(((n%m)+m)%m)
return(((n%m)+m)%m)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 345: Line 345:
=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang BASIC256>
<syntaxhighlight lang=BASIC256>
for i = 3 to max_sieve step 2
for i = 3 to max_sieve step 2
isprime[i] = 1
isprime[i] = 1
Line 382: Line 382:
next i
next i
end
end
</syntaxhighlight>
</lang>




=={{header|C}}==
=={{header|C}}==
<syntaxhighlight lang=C>
<lang C>
#include <stdio.h>
#include <stdio.h>


Line 435: Line 435:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 456: Line 456:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iomanip>
<syntaxhighlight lang=cpp>#include <iomanip>
#include <iostream>
#include <iostream>


Line 509: Line 509:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 585: Line 585:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>
<syntaxhighlight lang=lisp>
(ns example
(ns example
(:gen-class))
(:gen-class))
Line 613: Line 613:
(doseq [t numbers]
(doseq [t numbers]
(println (format "%5d x %5d x %5d = %10d" (first t) (second t) (last t) (apply * t))))
(println (format "%5d x %5d x %5d = %10d" (first t) (second t) (last t) (apply * t))))
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 690: Line 690:


=={{header|D}}==
=={{header|D}}==
<lang d>enum mod = (in int n, in int m) pure nothrow @nogc=> ((n % m) + m) % m;
<syntaxhighlight lang=d>enum mod = (in int n, in int m) pure nothrow @nogc=> ((n % m) + m) % m;


bool isPrime(in uint n) pure nothrow @nogc {
bool isPrime(in uint n) pure nothrow @nogc {
Line 722: Line 722:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3 x 11 x 17
<pre>3 x 11 x 17
Line 795: Line 795:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang=scheme>
;; charmichaël numbers up to N-th prime ; 61 is 18-th prime
;; charmichaël numbers up to N-th prime ; 61 is 18-th prime
(define (charms (N 18) local: (h31 0) (Prime2 0) (Prime3 0))
(define (charms (N 18) local: (h31 0) (Prime2 0) (Prime3 0))
Line 810: Line 810:
#:when (= 1 (modulo (* Prime2 Prime3) (1- Prime1)))
#:when (= 1 (modulo (* Prime2 Prime3) (1- Prime1)))
(printf " 💥 %12d = %d x %d x %d" (* Prime1 Prime2 Prime3) Prime1 Prime2 Prime3)))
(printf " 💥 %12d = %d x %d x %d" (* Prime1 Prime2 Prime3) Prime1 Prime2 Prime3)))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<lang scheme>
<syntaxhighlight lang=scheme>
(charms 3)
(charms 3)
💥 561 = 3 x 11 x 17
💥 561 = 3 x 11 x 17
Line 835: Line 835:
💥 6189121 = 61 x 241 x 421
💥 6189121 = 61 x 241 x 421
💥 824389441 = 61 x 3361 x 4021
💥 824389441 = 61 x 3361 x 4021
</syntaxhighlight>
</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
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#)]
<lang fsharp>
<syntaxhighlight lang=fsharp>
// Carmichael Number . Nigel Galloway: November 19th., 2017
// Carmichael Number . Nigel Galloway: November 19th., 2017
let fN n = Seq.collect ((fun g->(Seq.map(fun e->(n,1+(n-1)*(n+g)/e,g,e))){1..(n+g-1)})){2..(n-1)}
let fN n = Seq.collect ((fun g->(Seq.map(fun e->(n,1+(n-1)*(n+g)/e,g,e))){1..(n+g-1)})){2..(n-1)}
Line 848: Line 848:
let carms g = primes|>Seq.takeWhile(fun n->n<=g)|>Seq.collect fN|>Seq.choose fG
let carms g = primes|>Seq.takeWhile(fun n->n<=g)|>Seq.collect fN|>Seq.choose fG
carms 61 |> Seq.iter (fun (P1,P2,P3)->printfn "%2d x %4d x %5d = %10d" P1 P2 P3 ((uint64 P3)*(uint64 (P1*P2))))
carms 61 |> Seq.iter (fun (P1,P2,P3)->printfn "%2d x %4d x %5d = %10d" P1 P2 P3 ((uint64 P3)*(uint64 (P1*P2))))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 924: Line 924:
=={{header|Factor}}==
=={{header|Factor}}==
Note the use of <code>rem</code> instead of <code>mod</code> when the remainder should always be positive.
Note the use of <code>rem</code> instead of <code>mod</code> when the remainder should always be positive.
<lang factor>USING: formatting kernel locals math math.primes math.ranges
<syntaxhighlight lang=factor>USING: formatting kernel locals math math.primes math.ranges
sequences ;
sequences ;
IN: rosetta-code.carmichael
IN: rosetta-code.carmichael
Line 946: Line 946:
: carmichael-demo ( -- ) 61 primes-upto [ carmichael ] each ;
: carmichael-demo ( -- ) 61 primes-upto [ carmichael ] each ;


MAIN: carmichael-demo</lang>
MAIN: carmichael-demo</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,024: Line 1,024:
This is F77 style, and directly translates the given calculation as per ''formula translation''. It turns out that the normal integers suffice for the demonstration, except for just one of the products of the three primes: 41x1721x35281 = 2489462641, which is bigger than 2147483647, the 32-bit limit. Fortunately, INTEGER*8 variables are also available, so the extension is easy. Otherwise, one would have to mess about with using two integers in a bignum style, one holding say the millions, and the second the number up to a million.
This is F77 style, and directly translates the given calculation as per ''formula translation''. It turns out that the normal integers suffice for the demonstration, except for just one of the products of the three primes: 41x1721x35281 = 2489462641, which is bigger than 2147483647, the 32-bit limit. Fortunately, INTEGER*8 variables are also available, so the extension is easy. Otherwise, one would have to mess about with using two integers in a bignum style, one holding say the millions, and the second the number up to a million.
===Source===
===Source===
So, using the double MOD approach (see the ''Discussion'') - which gives the same result for either style of MOD... <lang Fortran> LOGICAL FUNCTION ISPRIME(N) !Ad-hoc, since N is not going to be big...
So, using the double MOD approach (see the ''Discussion'') - which gives the same result for either style of MOD... <syntaxhighlight lang=Fortran> LOGICAL FUNCTION ISPRIME(N) !Ad-hoc, since N is not going to be big...
INTEGER N !Despite this intimidating allowance of 32 bits...
INTEGER N !Despite this intimidating allowance of 32 bits...
INTEGER F !A possible factor.
INTEGER F !A possible factor.
Line 1,063: Line 1,063:
END IF
END IF
END DO
END DO
END</lang>
END</syntaxhighlight>


===Output===
===Output===
Line 1,141: Line 1,141:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 17-10-2016
<syntaxhighlight lang=freebasic>' version 17-10-2016
' compile with: fbc -s console
' compile with: fbc -s console


Line 1,199: Line 1,199:
Print : Print "hit any key to end program"
Print : Print "hit any key to end program"
Sleep
Sleep
End</lang>
End</syntaxhighlight>
{{out}}
{{out}}
<pre> 3 * 11 * 17
<pre> 3 * 11 * 17
Line 1,272: Line 1,272:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang=go>package main


import "fmt"
import "fmt"
Line 1,319: Line 1,319:
if isPrime(p1) { carmichael(p1) }
if isPrime(p1) { carmichael(p1) }
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,403: Line 1,403:
{{Works with|GHC|7.4.1}}
{{Works with|GHC|7.4.1}}
{{Works with|primes|0.2.1.0}}
{{Works with|primes|0.2.1.0}}
<lang haskell>#!/usr/bin/runhaskell
<syntaxhighlight lang=haskell>#!/usr/bin/runhaskell


import Data.Numbers.Primes
import Data.Numbers.Primes
Line 1,420: Line 1,420:
return (p, q, r)
return (p, q, r)


main = putStr $ unlines $ map show carmichaels</lang>
main = putStr $ unlines $ map show carmichaels</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,497: Line 1,497:


The following works in both languages.
The following works in both languages.
<lang unicon>link "factors"
<syntaxhighlight lang=unicon>link "factors"


procedure main(A)
procedure main(A)
Line 1,520: Line 1,520:
procedure format(p1,p2,p3)
procedure format(p1,p2,p3)
return left(p1||" * "||p2||" * "||p3,20)||" = "||(p1*p2*p3)
return left(p1||" * "||p2||" * "||p3,20)||" = "||(p1*p2*p3)
end</lang>
end</syntaxhighlight>


Output, with middle lines elided:
Output, with middle lines elided:
Line 1,556: Line 1,556:


=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang=J>
<lang J>
q =: (,"0 1~ >:@i.@<:@+/"1)&.>@(,&.>"0 1~ >:@i.)&.>@I.@(1&p:@i.)@>:
q =: (,"0 1~ >:@i.@<:@+/"1)&.>@(,&.>"0 1~ >:@i.)&.>@I.@(1&p:@i.)@>:
f1 =: (0: = {. | <:@{: * 1&{ + {:) *. ((1&{ | -@*:@{:) = 1&{ | {.)
f1 =: (0: = {. | <:@{: * 1&{ + {:) *. ((1&{ | -@*:@{:) = 1&{ | {.)
Line 1,563: Line 1,563:
p3 =: 3:$0:`((* 1&p:)@({: , {. , (<.@>:@(1&{ %~ {. * {:))))@.(*@{.)@(p2 , }.)
p3 =: 3:$0:`((* 1&p:)@({: , {. , (<.@>:@(1&{ %~ {. * {:))))@.(*@{.)@(p2 , }.)
(-. 3:$0:)@(((*"0 f2)@p3"1)@;@;@q) 61
(-. 3:$0:)@(((*"0 f2)@p3"1)@;@;@q) 61
</syntaxhighlight>
</lang>
Output
Output
<pre>
<pre>
Line 1,639: Line 1,639:
=={{header|Java}}==
=={{header|Java}}==
{{trans|D}}
{{trans|D}}
<lang java>public class Test {
<syntaxhighlight lang=java>public class Test {


static int mod(int n, int m) {
static int mod(int n, int m) {
Line 1,677: Line 1,677:
}
}
}
}
}</lang>
}</syntaxhighlight>
<pre>3 x 11 x 17
<pre>3 x 11 x 17
5 x 29 x 73
5 x 29 x 73
Line 1,752: Line 1,752:


'''Function'''
'''Function'''
<lang julia>using Primes
<syntaxhighlight lang=julia>using Primes


function carmichael(pmax::Integer)
function carmichael(pmax::Integer)
Line 1,772: Line 1,772:
end
end
return reshape(car, 3, length(car) ÷ 3)
return reshape(car, 3, length(car) ÷ 3)
end</lang>
end</syntaxhighlight>


'''Main'''
'''Main'''
<lang julia>hi = 61
<syntaxhighlight lang=julia>hi = 61
car = carmichael(hi)
car = carmichael(hi)


Line 1,796: Line 1,796:
@printf("p× %d × %d = %d ", q, r, c)
@printf("p× %d × %d = %d ", q, r, c)
end
end
println("\n\n", size(car)[2], " results in total.")</lang>
println("\n\n", size(car)[2], " results in total.")</syntaxhighlight>


{{out}}
{{out}}
Line 1,846: Line 1,846:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|D}}
{{trans|D}}
<lang scala>fun Int.isPrime(): Boolean {
<syntaxhighlight lang=scala>fun Int.isPrime(): Boolean {
return when {
return when {
this == 2 -> true
this == 2 -> true
Line 1,881: Line 1,881:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
See D output.
See D output.


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>local function isprime(n)
<syntaxhighlight lang=lua>local function isprime(n)
if n < 2 then return false end
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
if n % 2 == 0 then return n==2 end
Line 1,926: Line 1,926:
end
end
end
end
print(found.." found.")</lang>
print(found.." found.")</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:30ex;overflow:scroll">3 × 11 × 17 = 561
<pre style="height:30ex;overflow:scroll">3 × 11 × 17 = 561
Line 2,000: Line 2,000:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang mathematica>Cases[Cases[
<syntaxhighlight lang=mathematica>Cases[Cases[
Cases[Table[{p1, h3, d}, {p1, Array[Prime, PrimePi@61]}, {h3, 2,
Cases[Table[{p1, h3, d}, {p1, Array[Prime, PrimePi@61]}, {h3, 2,
p1 - 1}, {d, 1, h3 + p1 - 1}], {p1_Integer, h3_, d_} /;
p1 - 1}, {d, 1, h3 + p1 - 1}], {p1_Integer, h3_, d_} /;
Line 2,007: Line 2,007:
Infinity], {p1_, p2_, h3_} /; PrimeQ[1 + Floor[p1 p2/h3]] :> {p1,
Infinity], {p1_, p2_, h3_} /; PrimeQ[1 + Floor[p1 p2/h3]] :> {p1,
p2, 1 + Floor[p1 p2/h3]}], {p1_, p2_, p3_} /;
p2, 1 + Floor[p1 p2/h3]}], {p1_, p2_, p3_} /;
Mod[p2 p3, p1 - 1] == 1 :> Print[p1, "*", p2, "*", p3]]</lang>
Mod[p2 p3, p1 - 1] == 1 :> Print[p1, "*", p2, "*", p3]]</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Vala}} with some modifications
{{trans|Vala}} with some modifications
<lang nim>import strformat
<syntaxhighlight lang=nim>import strformat


func isPrime(n: int64): bool =
func isPrime(n: int64): bool =
Line 2,041: Line 2,041:
if not isPrime(r) or (q * r) mod (p - 1) != 1:
if not isPrime(r) or (q * r) mod (p - 1) != 1:
continue
continue
echo &"{p:5} × {q:5} × {r:5} = {p * q * r:10}"</lang>
echo &"{p:5} × {q:5} × {r:5} = {p * q * r:10}"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,116: Line 2,116:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>f(p)={
<syntaxhighlight lang=parigp>f(p)={
my(v=List(),q,r);
my(v=List(),q,r);
for(h=2,p-1,
for(h=2,p-1,
Line 2,127: Line 2,127:
Set(v)
Set(v)
};
};
forprime(p=3,67,v=f(p); for(i=1,#v,print1(v[i]", ")))</lang>
forprime(p=3,67,v=f(p); for(i=1,#v,print1(v[i]", ")))</syntaxhighlight>
{{out}}
{{out}}
<pre>561, 1105, 2465, 10585, 1729, 2821, 6601, 8911, 15841, 52633, 29341, 46657, 115921, 314821, 530881, 162401, 7207201, 334153, 1024651, 1615681, 3581761, 5444489, 399001, 512461, 1193221, 1857241, 5049001, 5481451, 471905281, 294409, 488881, 1461241, 8134561, 36765901, 252601, 410041, 1909001, 5148001, 7519441, 434932961, 2489462641, 1152271, 3057601, 5968873, 6868261, 11972017, 14913991, 15829633, 43331401, 67902031, 368113411, 564651361, 104569501, 902645857, 958762729, 2508013, 4335241, 17316001, 178837201, 6189121, 9439201, 15247621, 35703361, 60957361, 99036001, 101649241, 329769721, 824389441, 1574601601, 10267951, 163954561, 7991602081,</pre>
<pre>561, 1105, 2465, 10585, 1729, 2821, 6601, 8911, 15841, 52633, 29341, 46657, 115921, 314821, 530881, 162401, 7207201, 334153, 1024651, 1615681, 3581761, 5444489, 399001, 512461, 1193221, 1857241, 5049001, 5481451, 471905281, 294409, 488881, 1461241, 8134561, 36765901, 252601, 410041, 1909001, 5148001, 7519441, 434932961, 2489462641, 1152271, 3057601, 5968873, 6868261, 11972017, 14913991, 15829633, 43331401, 67902031, 368113411, 564651361, 104569501, 902645857, 958762729, 2508013, 4335241, 17316001, 178837201, 6189121, 9439201, 15247621, 35703361, 60957361, 99036001, 101649241, 329769721, 824389441, 1574601601, 10267951, 163954561, 7991602081,</pre>
Line 2,133: Line 2,133:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use ntheory qw/forprimes is_prime vecprod/;
<syntaxhighlight lang=perl>use ntheory qw/forprimes is_prime vecprod/;


forprimes { my $p = $_;
forprimes { my $p = $_;
Line 2,149: Line 2,149:
}
}
}
}
} 3,61;</lang>
} 3,61;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,164: Line 2,164:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<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: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 2,190: Line 2,190:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d Carmichael numbers found\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d Carmichael numbers found\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,206: Line 2,206:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de modulo (X Y)
<syntaxhighlight lang=PicoLisp>(de modulo (X Y)
(% (+ Y (% X Y)) Y) )
(% (+ Y (% X Y)) Y) )
Line 2,245: Line 2,245:
(prinl)
(prinl)
(bye)</lang>
(bye)</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>Carmichael: procedure options (main, reorder); /* 24 January 2014 */
<syntaxhighlight lang=PL/I>Carmichael: procedure options (main, reorder); /* 24 January 2014 */
declare (Prime1, Prime2, Prime3, h3, d) fixed binary (31);
declare (Prime1, Prime2, Prime3, h3, d) fixed binary (31);


Line 2,274: Line 2,274:
/* Uses is_prime from Rosetta Code PL/I. */
/* Uses is_prime from Rosetta Code PL/I. */


end Carmichael;</lang>
end Carmichael;</syntaxhighlight>
Results:
Results:
<pre>
<pre>
Line 2,373: Line 2,373:


=={{header|Prolog}}==
=={{header|Prolog}}==
<lang prolog>
<syntaxhighlight lang=prolog>
show(Limit) :-
show(Limit) :-
forall(
forall(
Line 2,405: Line 2,405:
N mod D =\= 0,
N mod D =\= 0,
D2 is D + A, prime(N, D2, As).
D2 is D + A, prime(N, D2, As).
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,524: Line 2,524:


=={{header|Python}}==
=={{header|Python}}==
<lang python>class Isprime():
<syntaxhighlight lang=python>class Isprime():
'''
'''
Extensible sieve of Eratosthenes
Extensible sieve of Eratosthenes
Line 2,594: Line 2,594:
ans = sorted(sum((carmichael(n) for n in range(62) if isprime(n)), []))
ans = sorted(sum((carmichael(n) for n in range(62) if isprime(n)), []))
print(',\n'.join(repr(ans[i:i+5])[1:-1] for i in range(0, len(ans)+1, 5)))</lang>
print(',\n'.join(repr(ans[i:i+5])[1:-1] for i in range(0, len(ans)+1, 5)))</syntaxhighlight>
{{out}}
{{out}}
<pre>(3, 11, 17), (5, 13, 17), (5, 17, 29), (5, 29, 73), (7, 13, 19),
<pre>(3, 11, 17), (5, 13, 17), (5, 17, 29), (5, 29, 73), (7, 13, 19),
Line 2,612: Line 2,612:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang=racket>
#lang racket
#lang racket
(require math)
(require math)
Line 2,629: Line 2,629:
(displayln (list p1 p2 p3 '=> (* p1 p2 p3))))))
(displayln (list p1 p2 p3 '=> (* p1 p2 p3))))))
(next (+ d 1))))))
(next (+ d 1))))))
</syntaxhighlight>
</lang>
Output:
Output:
<lang racket>
<syntaxhighlight lang=racket>
(3 11 17 => 561)
(3 11 17 => 561)
(5 29 73 => 10585)
(5 29 73 => 10585)
Line 2,694: Line 2,694:
(61 241 421 => 6189121)
(61 241 421 => 6189121)
(61 3361 4021 => 824389441)
(61 3361 4021 => 824389441)
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,700: Line 2,700:
{{works with|Rakudo|2015.12}}
{{works with|Rakudo|2015.12}}
An almost direct translation of the pseudocode. We take the liberty of going up to 67 to show we aren't limited to 32-bit integers. (Raku uses arbitrary precision in any case.)
An almost direct translation of the pseudocode. We take the liberty of going up to 67 to show we aren't limited to 32-bit integers. (Raku uses arbitrary precision in any case.)
<lang perl6>for (2..67).grep: *.is-prime -> \Prime1 {
<syntaxhighlight lang=raku line>for (2..67).grep: *.is-prime -> \Prime1 {
for 1 ^..^ Prime1 -> \h3 {
for 1 ^..^ Prime1 -> \h3 {
my \g = h3 + Prime1;
my \g = h3 + Prime1;
Line 2,714: Line 2,714:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>3 × 11 × 17 == 561
<pre>3 × 11 × 17 == 561
Line 2,795: Line 2,795:


Some code optimization was done, while not necessary for the small default number ('''61'''), &nbsp; it was significant for larger numbers.
Some code optimization was done, while not necessary for the small default number ('''61'''), &nbsp; it was significant for larger numbers.
<lang rexx>/*REXX program calculates Carmichael 3─strong pseudoprimes (up to and including N). */
<syntaxhighlight lang=rexx>/*REXX program calculates Carmichael 3─strong pseudoprimes (up to and including N). */
numeric digits 18 /*handle big dig #s (9 is the default).*/
numeric digits 18 /*handle big dig #s (9 is the default).*/
parse arg N .; if N=='' | N=="," then N=61 /*allow user to specify for the search.*/
parse arg N .; if N=='' | N=="," then N=61 /*allow user to specify for the search.*/
Line 2,836: Line 2,836:
if x//(k+2) ==0 then return 0
if x//(k+2) ==0 then return 0
end /*k*/
end /*k*/
@.x=1; return 1</lang>
@.x=1; return 1</syntaxhighlight>
'''output''' &nbsp; when using the default input:
'''output''' &nbsp; when using the default input:
<pre>
<pre>
Line 2,868: Line 2,868:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
# Project : Carmichael 3 strong pseudoprimes
# Project : Carmichael 3 strong pseudoprimes


Line 2,912: Line 2,912:
next
next
return 1
return 1
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,991: Line 2,991:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{works with|Ruby|1.9}}
{{works with|Ruby|1.9}}
<lang ruby># Generate Charmichael Numbers
<syntaxhighlight lang=ruby># Generate Charmichael Numbers


require 'prime'
require 'prime'
Line 3,008: Line 3,008:
end
end
puts
puts
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 3,100: Line 3,100:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>
<syntaxhighlight lang=rust>
fn is_prime(n: i64) -> bool {
fn is_prime(n: i64) -> bool {
if n > 1 {
if n > 1 {
Line 3,152: Line 3,152:
.count(); // Evaluate entire iterator
.count(); // Evaluate entire iterator
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,171: Line 3,171:
The function [http://seed7.sourceforge.net/algorith/math.htm#isPrime isPrime] below is borrowed from the [http://seed7.sourceforge.net/algorith Seed7 algorithm collection].
The function [http://seed7.sourceforge.net/algorith/math.htm#isPrime isPrime] below is borrowed from the [http://seed7.sourceforge.net/algorith Seed7 algorithm collection].


<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
const func boolean: isPrime (in integer: number) is func
const func boolean: isPrime (in integer: number) is func
Line 3,218: Line 3,218:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 3,295: Line 3,295:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>func forprimes(a, b, callback) {
<syntaxhighlight lang=ruby>func forprimes(a, b, callback) {
for (a = (a-1 -> next_prime); a <= b; a.next_prime!) {
for (a = (a-1 -> next_prime); a <= b; a.next_prime!) {
callback(a)
callback(a)
Line 3,315: Line 3,315:
}
}
}
}
})</lang>
})</syntaxhighlight>


{{out}}
{{out}}
Line 3,334: Line 3,334:
{{trans|Rust}}
{{trans|Rust}}


<lang swift>import Foundation
<syntaxhighlight lang=swift>import Foundation


extension BinaryInteger {
extension BinaryInteger {
Line 3,403: Line 3,403:
for c in res {
for c in res {
print(c)
print(c)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,420: Line 3,420:
=={{header|Tcl}}==
=={{header|Tcl}}==
Using the primality tester from [[Miller-Rabin primality test#Tcl|the Miller-Rabin task]]...
Using the primality tester from [[Miller-Rabin primality test#Tcl|the Miller-Rabin task]]...
<lang tcl>proc carmichael {limit {rounds 10}} {
<syntaxhighlight lang=tcl>proc carmichael {limit {rounds 10}} {
set carmichaels {}
set carmichaels {}
for {set p1 2} {$p1 <= $limit} {incr p1} {
for {set p1 2} {$p1 <= $limit} {incr p1} {
Line 3,442: Line 3,442:
}
}
return $carmichaels
return $carmichaels
}</lang>
}</syntaxhighlight>
Demonstrating:
Demonstrating:
<lang tcl>set results [carmichael 61 2]
<syntaxhighlight lang=tcl>set results [carmichael 61 2]
puts "[expr {[llength $results]/4}] Carmichael numbers found"
puts "[expr {[llength $results]/4}] Carmichael numbers found"
foreach {p1 p2 p3 c} $results {
foreach {p1 p2 p3 c} $results {
puts "$p1 x $p2 x $p3 = $c"
puts "$p1 x $p2 x $p3 = $c"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,525: Line 3,525:
=={{header|Vala}}==
=={{header|Vala}}==
{{trans|D}}
{{trans|D}}
<lang vala>long mod(long n, long m) {
<syntaxhighlight lang=vala>long mod(long n, long m) {
return ((n % m) + m) % m;
return ((n % m) + m) % m;
}
}
Line 3,557: Line 3,557:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,664: Line 3,664:
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
{{libheader|Wren-math}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt
import "/math" for Int
import "/math" for Int


Line 3,693: Line 3,693:
for (p1 in 2..61) {
for (p1 in 2..61) {
if (Int.isPrime(p1)) carmichael.call(p1)
if (Int.isPrime(p1)) carmichael.call(p1)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,774: Line 3,774:
=={{header|zkl}}==
=={{header|zkl}}==
Using the Miller-Rabin primality test in lib GMP.
Using the Miller-Rabin primality test in lib GMP.
<lang zkl>var BN=Import("zklBigNum"), bi=BN(0); // gonna recycle bi
<syntaxhighlight lang=zkl>var BN=Import("zklBigNum"), bi=BN(0); // gonna recycle bi
primes:=T(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61);
primes:=T(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61);
var p2,p3;
var p2,p3;
Line 3,785: Line 3,785:
{ T(p1,p2,p3) } // return list of three primes in Carmichael number
{ T(p1,p2,p3) } // return list of three primes in Carmichael number
]];
]];
fcn mod(a,b) { m:=a%b; if(m<0) m+b else m }</lang>
fcn mod(a,b) { m:=a%b; if(m<0) m+b else m }</syntaxhighlight>
<lang zkl>cs.len().println(" Carmichael numbers found:");
<syntaxhighlight lang=zkl>cs.len().println(" Carmichael numbers found:");
cs.pump(Console.println,fcn([(p1,p2,p3)]){
cs.pump(Console.println,fcn([(p1,p2,p3)]){
"%2d * %4d * %5d = %d".fmt(p1,p2,p3,p1*p2*p3) });</lang>
"%2d * %4d * %5d = %d".fmt(p1,p2,p3,p1*p2*p3) });</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,808: Line 3,808:
=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
{{trans|C}}
{{trans|C}}
<lang zxbasic>10 FOR p=2 TO 61
<syntaxhighlight lang=zxbasic>10 FOR p=2 TO 61
20 LET n=p: GO SUB 1000
20 LET n=p: GO SUB 1000
30 IF NOT n THEN GO TO 200
30 IF NOT n THEN GO TO 200
Line 3,834: Line 3,834:
2000 DEF FN m(a,b)=a-(INT (a/b)*b): REM Mod function
2000 DEF FN m(a,b)=a-(INT (a/b)*b): REM Mod function
2010 DEF FN w(a,b)=FN m(FN m(a,b)+b,b): REM Mod function modified
2010 DEF FN w(a,b)=FN m(FN m(a,b)+b,b): REM Mod function modified
</syntaxhighlight>
</lang>