Circular primes: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 30: | Line 30: | ||
* [[wp:Repunit|Wikipedia article - Repunit]]. |
* [[wp:Repunit|Wikipedia article - Repunit]]. |
||
* [[oeis:A016114|OEIS sequence A016114 - Circular primes]]. |
* [[oeis:A016114|OEIS sequence A016114 - Circular primes]]. |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<syntaxhighlight 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 103: | Line 101: | ||
First 19 circular primes: 2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
First 19 circular primes: 2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
<syntaxhighlight 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 179: | Line 176: | ||
First 19 circular primes: 2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
First 19 circular primes: 2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight 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 238: | Line 234: | ||
193939 |
193939 |
||
199933</pre> |
199933</pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="awk"> |
||
# syntax: GAWK -f CIRCULAR_PRIMES.AWK |
# syntax: GAWK -f CIRCULAR_PRIMES.AWK |
||
BEGIN { |
BEGIN { |
||
Line 293: | Line 287: | ||
first 19 circular primes: 2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
first 19 circular primes: 2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
{{libheader|GMP}} |
{{libheader|GMP}} |
||
<syntaxhighlight lang=c>#include <stdbool.h> |
<syntaxhighlight lang="c">#include <stdbool.h> |
||
#include <stdint.h> |
#include <stdint.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 409: | Line 402: | ||
R(49081) is probably prime. |
R(49081) is probably prime. |
||
</pre> |
</pre> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{libheader|GMP}} |
{{libheader|GMP}} |
||
<syntaxhighlight lang=cpp>#include <cstdint> |
<syntaxhighlight lang="cpp">#include <cstdint> |
||
#include <algorithm> |
#include <algorithm> |
||
#include <iostream> |
#include <iostream> |
||
Line 511: | Line 503: | ||
R(49081) is probably prime |
R(49081) is probably prime |
||
</pre> |
</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="d">import std.bigint; |
||
import std.stdio; |
import std.stdio; |
||
Line 614: | Line 605: | ||
<pre>First 19 circular primes: |
<pre>First 19 circular primes: |
||
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</pre> |
||
=={{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#)] |
||
<syntaxhighlight 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 630: | Line 620: | ||
The first 5 repunit primes are R(2) R(19) R(23) R(317) R(1031) |
The first 5 repunit primes are R(2) R(19) R(23) R(317) R(1031) |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
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}} |
||
<syntaxhighlight 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 671: | Line 660: | ||
R(19) R(23) R(317) R(1031) |
R(19) R(23) R(317) R(1031) |
||
</pre> |
</pre> |
||
=={{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. |
||
<syntaxhighlight lang= |
<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 737: | Line 725: | ||
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight 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 775: | Line 762: | ||
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{libheader|GMP(Go wrapper)}} |
{{libheader|GMP(Go wrapper)}} |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 921: | Line 907: | ||
=={{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 |
||
<syntaxhighlight 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 992: | Line 978: | ||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="j"> |
||
R=: [: ". 'x' ,~ #&'1' |
R=: [: ". 'x' ,~ #&'1' |
||
Line 1,116: | Line 1,102: | ||
NB. R(2) R(19), R(23) are probably prime. |
NB. R(2) R(19), R(23) are probably prime. |
||
</pre> |
</pre> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang=java>import java.math.BigInteger; |
<syntaxhighlight lang="java">import java.math.BigInteger; |
||
import java.util.Arrays; |
import java.util.Arrays; |
||
Line 1,221: | Line 1,206: | ||
R(25031) is not prime. |
R(25031) is not prime. |
||
</pre> |
</pre> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq}} |
{{works with|jq}} |
||
Line 1,231: | Line 1,215: | ||
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> |
<syntaxhighlight 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,246: | Line 1,230: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
'''The first task''' |
'''The first task''' |
||
<syntaxhighlight lang=jq>limit(19; circular_primes)</syntaxhighlight> |
<syntaxhighlight lang="jq">limit(19; circular_primes)</syntaxhighlight> |
||
'''The second task''' (viewed independently): |
'''The second task''' (viewed independently): |
||
<syntaxhighlight lang=jq> |
<syntaxhighlight 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)) |
||
Line 1,275: | Line 1,259: | ||
199933 |
199933 |
||
</pre> |
</pre> |
||
=={{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 |
||
<syntaxhighlight lang=julia>using Lazy, Primes |
<syntaxhighlight lang="julia">using Lazy, Primes |
||
function iscircularprime(n) |
function iscircularprime(n) |
||
Line 1,314: | Line 1,296: | ||
R(49081) is prime. |
R(49081) is prime. |
||
</pre> |
</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=scala>import java.math.BigInteger |
<syntaxhighlight lang="scala">import java.math.BigInteger |
||
val SMALL_PRIMES = listOf( |
val SMALL_PRIMES = listOf( |
||
Line 1,445: | Line 1,426: | ||
R(25031) is not prime. |
R(25031) is not prime. |
||
R(35317) is not prime.</pre> |
R(35317) is not prime.</pre> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<syntaxhighlight 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,477: | Line 1,456: | ||
{{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}}== |
||
<syntaxhighlight lang= |
<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,515: | Line 1,493: | ||
False |
False |
||
True</pre> |
True</pre> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|bignum}} |
{{libheader|bignum}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import bignum |
||
import strformat |
import strformat |
||
Line 1,644: | Line 1,621: | ||
R(35317) is not prime. |
R(35317) is not prime. |
||
R(49081) is probably prime.</pre> |
R(49081) is probably prime.</pre> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
==={{header|Free Pascal}}=== |
==={{header|Free Pascal}}=== |
||
Line 1,652: | Line 1,628: | ||
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. |
||
<syntaxhighlight lang=pascal> |
<syntaxhighlight lang="pascal"> |
||
program CircularPrimes; |
program CircularPrimes; |
||
//nearly the way it is done: |
//nearly the way it is done: |
||
Line 1,845: | Line 1,821: | ||
14 4 247209700 0 8842 |
14 4 247209700 0 8842 |
||
that reduces the count of gmp-prime tests to 1/6 </pre> |
that reduces the count of gmp-prime tests to 1/6 </pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
<syntaxhighlight lang=perl>use strict; |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,901: | Line 1,876: | ||
R(35317): Prime? False |
R(35317): Prime? False |
||
R(49081): Prime? True</pre> |
R(49081): Prime? True</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<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,953: | Line 1,927: | ||
===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> |
||
<!--<syntaxhighlight lang= |
<!--<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,985: | Line 1,959: | ||
diag looping, error code is 1, era is #00644651 |
diag looping, error code is 1, era is #00644651 |
||
</pre> |
</pre> |
||
=={{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. |
||
<syntaxhighlight lang= |
<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,060: | Line 2,033: | ||
(2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 (R 19) (R 23) (R 317) (R 1031)) |
(2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 (R 19) (R 23) (R 317) (R 1031)) |
||
</pre> |
</pre> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Uses a miller-rabin primality tester that I wrote which includes a trial division pass for small prime factors. One could substitute with e.g. the Miller Rabin Primaliy Test task. |
Uses a miller-rabin primality tester that I wrote which includes a trial division pass for small prime factors. One could substitute with e.g. the Miller Rabin Primaliy Test task. |
||
Line 2,067: | Line 2,039: | ||
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. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="prolog"> |
||
?- use_module(library(primality)). |
?- use_module(library(primality)). |
||
Line 2,114: | Line 2,086: | ||
[2,3,5,7,11,13,17,37,79,113,197,199,337,1193,3779,11939,19937,193939,199933,r(19),r(23),r(317),r(1031)] |
[2,3,5,7,11,13,17,37,79,113,197,199,337,1193,3779,11939,19937,193939,199933,r(19),r(23),r(317),r(1031)] |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="python"> |
||
import random |
import random |
||
Line 2,230: | Line 2,201: | ||
# 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> |
</syntaxhighlight> |
||
=={{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}} |
||
<syntaxhighlight lang=raku line>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,277: | Line 2,247: | ||
R(35317): Prime? False 0.32 |
R(35317): Prime? False 0.32 |
||
R(49081): Prime? True 921.73</pre> |
R(49081): Prime? True 921.73</pre> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<syntaxhighlight 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,322: | Line 2,291: | ||
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight 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,379: | Line 2,347: | ||
done... |
done... |
||
</pre> |
</pre> |
||
=={{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. |
||
<syntaxhighlight 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,426: | Line 2,393: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=rust>// [dependencies] |
<syntaxhighlight lang="rust">// [dependencies] |
||
// rug = "1.8" |
// rug = "1.8" |
||
Line 2,547: | Line 2,514: | ||
R(49081) is probably prime. |
R(49081) is probably prime. |
||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight 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,667: | Line 2,633: | ||
R(15073) is not prime. |
R(15073) is not prime. |
||
R(25031) is not prime.</pre> |
R(25031) is not prime.</pre> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
<syntaxhighlight 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,717: | Line 2,682: | ||
R(35317) -> composite (took: 0.875 sec) |
R(35317) -> composite (took: 0.875 sec) |
||
</pre> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
Line 2,726: | Line 2,690: | ||
===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. |
||
<syntaxhighlight 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,806: | Line 2,770: | ||
{{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. |
||
<syntaxhighlight lang=ecmascript>/* circular_primes_embedded.wren */ |
<syntaxhighlight lang="ecmascript">/* circular_primes_embedded.wren */ |
||
import "./gmp" for Mpz |
import "./gmp" for Mpz |
||
Line 2,899: | Line 2,863: | ||
R(49081) : true |
R(49081) : true |
||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<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,949: | Line 2,912: | ||
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
2 3 5 7 11 13 17 37 79 113 197 199 337 1193 3779 11939 19937 193939 199933 |
||
</pre> |
</pre> |
||
=={{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. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="zig"> |
||
const std = @import("std"); |
const std = @import("std"); |
||
const math = std.math; |
const math = std.math; |