Carmichael 3 strong pseudoprimes: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 36: | Line 36: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<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)</ |
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]]. |
||
< |
<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;</ |
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). |
||
< |
<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</ |
OD</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 223: | Line 223: | ||
=={{header|AWK}}== |
=={{header|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}} |
||
< |
<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++}}== |
||
< |
<syntaxhighlight lang=cpp>#include <iomanip> |
||
#include <iostream> |
#include <iostream> |
||
Line 509: | Line 509: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 585: | Line 585: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<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}}== |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>3 x 11 x 17 |
<pre>3 x 11 x 17 |
||
Line 795: | Line 795: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<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}} |
||
< |
<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#)] |
||
< |
<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. |
||
< |
<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</ |
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... < |
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</ |
END</syntaxhighlight> |
||
===Output=== |
===Output=== |
||
Line 1,141: | Line 1,141: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<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</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 3 * 11 * 17 |
<pre> 3 * 11 * 17 |
||
Line 1,272: | Line 1,272: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<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) } |
||
} |
} |
||
}</ |
}</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}} |
||
< |
<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</ |
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. |
||
< |
<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</ |
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}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</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''' |
||
< |
<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</ |
end</syntaxhighlight> |
||
'''Main''' |
'''Main''' |
||
< |
<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.")</ |
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}} |
||
< |
<syntaxhighlight lang=scala>fun Int.isPrime(): Boolean { |
||
return when { |
return when { |
||
this == 2 -> true |
this == 2 -> true |
||
Line 1,881: | Line 1,881: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
See D output. |
See D output. |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<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.")</ |
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}}== |
||
< |
<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]]</ |
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 |
||
< |
<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}"</ |
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}}== |
||
< |
<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]", ")))</ |
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}} |
||
< |
<syntaxhighlight lang=perl>use ntheory qw/forprimes is_prime vecprod/; |
||
forprimes { my $p = $_; |
forprimes { my $p = $_; |
||
Line 2,149: | Line 2,149: | ||
} |
} |
||
} |
} |
||
} 3,61;</ |
} 3,61;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,164: | Line 2,164: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,206: | Line 2,206: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>(de modulo (X Y) |
||
(% (+ Y (% X Y)) Y) ) |
(% (+ Y (% X Y)) Y) ) |
||
Line 2,245: | Line 2,245: | ||
(prinl) |
(prinl) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<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;</ |
end Carmichael;</syntaxhighlight> |
||
Results: |
Results: |
||
<pre> |
<pre> |
||
Line 2,373: | Line 2,373: | ||
=={{header|Prolog}}== |
=={{header|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}}== |
||
< |
<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)))</ |
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}}== |
||
< |
<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: |
||
< |
<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 |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</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'''), it was significant for larger numbers. |
Some code optimization was done, while not necessary for the small default number ('''61'''), it was significant for larger numbers. |
||
< |
<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</ |
@.x=1; return 1</syntaxhighlight> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 2,868: | Line 2,868: | ||
=={{header|Ring}}== |
=={{header|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}} |
||
< |
<syntaxhighlight lang=ruby># Generate Charmichael Numbers |
||
require 'prime' |
require 'prime' |
||
Line 3,008: | Line 3,008: | ||
end |
end |
||
puts |
puts |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,100: | Line 3,100: | ||
=={{header|Rust}}== |
=={{header|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]. |
||
< |
<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;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,295: | Line 3,295: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
})</ |
})</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,334: | Line 3,334: | ||
{{trans|Rust}} |
{{trans|Rust}} |
||
< |
<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) |
||
}</ |
}</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]]... |
||
< |
<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 |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
< |
<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" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,525: | Line 3,525: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<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: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,664: | Line 3,664: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<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) |
||
}</ |
}</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. |
||
< |
<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 }</ |
fcn mod(a,b) { m:=a%b; if(m<0) m+b else m }</syntaxhighlight> |
||
< |
<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) });</ |
"%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}} |
||
< |
<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> |