Circular primes: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 33:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang=algol68>BEGIN # find circular primes - primes where all cyclic permutations #
# of the digits are also prime #
# genertes a sieve of circular primes, only the first #
Line 98:
OD;
print( ( newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 105:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang=algolw>begin % find circular primes - primes where all cyclic permutations %
% of the digits are also prime %
% sets p( 1 :: n ) to a sieve of primes up to n %
Line 174:
end for_i ;
end_circular:
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 182:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>perms: function [n][
str: repeat to :string n 2
result: new []
Line 215:
]
i: i + 1
]</langsyntaxhighlight>
 
{{out}}
Line 241:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CIRCULAR_PRIMES.AWK
BEGIN {
Line 288:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 296:
=={{header|C}}==
{{libheader|GMP}}
<langsyntaxhighlight lang=c>#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Line 393:
test_repunit(49081);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 412:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang=cpp>#include <cstdint>
#include <algorithm>
#include <iostream>
Line 496:
test_repunit(49081);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 514:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang=D>import std.bigint;
import std.stdio;
 
Line 610:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>First 19 circular primes:
Line 617:
=={{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#)]
<langsyntaxhighlight lang=fsharp>
// 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)
Line 624:
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 ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
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).
{{works with|Factor|0.99 2020-03-02}}
<langsyntaxhighlight lang=factor>USING: combinators.short-circuit formatting io kernel lists
lists.lazy math math.combinatorics math.functions math.parser
math.primes sequences sequences.extras ;
Line 662:
 
"The next 4 circular primes, in repunit format, are:" print
4 prime-repunits ltake [ "R(%d) " printf ] leach nl</langsyntaxhighlight>
{{out}}
<pre>
Line 674:
=={{header|Forth}}==
Forth only supports native sized integers, so we only implement the first part of the task.
<langsyntaxhighlight lang=Forth>
create 235-wheel 6 c, 4 c, 2 c, 4 c, 2 c, 4 c, 6 c, 2 c,
does> swap 7 and + c@ ;
Line 731:
." The first 19 circular primes are:" cr .primes cr
bye
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 739:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>#define floor(x) ((x*2.0-0.5)Shr 1)
 
Function isPrime(Byval p As Integer) As Boolean
Line 769:
p += dp: dp = 2
Wend
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 778:
=={{header|Go}}==
{{libheader|GMP(Go wrapper)}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 901:
fmt.Printf("R(%-5d) : %t\n", i, repunit(i).ProbablyPrime(10))
}
}</langsyntaxhighlight>
 
{{out}}
Line 921:
=={{header|Haskell}}==
Uses arithmoi Library: http://hackage.haskell.org/package/arithmoi-0.10.0.0
<langsyntaxhighlight lang=haskell>import Math.NumberTheory.Primes (Prime, unPrime, nextPrime)
import Math.NumberTheory.Primes.Testing (isPrime, millerRabinV)
import Text.Printf (printf)
Line 973:
circular = show . take 19 . filter (isCircular False)
reps = map (sum . digits). tail . take 5 . filter (isCircular True)
checkReps = (,) <$> id <*> show . isCircular True . asRepunit</langsyntaxhighlight>
{{out}}
<pre>
Line 992:
</pre>
=={{header|J}}==
<syntaxhighlight lang=J>
<lang J>
 
R=: [: ". 'x' ,~ #&'1'
Line 1,028:
group </. P
)
</syntaxhighlight>
</lang>
J lends itself to investigation. Demonstrate and play with the definitions.
<pre>
Line 1,118:
 
=={{header|Java}}==
<langsyntaxhighlight lang=java>import java.math.BigInteger;
import java.util.Arrays;
 
Line 1,208:
return new BigInteger(new String(ch));
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,231:
 
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 circle: range(0;length) as $i | .[$i:] + .[:$i];
Line 1,244:
def repunits:
1 | recurse(10*. + 1);
</syntaxhighlight>
</lang>
'''The first task'''
<syntaxhighlight lang =jq>limit(19; circular_primes)</langsyntaxhighlight>
'''The second task''' (viewed independently):
<syntaxhighlight lang=jq>
<lang jq>
last(limit(19; circular_primes)) as $max
| limit(4; repunits | select(. > $max and is_prime))
| "R(\(tostring|length))"</langsyntaxhighlight>
{{out}}
For the first task:
Line 1,279:
=={{header|Julia}}==
Note that the evalpoly function used in this program was added in Julia 1.4
<langsyntaxhighlight lang=julia>using Lazy, Primes
 
function iscircularprime(n)
Line 1,299:
println("R($i) is ", isprimerepunit(i) ? "prime." : "not prime.")
end
</langsyntaxhighlight>{{out}}
<pre>
The first 19 circular primes are:
Line 1,317:
=={{header|Kotlin}}==
{{trans|C}}
<langsyntaxhighlight lang=scala>import java.math.BigInteger
 
val SMALL_PRIMES = listOf(
Line 1,434:
testRepUnit(35317)
testRepUnit(49081)
}</langsyntaxhighlight>
{{out}}
<pre>First 19 circular primes:
Line 1,448:
 
=={{header|Lua}}==
<langsyntaxhighlight lang=lua>-- Circular primes, in Lua, 6/22/2020 db
local function isprime(n)
if n < 2 then return false end
Line 1,474:
p, dp = p + dp, 2
end
print(table.concat(list, ", "))</langsyntaxhighlight>
{{out}}
<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}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[RepUnit, CircularPrimeQ]
RepUnit[n_] := (10^n - 1)/9
CircularPrimeQ[n_Integer] := Module[{id = IntegerDigits[n], nums, t},
Line 1,505:
]
 
Scan[Print@*PrimeQ@*RepUnit, {5003, 9887, 15073, 25031, 35317, 49081}]</langsyntaxhighlight>
{{out}}
<pre>{2, 3, 5, 7, 11, 13, 17, 37, 79, 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933}
Line 1,519:
{{trans|Kotlin}}
{{libheader|bignum}}
<langsyntaxhighlight lang=Nim>import bignum
import strformat
 
Line 1,629:
testRepUnit(25031)
testRepUnit(35317)
testRepUnit(49081)</langsyntaxhighlight>
 
{{out}}
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.
The main speed up is reached by testing with small primes within CycleNum.
<langsyntaxhighlight lang=pascal>
program CircularPrimes;
//nearly the way it is done:
Line 1,819:
{$ENDIF}
end.
</syntaxhighlight>
</lang>
{{Out|@ Tio.Run}}
<pre>
Line 1,849:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use feature 'say';
Line 1,883:
for (5003, 9887, 15073, 25031, 35317, 49081) {
say "R($_): Prime? " . (is_prime 1 x $_ ? 'True' : 'False');
}</langsyntaxhighlight>
{{out}}
<pre>The first 19 circular primes are:
Line 1,903:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<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>
Line 1,941:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,953:
===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>
<!--<langsyntaxhighlight 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: #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:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
64-bit can only cope with the first five (it terminates abruptly on the sixth)<br>
Line 1,988:
=={{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.
<langsyntaxhighlight lang=PicoLisp>
(load "plcommon/primality.l") # see task: "Miller-Rabin Primality Test"
 
Line 2,054:
(println Circular)
(bye)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
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.
<langsyntaxhighlight lang=Prolog>
?- use_module(library(primality)).
 
Line 2,108:
 
?- main.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,116:
 
=={{header|Python}}==
<langsyntaxhighlight lang=Python>
import random
 
Line 2,229:
 
# because this Miller-Rabin test is already on rosettacode there's no good reason to test the longer repunits.
</syntaxhighlight>
</lang>
 
=={{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.
{{libheader|ntheory}}
<syntaxhighlight lang=raku perl6line>sub isCircular(\n) {
return False unless n.is-prime;
my @circular = n.comb;
Line 2,259:
my $now = now;
say "R($_): Prime? ", ?is_prime("{1 x $_}"), " {(now - $now).fmt: '%.2f'}"
}</langsyntaxhighlight>
{{out}}
<pre>The first 19 circular primes are:
Line 2,279:
 
=={{header|REXX}}==
<langsyntaxhighlight 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*/
if N=='' | N=="," then N= 19 /* " " " " " " */
Line 2,315:
end /*k*/ /* [↓] a prime (J) has been found. */
#= #+1; !.j= 1; sq.#= j*j; @.#= j /*bump P cnt; assign P to @. and !. */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,324:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
see "working..." + nl
see "First 19 circular numbers are:" + nl
Line 2,368:
next
return 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,382:
=={{header|Ruby}}==
It takes more then 25 minutes to verify that R49081 is probably prime - omitted here.
<langsyntaxhighlight lang=ruby>require 'gmp'
require 'prime'
candidate_primes = Enumerator.new do |y|
Line 2,411:
puts reps.map{|r| "R" + r.to_s}.join(", "), ""
[5003, 9887, 15073, 25031].each {|rep| puts "R#{rep} circular_prime ? #{circular?("1"*rep)}" }
</syntaxhighlight>
</lang>
{{out}}
<pre>First 19 circular primes:
Line 2,426:
=={{header|Rust}}==
{{trans|C}}
<langsyntaxhighlight lang=rust>// [dependencies]
// rug = "1.8"
 
Line 2,531:
test_repunit(35317);
test_repunit(49081);
}</langsyntaxhighlight>
 
{{out}}
Line 2,550:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang=scala>object CircularPrimes {
def main(args: Array[String]): Unit = {
println("First 19 circular primes:")
Line 2,657:
BigInt.apply(new String(ch))
}
}</langsyntaxhighlight>
{{out}}
<pre>First 19 circular primes:
Line 2,670:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang=ruby>func is_circular_prime(n) {
n.is_prime || return false
 
Line 2,698:
say ("R(#{n}) -> ", is_prob_prime((10**n - 1)/9) ? 'probably prime' : 'composite',
" (took: #{'%.3f' % Time.micro-now} sec)")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,726:
===Wren-cli===
Second part is very slow - over 37 minutes to find all four.
<langsyntaxhighlight lang=ecmascript>import "/math" for Int
import "/big" for BigInt
import "/str" for Str
Line 2,792:
}
}
System.print(rus)</langsyntaxhighlight>
 
{{out}}
Line 2,806:
{{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.
<langsyntaxhighlight lang=ecmascript>/* circular_primes_embedded.wren */
import "./gmp" for Mpz
Line 2,880:
repunit.setStr(Str.repeat("1", i), 10)
Fmt.print("R($-5d) : $s", i, repunit.probPrime(15) > 0)
}</langsyntaxhighlight>
<br>
 
Line 2,901:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>func IsPrime(N); \Return 'true' if N > 2 is a prime number
int N, I;
[if (N&1) = 0 \even number\ then return false;
Line 2,943:
N:= N+2;
];
]</langsyntaxhighlight>
 
{{out}}
Line 2,952:
=={{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.
<langsyntaxhighlight lang=Zig>
const std = @import("std");
const math = std.math;
Line 3,037:
} else true;
}
</syntaxhighlight>
</lang>
{{Out}}
<pre>
10,333

edits