Circular primes: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 33: Line 33:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find circular primes - primes where all cyclic permutations #
<syntaxhighlight lang=algol68>BEGIN # find circular primes - primes where all cyclic permutations #
# of the digits are also prime #
# of the digits are also prime #
# genertes a sieve of circular primes, only the first #
# genertes a sieve of circular primes, only the first #
Line 98: Line 98:
OD;
OD;
print( ( newline ) )
print( ( newline ) )
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 105: Line 105:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % find circular primes - primes where all cyclic permutations %
<syntaxhighlight lang=algolw>begin % find circular primes - primes where all cyclic permutations %
% of the digits are also prime %
% of the digits are also prime %
% sets p( 1 :: n ) to a sieve of primes up to n %
% sets p( 1 :: n ) to a sieve of primes up to n %
Line 174: Line 174:
end for_i ;
end for_i ;
end_circular:
end_circular:
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 182: Line 182:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>perms: function [n][
<syntaxhighlight lang=rebol>perms: function [n][
str: repeat to :string n 2
str: repeat to :string n 2
result: new []
result: new []
Line 215: Line 215:
]
]
i: i + 1
i: i + 1
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 241: Line 241:


=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<syntaxhighlight lang=AWK>
# syntax: GAWK -f CIRCULAR_PRIMES.AWK
# syntax: GAWK -f CIRCULAR_PRIMES.AWK
BEGIN {
BEGIN {
Line 288: Line 288:
return(1)
return(1)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 296: Line 296:
=={{header|C}}==
=={{header|C}}==
{{libheader|GMP}}
{{libheader|GMP}}
<lang c>#include <stdbool.h>
<syntaxhighlight lang=c>#include <stdbool.h>
#include <stdint.h>
#include <stdint.h>
#include <stdio.h>
#include <stdio.h>
Line 393: Line 393:
test_repunit(49081);
test_repunit(49081);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 412: Line 412:
=={{header|C++}}==
=={{header|C++}}==
{{libheader|GMP}}
{{libheader|GMP}}
<lang cpp>#include <cstdint>
<syntaxhighlight lang=cpp>#include <cstdint>
#include <algorithm>
#include <algorithm>
#include <iostream>
#include <iostream>
Line 496: Line 496:
test_repunit(49081);
test_repunit(49081);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 514: Line 514:
=={{header|D}}==
=={{header|D}}==
{{trans|C}}
{{trans|C}}
<lang D>import std.bigint;
<syntaxhighlight lang=D>import std.bigint;
import std.stdio;
import std.stdio;


Line 610: Line 610:
}
}
writeln;
writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 19 circular primes:
<pre>First 19 circular primes:
Line 617: Line 617:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Repunit_primes#F.23 rUnitP] and [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
This task uses [http://www.rosettacode.org/wiki/Repunit_primes#F.23 rUnitP] and [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<lang fsharp>
<syntaxhighlight lang=fsharp>
// Circular primes - Nigel Galloway: September 13th., 2021
// Circular primes - Nigel Galloway: September 13th., 2021
let fG n g=let rec fG y=if y=g then true else if y>g && isPrime y then fG(10*(y%n)+y/n) else false in fG(10*(g%n)+g/n)
let fG n g=let rec fG y=if y=g then true else if y>g && isPrime y then fG(10*(y%n)+y/n) else false in fG(10*(g%n)+g/n)
Line 624: Line 624:
circP()|> Seq.take 19 |>Seq.iter(printf "%d "); printfn ""
circP()|> Seq.take 19 |>Seq.iter(printf "%d "); printfn ""
printf "The first 5 repunit primes are "; rUnitP(10)|>Seq.take 5|>Seq.iter(fun n->printf $"R(%d{n}) "); printfn ""
printf "The first 5 repunit primes are "; rUnitP(10)|>Seq.take 5|>Seq.iter(fun n->printf $"R(%d{n}) "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 634: Line 634:
Unfortunately Factor's miller-rabin test or bignums aren't quite up to the task of finding the next four circular prime repunits in a reasonable time. It takes ~90 seconds to check R(7)-R(1031).
Unfortunately Factor's miller-rabin test or bignums aren't quite up to the task of finding the next four circular prime repunits in a reasonable time. It takes ~90 seconds to check R(7)-R(1031).
{{works with|Factor|0.99 2020-03-02}}
{{works with|Factor|0.99 2020-03-02}}
<lang factor>USING: combinators.short-circuit formatting io kernel lists
<syntaxhighlight lang=factor>USING: combinators.short-circuit formatting io kernel lists
lists.lazy math math.combinatorics math.functions math.parser
lists.lazy math math.combinatorics math.functions math.parser
math.primes sequences sequences.extras ;
math.primes sequences sequences.extras ;
Line 662: Line 662:


"The next 4 circular primes, in repunit format, are:" print
"The next 4 circular primes, in repunit format, are:" print
4 prime-repunits ltake [ "R(%d) " printf ] leach nl</lang>
4 prime-repunits ltake [ "R(%d) " printf ] leach nl</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 674: Line 674:
=={{header|Forth}}==
=={{header|Forth}}==
Forth only supports native sized integers, so we only implement the first part of the task.
Forth only supports native sized integers, so we only implement the first part of the task.
<lang Forth>
<syntaxhighlight lang=Forth>
create 235-wheel 6 c, 4 c, 2 c, 4 c, 2 c, 4 c, 6 c, 2 c,
create 235-wheel 6 c, 4 c, 2 c, 4 c, 2 c, 4 c, 6 c, 2 c,
does> swap 7 and + c@ ;
does> swap 7 and + c@ ;
Line 731: Line 731:
." The first 19 circular primes are:" cr .primes cr
." The first 19 circular primes are:" cr .primes cr
bye
bye
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 739: Line 739:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#define floor(x) ((x*2.0-0.5)Shr 1)
<syntaxhighlight lang=freebasic>#define floor(x) ((x*2.0-0.5)Shr 1)


Function isPrime(Byval p As Integer) As Boolean
Function isPrime(Byval p As Integer) As Boolean
Line 769: Line 769:
p += dp: dp = 2
p += dp: dp = 2
Wend
Wend
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 778: Line 778:
=={{header|Go}}==
=={{header|Go}}==
{{libheader|GMP(Go wrapper)}}
{{libheader|GMP(Go wrapper)}}
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 901: Line 901:
fmt.Printf("R(%-5d) : %t\n", i, repunit(i).ProbablyPrime(10))
fmt.Printf("R(%-5d) : %t\n", i, repunit(i).ProbablyPrime(10))
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 921: Line 921:
=={{header|Haskell}}==
=={{header|Haskell}}==
Uses arithmoi Library: http://hackage.haskell.org/package/arithmoi-0.10.0.0
Uses arithmoi Library: http://hackage.haskell.org/package/arithmoi-0.10.0.0
<lang haskell>import Math.NumberTheory.Primes (Prime, unPrime, nextPrime)
<syntaxhighlight lang=haskell>import Math.NumberTheory.Primes (Prime, unPrime, nextPrime)
import Math.NumberTheory.Primes.Testing (isPrime, millerRabinV)
import Math.NumberTheory.Primes.Testing (isPrime, millerRabinV)
import Text.Printf (printf)
import Text.Printf (printf)
Line 973: Line 973:
circular = show . take 19 . filter (isCircular False)
circular = show . take 19 . filter (isCircular False)
reps = map (sum . digits). tail . take 5 . filter (isCircular True)
reps = map (sum . digits). tail . take 5 . filter (isCircular True)
checkReps = (,) <$> id <*> show . isCircular True . asRepunit</lang>
checkReps = (,) <$> id <*> show . isCircular True . asRepunit</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 992: Line 992:
</pre>
</pre>
=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang=J>
<lang J>


R=: [: ". 'x' ,~ #&'1'
R=: [: ". 'x' ,~ #&'1'
Line 1,028: Line 1,028:
group </. P
group </. P
)
)
</syntaxhighlight>
</lang>
J lends itself to investigation. Demonstrate and play with the definitions.
J lends itself to investigation. Demonstrate and play with the definitions.
<pre>
<pre>
Line 1,118: Line 1,118:


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.math.BigInteger;
<syntaxhighlight lang=java>import java.math.BigInteger;
import java.util.Arrays;
import java.util.Arrays;


Line 1,208: Line 1,208:
return new BigInteger(new String(ch));
return new BigInteger(new String(ch));
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,231: Line 1,231:


For an implementation of `is_prime` suitable for the first task, see for example [[Erd%C5%91s-primes#jq]].
For an implementation of `is_prime` suitable for the first task, see for example [[Erd%C5%91s-primes#jq]].
<syntaxhighlight lang=jq>
<lang jq>
def is_circular_prime:
def is_circular_prime:
def circle: range(0;length) as $i | .[$i:] + .[:$i];
def circle: range(0;length) as $i | .[$i:] + .[:$i];
Line 1,244: Line 1,244:
def repunits:
def repunits:
1 | recurse(10*. + 1);
1 | recurse(10*. + 1);
</syntaxhighlight>
</lang>
'''The first task'''
'''The first task'''
<lang jq>limit(19; circular_primes)</lang>
<syntaxhighlight lang=jq>limit(19; circular_primes)</syntaxhighlight>
'''The second task''' (viewed independently):
'''The second task''' (viewed independently):
<syntaxhighlight lang=jq>
<lang jq>
last(limit(19; circular_primes)) as $max
last(limit(19; circular_primes)) as $max
| limit(4; repunits | select(. > $max and is_prime))
| limit(4; repunits | select(. > $max and is_prime))
| "R(\(tostring|length))"</lang>
| "R(\(tostring|length))"</syntaxhighlight>
{{out}}
{{out}}
For the first task:
For the first task:
Line 1,279: Line 1,279:
=={{header|Julia}}==
=={{header|Julia}}==
Note that the evalpoly function used in this program was added in Julia 1.4
Note that the evalpoly function used in this program was added in Julia 1.4
<lang julia>using Lazy, Primes
<syntaxhighlight lang=julia>using Lazy, Primes


function iscircularprime(n)
function iscircularprime(n)
Line 1,299: Line 1,299:
println("R($i) is ", isprimerepunit(i) ? "prime." : "not prime.")
println("R($i) is ", isprimerepunit(i) ? "prime." : "not prime.")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The first 19 circular primes are:
The first 19 circular primes are:
Line 1,317: Line 1,317:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|C}}
{{trans|C}}
<lang scala>import java.math.BigInteger
<syntaxhighlight lang=scala>import java.math.BigInteger


val SMALL_PRIMES = listOf(
val SMALL_PRIMES = listOf(
Line 1,434: Line 1,434:
testRepUnit(35317)
testRepUnit(35317)
testRepUnit(49081)
testRepUnit(49081)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 19 circular primes:
<pre>First 19 circular primes:
Line 1,448: Line 1,448:


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>-- Circular primes, in Lua, 6/22/2020 db
<syntaxhighlight lang=lua>-- Circular primes, in Lua, 6/22/2020 db
local function isprime(n)
local function isprime(n)
if n < 2 then return false end
if n < 2 then return false end
Line 1,474: Line 1,474:
p, dp = p + dp, 2
p, dp = p + dp, 2
end
end
print(table.concat(list, ", "))</lang>
print(table.concat(list, ", "))</syntaxhighlight>
{{out}}
{{out}}
<pre>2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933</pre>
<pre>2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933</pre>


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>ClearAll[RepUnit, CircularPrimeQ]
<syntaxhighlight lang=Mathematica>ClearAll[RepUnit, CircularPrimeQ]
RepUnit[n_] := (10^n - 1)/9
RepUnit[n_] := (10^n - 1)/9
CircularPrimeQ[n_Integer] := Module[{id = IntegerDigits[n], nums, t},
CircularPrimeQ[n_Integer] := Module[{id = IntegerDigits[n], nums, t},
Line 1,505: Line 1,505:
]
]


Scan[Print@*PrimeQ@*RepUnit, {5003, 9887, 15073, 25031, 35317, 49081}]</lang>
Scan[Print@*PrimeQ@*RepUnit, {5003, 9887, 15073, 25031, 35317, 49081}]</syntaxhighlight>
{{out}}
{{out}}
<pre>{2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933}
<pre>{2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933}
Line 1,519: Line 1,519:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|bignum}}
{{libheader|bignum}}
<lang Nim>import bignum
<syntaxhighlight lang=Nim>import bignum
import strformat
import strformat


Line 1,629: Line 1,629:
testRepUnit(25031)
testRepUnit(25031)
testRepUnit(35317)
testRepUnit(35317)
testRepUnit(49081)</lang>
testRepUnit(49081)</syntaxhighlight>


{{out}}
{{out}}
Line 1,652: Line 1,652:
199-> 333 and not 311 so a base4 counter 1_(1,3,7,9) changed to base3 3_( 3,7,9 )->base2 7_(7,9) -> base 1 = 99....99.
199-> 333 and not 311 so a base4 counter 1_(1,3,7,9) changed to base3 3_( 3,7,9 )->base2 7_(7,9) -> base 1 = 99....99.
The main speed up is reached by testing with small primes within CycleNum.
The main speed up is reached by testing with small primes within CycleNum.
<lang pascal>
<syntaxhighlight lang=pascal>
program CircularPrimes;
program CircularPrimes;
//nearly the way it is done:
//nearly the way it is done:
Line 1,819: Line 1,819:
{$ENDIF}
{$ENDIF}
end.
end.
</syntaxhighlight>
</lang>
{{Out|@ Tio.Run}}
{{Out|@ Tio.Run}}
<pre>
<pre>
Line 1,849: Line 1,849:
{{trans|Raku}}
{{trans|Raku}}
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,883: Line 1,883:
for (5003, 9887, 15073, 25031, 35317, 49081) {
for (5003, 9887, 15073, 25031, 35317, 49081) {
say "R($_): Prime? " . (is_prime 1 x $_ ? 'True' : 'False');
say "R($_): Prime? " . (is_prime 1 x $_ ? 'True' : 'False');
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 19 circular primes are:
<pre>The first 19 circular primes are:
Line 1,903: Line 1,903:


=={{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: #008080;">function</span> <span style="color: #000000;">circular</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">circular</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
Line 1,941: Line 1,941:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</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;">"The next 4 circular primes, in repunit format, are:\n%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</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;">"The next 4 circular primes, in repunit format, are:\n%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,953: Line 1,953:
===stretch===
===stretch===
<small>(It is probably quite unwise to throw this in the direction of pwa/p2js, I am not even going to bother trying.)</small>
<small>(It is probably quite unwise to throw this in the direction of pwa/p2js, I am not even going to bother trying.)</small>
<!--<lang Phix>-->
<!--<syntaxhighlight lang=Phix>-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5003</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9887</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15073</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25031</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">35317</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49081</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5003</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9887</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">15073</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25031</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">35317</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">49081</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;">"The following repunits are probably circular primes:\n"</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;">"The following repunits are probably circular primes:\n"</span><span style="color: #0000FF;">)</span>
Line 1,963: Line 1,963:
<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;">"R(%d) : %t (%s)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bPrime</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</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;">"R(%d) : %t (%s)\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bPrime</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
64-bit can only cope with the first five (it terminates abruptly on the sixth)<br>
64-bit can only cope with the first five (it terminates abruptly on the sixth)<br>
Line 1,988: Line 1,988:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
For small primes, I only check numbers that are made up of the digits 1, 3, 7, and 9, and I also use a small prime checker to avoid the overhead of the Miller-Rabin Primality Test. For the large repunits, one can use the code from the Miller Rabin task.
For small primes, I only check numbers that are made up of the digits 1, 3, 7, and 9, and I also use a small prime checker to avoid the overhead of the Miller-Rabin Primality Test. For the large repunits, one can use the code from the Miller Rabin task.
<lang PicoLisp>
<syntaxhighlight lang=PicoLisp>
(load "plcommon/primality.l") # see task: "Miller-Rabin Primality Test"
(load "plcommon/primality.l") # see task: "Miller-Rabin Primality Test"


Line 2,054: Line 2,054:
(println Circular)
(println Circular)
(bye)
(bye)
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,067: Line 2,067:


Also the code is smart in that it only checks primes > 9 that are composed of the digits 1, 3, 7, and 9.
Also the code is smart in that it only checks primes > 9 that are composed of the digits 1, 3, 7, and 9.
<lang Prolog>
<syntaxhighlight lang=Prolog>
?- use_module(library(primality)).
?- use_module(library(primality)).


Line 2,108: Line 2,108:


?- main.
?- main.
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,116: Line 2,116:


=={{header|Python}}==
=={{header|Python}}==
<lang Python>
<syntaxhighlight lang=Python>
import random
import random


Line 2,229: Line 2,229:


# because this Miller-Rabin test is already on rosettacode there's no good reason to test the longer repunits.
# because this Miller-Rabin test is already on rosettacode there's no good reason to test the longer repunits.
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Most of the repunit testing is relatively speedy using the ntheory library. The really slow ones are R(25031), at ~42 seconds and R(49081) at 922(!!) seconds.
Most of the repunit testing is relatively speedy using the ntheory library. The really slow ones are R(25031), at ~42 seconds and R(49081) at 922(!!) seconds.
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl6>sub isCircular(\n) {
<syntaxhighlight lang=raku line>sub isCircular(\n) {
return False unless n.is-prime;
return False unless n.is-prime;
my @circular = n.comb;
my @circular = n.comb;
Line 2,259: Line 2,259:
my $now = now;
my $now = now;
say "R($_): Prime? ", ?is_prime("{1 x $_}"), " {(now - $now).fmt: '%.2f'}"
say "R($_): Prime? ", ?is_prime("{1 x $_}"), " {(now - $now).fmt: '%.2f'}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 19 circular primes are:
<pre>The first 19 circular primes are:
Line 2,279: Line 2,279:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds & displays circular primes (with a title & in a horizontal format).*/
<syntaxhighlight lang=rexx>/*REXX program finds & displays circular primes (with a title & in a horizontal format).*/
parse arg N hp . /*obtain optional arguments from the CL*/
parse arg N hp . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 19 /* " " " " " " */
if N=='' | N=="," then N= 19 /* " " " " " " */
Line 2,315: Line 2,315:
end /*k*/ /* [↓] a prime (J) has been found. */
end /*k*/ /* [↓] a prime (J) has been found. */
#= #+1; !.j= 1; sq.#= j*j; @.#= j /*bump P cnt; assign P to @. and !. */
#= #+1; !.j= 1; sq.#= j*j; @.#= j /*bump P cnt; assign P to @. and !. */
end /*j*/; return</lang>
end /*j*/; return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,324: Line 2,324:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
see "working..." + nl
see "working..." + nl
see "First 19 circular numbers are:" + nl
see "First 19 circular numbers are:" + nl
Line 2,368: Line 2,368:
next
next
return 1
return 1
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,382: Line 2,382:
=={{header|Ruby}}==
=={{header|Ruby}}==
It takes more then 25 minutes to verify that R49081 is probably prime - omitted here.
It takes more then 25 minutes to verify that R49081 is probably prime - omitted here.
<lang ruby>require 'gmp'
<syntaxhighlight lang=ruby>require 'gmp'
require 'prime'
require 'prime'
candidate_primes = Enumerator.new do |y|
candidate_primes = Enumerator.new do |y|
Line 2,411: Line 2,411:
puts reps.map{|r| "R" + r.to_s}.join(", "), ""
puts reps.map{|r| "R" + r.to_s}.join(", "), ""
[5003, 9887, 15073, 25031].each {|rep| puts "R#{rep} circular_prime ? #{circular?("1"*rep)}" }
[5003, 9887, 15073, 25031].each {|rep| puts "R#{rep} circular_prime ? #{circular?("1"*rep)}" }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>First 19 circular primes:
<pre>First 19 circular primes:
Line 2,426: Line 2,426:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|C}}
{{trans|C}}
<lang rust>// [dependencies]
<syntaxhighlight lang=rust>// [dependencies]
// rug = "1.8"
// rug = "1.8"


Line 2,531: Line 2,531:
test_repunit(35317);
test_repunit(35317);
test_repunit(49081);
test_repunit(49081);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,550: Line 2,550:
=={{header|Scala}}==
=={{header|Scala}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>object CircularPrimes {
<syntaxhighlight lang=scala>object CircularPrimes {
def main(args: Array[String]): Unit = {
def main(args: Array[String]): Unit = {
println("First 19 circular primes:")
println("First 19 circular primes:")
Line 2,657: Line 2,657:
BigInt.apply(new String(ch))
BigInt.apply(new String(ch))
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 19 circular primes:
<pre>First 19 circular primes:
Line 2,670: Line 2,670:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>func is_circular_prime(n) {
<syntaxhighlight lang=ruby>func is_circular_prime(n) {
n.is_prime || return false
n.is_prime || return false


Line 2,698: Line 2,698:
say ("R(#{n}) -> ", is_prob_prime((10**n - 1)/9) ? 'probably prime' : 'composite',
say ("R(#{n}) -> ", is_prob_prime((10**n - 1)/9) ? 'probably prime' : 'composite',
" (took: #{'%.3f' % Time.micro-now} sec)")
" (took: #{'%.3f' % Time.micro-now} sec)")
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,726: Line 2,726:
===Wren-cli===
===Wren-cli===
Second part is very slow - over 37 minutes to find all four.
Second part is very slow - over 37 minutes to find all four.
<lang ecmascript>import "/math" for Int
<syntaxhighlight lang=ecmascript>import "/math" for Int
import "/big" for BigInt
import "/big" for BigInt
import "/str" for Str
import "/str" for Str
Line 2,792: Line 2,792:
}
}
}
}
System.print(rus)</lang>
System.print(rus)</syntaxhighlight>


{{out}}
{{out}}
Line 2,806: Line 2,806:
{{libheader|Wren-gmp}}
{{libheader|Wren-gmp}}
A massive speed-up, of course, when GMP is plugged in for the 'probably prime' calculations. 11 minutes 19 seconds including the stretch goal.
A massive speed-up, of course, when GMP is plugged in for the 'probably prime' calculations. 11 minutes 19 seconds including the stretch goal.
<lang ecmascript>/* circular_primes_embedded.wren */
<syntaxhighlight lang=ecmascript>/* circular_primes_embedded.wren */
import "./gmp" for Mpz
import "./gmp" for Mpz
Line 2,880: Line 2,880:
repunit.setStr(Str.repeat("1", i), 10)
repunit.setStr(Str.repeat("1", i), 10)
Fmt.print("R($-5d) : $s", i, repunit.probPrime(15) > 0)
Fmt.print("R($-5d) : $s", i, repunit.probPrime(15) > 0)
}</lang>
}</syntaxhighlight>
<br>
<br>


Line 2,901: Line 2,901:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func IsPrime(N); \Return 'true' if N > 2 is a prime number
<syntaxhighlight lang=XPL0>func IsPrime(N); \Return 'true' if N > 2 is a prime number
int N, I;
int N, I;
[if (N&1) = 0 \even number\ then return false;
[if (N&1) = 0 \even number\ then return false;
Line 2,943: Line 2,943:
N:= N+2;
N:= N+2;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,952: Line 2,952:
=={{header|Zig}}==
=={{header|Zig}}==
As of now (Zig release 0.8.1), Zig has large integer support, but there is not yet a prime test in the standard library. Therefore, we only find the circular primes < 1e6. As with the Prolog version, we only check numbers composed of 1, 3, 7, or 9.
As of now (Zig release 0.8.1), Zig has large integer support, but there is not yet a prime test in the standard library. Therefore, we only find the circular primes < 1e6. As with the Prolog version, we only check numbers composed of 1, 3, 7, or 9.
<lang Zig>
<syntaxhighlight lang=Zig>
const std = @import("std");
const std = @import("std");
const math = std.math;
const math = std.math;
Line 3,037: Line 3,037:
} else true;
} else true;
}
}
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>