Pierpont primes: Difference between revisions

m
(Added Delphi exemple)
m (→‎{{header|Wren}}: Minor tidy)
 
(25 intermediate revisions by 15 users not shown)
Line 28:
 
<br>
 
=={{header|ALGOL 68}}==
As in the second Go sample, generates the 3-smooth number sequence to find Pierpoint Prime candidates. Uses a sliding window on the sequence to avoid having to keep it all in memory.
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find some pierpoint primes of the first kind (2^u3^v + 1) #
# and second kind (2^u3^v - 1) #
# construct a sieve of primes up to 10 000 #
PR read "primes.incl.a68" PR
[]BOOL prime = PRIMESIEVE 10 000;
# returns the minimum of a and b #
PROC min = ( INT a, b )INT: IF a < b THEN a ELSE b FI;
# returns TRUE if p is prime, FALSE otherwise #
PROC is prime = ( INT p )BOOL:
IF NOT ODD p
THEN p = 2
ELIF p <= UPB prime
THEN prime[ p ] # small enough to use the sieve #
ELSE # use trial division by the sieved primes #
BOOL probably prime := TRUE;
FOR d FROM 3 BY 2 WHILE d * d <= p AND probably prime DO
IF prime[ d ] THEN probably prime := p MOD d /= 0 FI
OD;
probably prime
FI; # is prime #
# sets the elements of pp1 to the first UPB pp1 Pierpoint primes of the #
# first kind and pp2 to the first UPB pp2 Pierpoint primes of the #
# second kind #
PROC find pierpoint = ( REF[]INT pp1, pp2 )VOID:
BEGIN
# saved 3-smooth values #
# - only the currently active part of the seuence is kept #
INT three smooth limit = 33;
[ 1 : three smooth limit * 3 ]INT s3s; FOR i TO UPB s3s DO s3s[ i ] := 0 OD;
INT pp1 pos := LWB pp1 - 1, pp2 pos := LWB pp2 - 1;
INT pp1 max = UPB pp1;
INT pp2 max = UPB pp2;
INT p2, p3, last2, last3;
INT s pos := s3s[ 1 ] := last2 := last3 := p2 := p3 := 1;
FOR n FROM 2 WHILE pp1 pos < pp1 max OR pp2 pos < pp2 max DO
# the next 3-smooth number is the lowest of the next #
# multiples of 2 and 3 #
INT m = min( p2, p3 );
IF pp1 pos < pp1 max THEN
IF INT first = m + 1;
is prime( first )
THEN # have a Pierpoint prime of the first kind #
pp1[ pp1 pos +:= 1 ] := first
FI
FI;
IF pp2 pos < pp2 max THEN
IF INT second = m - 1;
is prime( second )
THEN # have a Pierpoint prime of the second kind #
pp2[ pp2 pos +:= 1 ] := second
FI
FI;
s3s[ s pos +:= 1 ] := m;
IF m = p2 THEN p2 := 2 * s3s[ last2 +:= 1 ] FI;
IF m = p3 THEN p3 := 3 * s3s[ last3 +:= 1 ] FI;
INT last min = IF last2 > last3 THEN last3 ELSE last2 FI;
IF last min > three smooth limit THEN
# shuffle the sequence down, over the now unused bits #
INT new pos := 0;
FOR pos FROM last min TO s pos DO
s3s[ new pos +:= 1 ] := s3s[ pos ]
OD;
INT diff := last min - 1;
last2 -:= diff;
last3 -:= diff;
s pos -:= diff
FI
OD
END; # find pierpoint #
# prints a table of Pierpoint primes of the specified kind #
PROC print pierpoint = ( []INT primes, STRING kind )VOID:
BEGIN
print( ( "The first "
, whole( ( UPB primes + 1 ) - LWB primes, 0 )
, " Pierpoint primes of the "
, kind
, " kind:"
, newline
)
);
INT p count := 0;
FOR i FROM LWB primes TO UPB primes DO
print( ( " ", whole( primes[ i ], -8 ) ) );
IF ( p count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
OD;
print( ( newline ) )
END; # print pierpoint #
# find the first 50 Pierpoint primes of the first and second kinds #
INT max pierpoint = 50;
[ 1 : max pierpoint ]INT pfirst;
[ 1 : max pierpoint ]INT psecond;
find pierpoint( pfirst, psecond );
# show the Pierpoint primes #
print pierpoint( pfirst, "First" );
print pierpoint( psecond, "Second" )
END</syntaxhighlight>
{{out}}
<pre>
The first 50 Pierpoint primes of the First kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
 
The first 50 Pierpoint primes of the Second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
</pre>
 
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Line 154 ⟶ 270:
}
printf("\n");
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 171 ⟶ 287:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Numerics;
Line 343 ⟶ 459:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 364 ⟶ 480:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <cassert>
#include <algorithm>
#include <iomanip>
Line 456 ⟶ 572:
std::cout << "250th Pierpont prime of the second kind: " << p2 << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 480 ⟶ 596:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.bigint;
import std.random;
Line 644 ⟶ 760:
writefln("%dth Pierpont prime of the first kind: %d", p[0].length, p[0][$ - 1]);
writefln("%dth Pierpont prime of the second kind: %d", p[1].length, p[1][$ - 1]);
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 672 ⟶ 788:
{{Trans|Go}}
Thanks Rudy Velthuis for the [https://github.com/rvelthuis/DelphiBigNumbers Velthuis.BigIntegers.Primes and Velthuis.BigIntegers] library.<br>
<syntaxhighlight lang="delphi">
<lang Delphi>
program Pierpont_primes;
 
Line 743 ⟶ 859:
 
readln;
end.</langsyntaxhighlight>
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<syntaxhighlight lang="fsharp">
// Pierpont primes . Nigel Galloway: March 19th., 2021
let fN g=let mutable g=g in ((fun()->g),fun()->g<-g+g;())
let fG y=let rec fG n g=seq{match g|>List.minBy(fun(n,_)->n()) with (f,s) when f()=n->yield f()+y; s(); yield! fG(n*3)(fN(n*3)::g)
|(f,s) ->yield f()+y; s(); yield! fG n g}
seq{yield! fG 1 [fN 1]}|>Seq.filter isPrime
let pierpontT1,pierpontT2=fG 1,fG -1
 
pierpontT1|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
pierpontT2|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 13 17 19 37 73 97 109 163 193 257 433 487 577 769 1153 1297 1459 2593 2917 3457 3889 10369 12289 17497 18433 39367 52489 65537 139969 147457 209953 331777 472393 629857 746497 786433 839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
2 3 5 7 11 17 23 31 47 53 71 107 127 191 383 431 647 863 971 1151 2591 4373 6143 6911 8191 8747 13121 15551 23327 27647 62207 73727 131071 139967 165887 294911 314927 442367 472391 497663 524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
</pre>
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: fry grouping io kernel locals make math math.functions
math.primes prettyprint sequences sorting ;
 
Line 773 ⟶ 907:
"250th Pierpont prime of the second kind: " write
249 second nth .
]</langsyntaxhighlight>
{{out}}
<pre>
Line 796 ⟶ 930:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define NPP 50
{{output?}}
<lang freebasic>#define NPP 50
 
Function isPrime(Byval n As Ulongint) As Boolean
function is_prime( n as ulongint ) as boolean
ifIf n < 2 thenThen returnReturn false
ifIf n = 2 thenThen returnReturn true
ifIf n modMod 2 = 0 thenThen returnReturn false
forFor i asAs uintegerUinteger = 3 toTo intInt(sqrSqr(n))+1 stepStep 2
ifIf n modMod i = 0 thenThen returnReturn false
nextNext i
returnReturn true
End Function
end function
 
functionFunction is_23( byvalByval n as uintegerAs Uinteger) asAs booleanBoolean
whileWhile n modMod 2 = 0
n = n /= 2
wendWend
whileWhile n modMod 3 = 0
n = n /= 3
wendWend
ifReturn Iif(n = 1 then return, true else return, false)
End Function
end function
 
Function isPierpont(n As Uinteger) As Uinteger
function is_pierpont( n as uinteger ) as uinteger
ifIf notNot is_primeisPrime(n) thenThen returnReturn 0 'not prime
dimDim asAs integerUinteger p1 = is_23(n+1), p2 = is_23(n-1)
ifIf p1 andAnd p2 thenThen returnReturn 3 'pierpont prime of both kinds
ifIf p1 thenThen returnReturn 1 'pierpont prime of the 1st kind
ifIf p2 thenThen returnReturn 2 'pierpont prime of the 2nd kind
returnReturn 0 'prime, but not pierpont
End Function
end function
 
dimDim asAs uintegerUinteger pier(1 toTo 2, 1 toTo NPP), np(1 toTo 2) = {0, 0}, x = 1, j
Dim As Uinteger x = 1, j
while np(1)<=NPP or np(2)<=NPP
While np(1) <= NPP Or np(2) <= NPP
x = x + 1
jx += is_pierpont(x)1
if j>0 then= isPierpont(x)
ifIf j mod 2 = 1> then0 Then
If j Mod 2 = 1 Then
np(1) += 1
ifIf np(1) <= NPP thenThen pier(1, np(1)) = x
endEnd ifIf
ifIf j > 1 thenThen
np(2) += 1
ifIf np(2) <= NPP thenThen pier(2, np(2)) = x
endEnd ifIf
endEnd ifIf
Wend
wend
 
printPrint " First 50 Pierpoint primes of the first Secondkind:"
forFor j = 1 toTo NPP
printPrint j,Using pier(1," j),########"; pier(2, j);
If j Mod 10 = 0 Then Print
next j</lang>
Next j
Print !"\nFirst 50 Pierpoint primes of the secod kind:"
For j = 1 To NPP
Print Using " ########"; pier(1, j);
If j Mod 10 = 0 Then Print
Next j
</syntaxhighlight>
{{out}}
<pre>
First 50 Pierpont primes of the first kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
 
First 50 Pierpont primes of the second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
</pre>
 
=={{header|Go}}==
Line 858 ⟶ 1,015:
 
However, in order to be sure that the first 250 Pierpont primes will be generated, it is necessary to choose the loop sizes to produce somewhat more than this and then sort them into order.
<langsyntaxhighlight lang="go">package main
 
import (
Line 919 ⟶ 1,076:
fmt.Println("\n250th Pierpont prime of the first kind:", pp[249])
fmt.Println("\n250th Pierpont prime of the second kind:", pp2[249])
}</langsyntaxhighlight>
 
{{out}}
Line 951 ⟶ 1,108:
These timings are for my Celeron @1.6GHz and should therefore be much faster on a more modern machine.
 
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,043 ⟶ 1,200:
elapsed := time.Now().Sub(start)
fmt.Printf("\nTook %s\n", elapsed)
}</langsyntaxhighlight>
 
{{out}}
Line 1,079 ⟶ 1,236:
Uses arithmoi Library: https://hackage.haskell.org/package/arithmoi-0.11.0.0 for prime generation and prime testing.<br>
n-smooth generation function based on [[Hamming_numbers#Avoiding_generation_of_duplicates]]
<langsyntaxhighlight lang="haskell">import Control.Monad (guard)
import Data.List (intercalate)
import Data.List.Split (chunksOf)
Line 1,119 ⟶ 1,276:
where
rows = chunksOf 10 . take 50
commas = reverse . intercalate "," . chunksOf 3 . reverse . show</langsyntaxhighlight>
{{out}}
<pre>
Line 1,142 ⟶ 1,299:
./pierpoints 0.04s user 0.01s system 20% cpu 0.215 total
</pre>
 
=={{header|J}}==
 
Implementation (first kind first):
 
<syntaxhighlight lang="j"> 5 10$(#~ 1 p:])1+/:~,*//2 3x^/i.20
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
5 10$(#~ 1 p:])_1+/:~,*//2 3x^/i.20
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 25509167 30233087 57395627</syntaxhighlight>
 
and
 
<syntaxhighlight lang="j"> 249{ (#~ 1 p:])1+/:~,*//2 3x^/i.112
62518864539857068333550694039553
249{ (#~ 1 p:])_1+/:~,*//2 3x^/i.112
4111131172000956525894875083702271</syntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.text.NumberFormat;
Line 1,210 ⟶ 1,391:
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,231 ⟶ 1,412:
 
250th Pierpont prime of the second kind: 4,111,131,172,000,956,525,894,875,083,702,271
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
Since we do not know how often 2^u * 3^v ± 1 is prime, we use an
adaptive approach based on adjusting the upper bounds for u and v
that we need consider. The idea is to avoid any hand-waving about the
correctness of the solution. Some "debug" statements that are
informative about the adaptive steps have been retained as comments.
 
The standard (C-based) implementation of jq does not support very large
integers, and the Go implementation, gojq, requires too much memory
to solve the stretch problem, so the code for the stretch task is
shown, but not executed.
 
'''Preliminaries'''
<syntaxhighlight lang="jq">def is_prime:
. as $n
| if ($n < 2) then false
elif ($n % 2 == 0) then $n == 2
elif ($n % 3 == 0) then $n == 3
elif ($n % 5 == 0) then $n == 5
elif ($n % 7 == 0) then $n == 7
elif ($n % 11 == 0) then $n == 11
elif ($n % 13 == 0) then $n == 13
elif ($n % 17 == 0) then $n == 17
elif ($n % 19 == 0) then $n == 19
else {i:23}
| until( (.i * .i) > $n or ($n % .i == 0); .i += 2)
| .i * .i > $n
end;
 
# pretty-printing
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def table($ncols; $colwidth):
nwise($ncols) | map(lpad($colwidth)) | join(" ");</syntaxhighlight>
'''Pierpont primes'''
<syntaxhighlight lang="jq"># Input: the target number of primes of the form p(u,v) == 2^u * 3^v ± 1.
# The main idea is to let u run from 0:m and v run from 0:n where n ~~ 0.63 * m.
# Initially we start with plausible values for m and n ($maxm and $maxn respectively),
# and then check whether these have been chosen conservatively enough.
#
def pierponts($firstkind):
. as $N
| (if $firstkind then 1 else -1 end) as $incdec
 
# Input: [$maxm, $maxn]
# Output: an array of objects of the form {p, m, n}
# where .p is prime and .m and .n are the corresponding powers of 2 and 3
| def pp:
. as [$maxm, $maxn]
| [ ({p2:1, m:0},
(foreach range(0; $maxm) as $m (1; . * 2; {p2: ., m: ($m + 1)}))) as $a
| ({p3:1, n:0},
(foreach range(0; $maxn) as $n (1; . * 3; {p3: ., n: ($n + 1)}))) as $b
| {p: ($a.p2 * $b.p3 + $incdec), m: $a.m, n: $b.n}
| select(.p|is_prime) ]
| unique_by(.p)
# | (length|debug) as $debug # informative
| .;
 
# input: output of pp
# check that length is sufficient, and that $maxm and $maxn are large enough
def adequate($maxm; $maxn):
# ( ".[$N-1].m is \(.[$N-1].m) vs $maxm=\($maxm)" | debug) as $debug |
# ( ".[$N-1].n is \(.[$N-1].n) vs $maxn=\($maxn)" | debug) as $debug |
length > $N
and .[$N-1].m < $maxm - 3 # -2 is not sufficient
and .[$N-1].n < $maxn - 3 # -2 is not sufficient
;
 
# If our search has not been `adequate` then increase $maxm and $maxn
# input: [maxm, maxn, ([maxm,maxn]|pp)]
# output: pp
def adapt:
. as [$maxm, $maxn, $one]
| if ($one|adequate($maxm; $maxn)) then $one
else [$maxm + 2, $maxn + 1.6] as $maxplus
# | ("retrying with \($maxplus)" | debug) as $debug
| ($maxplus|pp) as $two
| $maxplus + [$two] | adapt
end;
 
# We start by selecting m and n so that
# m*n >> $N, i.e., 0.63 * m^2 >> $N , so m >> sqrt(1.585 * $N)
# Using 7 as the constant to start with is sufficient to avoid too much rework.
((9 * $N) | sqrt) as $maxm
| (0.63 * $maxm + 1) as $maxn
| [$maxm, $maxn] as $max
| ($max | pp) as $pp
| ($max + [$pp]) | [adapt[:$N][].p] ;
 
# The stretch goal:
def stretch:
250
| "\nThe \(.)th Pierpoint prime of the first kind is \(pierponts(true)[-1])",
"\nThe \(.)th Pierpoint prime of the second kind is \(pierponts(false)[-1])" ;
 
# The primary task:
50
| "\nThe first \(.) Pierpoint primes of the first kind:", (pierponts(true) | table(10;8)),
"\nThe first \(.) Pierpoint primes of the second kind:", (pierponts(false) | table(10;8))</syntaxhighlight>
{{out}}
<pre>
The first 50 Pierpoint primes of the first kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
 
The first 50 Pierpoint primes of the second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
</pre>
 
=={{header|Julia}}==
The generator method is very fast but does not guarantee the primes are generated in order. Therefore we generate two times the primes needed and then sort and return the lower half.
<langsyntaxhighlight lang="julia">using Primes
 
function pierponts(N, firstkind = true)
Line 1,267 ⟶ 1,572:
 
println("\nThe 2000th Pierpont prime (second kind) is: ", pierponts(2000, false)[2000])
</langsyntaxhighlight>{{out}}
<pre>
The first 50 Pierpont primes (first kind) are: BigInt[2, 3, 5, 7, 13, 17, 19, 37, 73, 97, 109, 163, 193, 257, 433, 487, 577, 769, 1153, 1297, 1459, 2593, 2917, 3457, 3889, 10369, 12289, 17497, 18433, 39367, 52489, 65537, 139969, 147457, 209953, 331777, 472393, 629857, 746497, 786433, 839809, 995329, 1179649, 1492993, 1769473, 1990657, 2654209, 5038849, 5308417, 8503057]
Line 1,288 ⟶ 1,593:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
import kotlin.math.min
 
Line 1,365 ⟶ 1,670:
println("\n2000th Pierpont prime of the first kind: ${p[0][1999]}")
println("\n2000th Pierpont prime of the first kind: ${p[1][1999]}")
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,394 ⟶ 1,699:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">local function isprime(n)
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
Line 1,443 ⟶ 1,748:
for i, p in ipairs(p2) do
io.write(string.format("%8d%s", p, (i%10==0 and "\n" or " ")))
end</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,458 ⟶ 1,763:
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087</pre>
 
=={{header|M2000 Interpreter}}==
{{trans|freebasic}}
<syntaxhighlight lang="m2000 interpreter">Module Pierpoint_Primes {
Form 80
Set Fast !
const NPP=50
dim pier(1 to 2, 1 to NPP), np(1 to 2) = 0
def long x = 1, j
while np(1)<=NPP or np(2)<=NPP
x++
j = @is_pierpont(x)
if j>0 Else Continue
if j mod 2 = 1 then np(1)++ :if np(1) <= NPP then pier(1, np(1)) = x
if j > 1 then np(2)++ : if np(2) <= NPP then pier(2, np(2)) = x
end while
print "First ";NPP;" Pierpont primes of the first kind:"
for j = 1 to NPP
print pier(2, j),
next j
if pos>0 then print
print "First ";NPP;" Pierpont primes of the second kind:"
for j = 1 to NPP
print pier(1, j),
next j
if pos>0 then print
Set Fast
function is_prime(n as decimal)
if n < 2 then = false : exit function
if n <4 then = true : exit function
if n mod 2 = 0 then = false : exit function
local i as long
for i = 3 to int(sqrt(n))+1 step 2
if n mod i = 0 then = false : exit function
next i
= true
end function
function is_23(n as long)
while n mod 2 = 0
n = n div 2
end while
while n mod 3 = 0
n = n div 3
end while
if n = 1 then = true else = false
end function
function is_pierpont(n as long)
if not @is_prime(n) then = 0& : exit function 'not prime
Local p1 = @is_23(n+1), p2 = @is_23(n-1)
if p1 and p2 then = 3 : exit function 'pierpont prime of both kinds
if p1 then = 1 : exit function 'pierpont prime of the 1st kind
if p2 then = 2 : exit function 'pierpont prime of the 2nd kind
= 0 'prime, but not pierpont
end function
}
Pierpoint_Primes</syntaxhighlight>
 
{{out}}
<pre>
First 50 Pierpont primes of the first kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
 
First 50 Pierpont primes of the second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[FindUpToMax]
FindUpToMax[max_Integer, b_Integer] := Module[{res, num},
res = {};
Do[
num = 2^u 3^v + b;
If[PrimeQ[num], AppendTo[res, num]]
,
{u, 0, Ceiling@Log[2, max]}
,
{v, 0, Ceiling@Log[3, max]}
];
res //= Select[LessEqualThan[max]];
res //= Sort;
res
]
Print["Piermont primes of the first kind:"]
Take[FindUpToMax[10^10, +1], UpTo[50]]
Print["Piermont primes of the second kind:"]
Take[FindUpToMax[10^10, -1], UpTo[50]]
Print["250th Piermont prime of the first kind:"]
Part[FindUpToMax[10^34, +1], 250]
Print["250th Piermont prime of the second kind:"]
Part[FindUpToMax[10^34, -1], 250]
Print["1000th Piermont prime of the first kind:"]
Part[FindUpToMax[10^130, +1], 1000]
Print["1000th Piermont prime of the second kind:"]
Part[FindUpToMax[10^150, -1], 1000]</syntaxhighlight>
{{out}}
<pre>Piermont primes of the first kind:
{2,3,5,7,13,17,19,37,73,97,109,163,193,257,433,487,577,769,1153,1297,1459,2593,2917,3457,3889,10369,12289,17497,18433,39367,52489,65537,139969,147457,209953,331777,472393,629857,746497,786433,839809,995329,1179649,1492993,1769473,1990657,2654209,5038849,5308417,8503057}
Piermont primes of the second kind:
{2,3,5,7,11,17,23,31,47,53,71,107,127,191,383,431,647,863,971,1151,2591,4373,6143,6911,8191,8747,13121,15551,23327,27647,62207,73727,131071,139967,165887,294911,314927,442367,472391,497663,524287,786431,995327,1062881,2519423,10616831,17915903,18874367,25509167,30233087}
250th Piermont primes of the first kind:
62518864539857068333550694039553
250th Piermont primes of the second kind:
4111131172000956525894875083702271
1000th Piermont prime of the first kind:
69269314716439690250482558089997110961545818230232043107188537422260188701607997086273960899938499201024414931399264696270849
1000th Piermont prime of the second kind:
1308088756227965581249669045506775407896673213729433892383353027814827286537163695213418982500477392209371001259166465228280492460735463423</pre>
 
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, strutils
 
func isPrime(n: int): bool =
Line 1,516 ⟶ 1,940:
stdout.write ($n).align(9)
inc count
if count mod 10 == 0: stdout.write '\n'</langsyntaxhighlight>
 
{{out}}
Line 1,536 ⟶ 1,960:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,588 ⟶ 2,012:
 
say "\n250th Pierpont prime of the first kind: " . $pierpont_1st[249];
say "\n250th Pierpont prime of the second kind: " . $pierpont_2nd[249];</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,612 ⟶ 2,036:
{{trans|Go}}
I also tried using a priority queue, popping the smallest (and any duplicates of it) and pushing *2 and *3 on every iteration, which worked pretty well but ended up about 20% slower, with about 1500 untested candidates left in the queue by the time it found the 2000th second kind.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- demo/rosetta/Pierpont_primes.exw
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Pierpont_primes.exw</span>
include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
function pierpont(integer n)
sequence p = {{mpz_init(2)},{}},
<span style="color: #008080;">function</span> <span style="color: #000000;">pierpont</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
s = {mpz_init(1)}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)},{}},</span>
integer i2 = 1, i3 = 1
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)}</span>
mpz {n2, n3, t} = mpz_inits(3)
<span style="color: #004080;">integer</span> <span style="color: #000000;">i2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
randstate state = gmp_randinit_mt()
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
atom t1 = time()+1
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
while min(length(p[1]),length(p[2])) < n do
<span style="color: #008080;">while</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]))</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
mpz_mul_si(n2,s[i2],2)
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
mpz_mul_si(n3,s[i3],3)
<span style="color: #7060A8;">mpz_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i3</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
if mpz_cmp(n2,n3)<0 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
mpz_set(t,n2)
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">)</span>
i2 += 1
<span style="color: #000000;">i2</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
else
<span style="color: mpz_set(t,n3)#008080;">else</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n3</span><span style="color: #0000FF;">)</span>
i3 += 1
<span style="color: #000000;">i3</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if mpz_cmp(t,s[$]) > 0 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[$])</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
s = append(s, mpz_init_set(t))
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span>
mpz_add_ui(t,t,1)
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
if length(p[1]) < n and mpz_probable_prime_p(t,state) then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">n</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
p[1] = append(p[1],mpz_init_set(t))
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
mpz_sub_ui(t,t,2)
<span style="color: #7060A8;">mpz_sub_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
if length(p[2]) < n and mpz_probable_prime_p(t,state) then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span> <span style="color: #0000FF;"><</span> <span style="color: #000000;">n</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">mpz_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
p[2] = append(p[2],mpz_init_set(t))
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">mpz_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if time()>t1 then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">then</span>
printf(1,"Found: 1st:%d/%d, 2nd:%d/%d\r",
<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;">"Found: 1st:%d/%d, 2nd:%d/%d\r"</span><span style="color: #0000FF;">,</span>
{length(p[1]),n,length(p[2]),n})
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
t1 = time()+1
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
return p
<span style="color: #008080;">return</span> <span style="color: #000000;">p</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant limit = 2000 -- 2 mins
<span style="color: #000080;font-style:italic;">--constant limit = 10002000 -- 8.1s2 mins
--constant limit = 250 1000 -- 08.1s</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">limit</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">250</span> <span style="color: #000080;font-style:italic;">-- 0.1s</span>
atom t0 = time()
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
sequence p = pierpont(limit)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pierpont</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
constant fs = {"first","second"}
<span style="color: #008080;">constant</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"first"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"second"</span><span style="color: #0000FF;">}</span>
 
for i=1 to length(fs) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"First 50 Pierpont primes of the %s kind:\n",{fs[i]})
<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;">"First 50 Pierpont primes of the %s kind:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]})</span>
for j=1 to 50 do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">50</span> <span style="color: #008080;">do</span>
mpfr_printf(1,"%8Zd ", p[i][j])
<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;">"%8s "</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])})</span>
if mod(j,10)=0 then printf(1,"\n") end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</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;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"\n")
<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;">"\n"</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
constant t = {250,1000,2000}
<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;">250</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2000</span><span style="color: #0000FF;">}</span>
for i=1 to length(t) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer ti = t[i]
<span style="color: #004080;">integer</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
if ti>limit then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">ti</span><span style="color: #0000FF;">></span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for j=1 to length(fs) do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string zs = shorten(mpz_get_str(p[j][ti]))
<span style="color: #004080;">string</span> <span style="color: #000000;">zs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">]))</span>
printf(1,"%dth Pierpont prime of the %s kind: %s\n",{ti,fs[j],zs})
<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;">"%dth Pierpont prime of the %s kind: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">zs</span><span style="color: #0000FF;">})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"\n")
<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;">"\n"</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"Took %s\n", elapsed(time()-t0))</lang>
<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;">"Took %s\n"</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,706 ⟶ 2,132:
Took 2 minutes and 01s
</pre>
Note that shorten() has recently been added as a builtin, in honour of this and the several other dozen rc
tasks that previously all re-implemented some variation of it. Should you not yet have it, just use this:
<lang Phix>function shorten(string s, what="digits", integer ml=20)
integer l = length(s)
string ls = sprintf(" (%d %s)",{l,what})
if l>ml*2+3+length(ls) then
s[ml..-ml] = "..."
s &= ls
end if
return s
end function</lang>
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
<lang Prolog>
?- use_module(library(heaps)).
 
Line 1,808 ⟶ 2,223:
 
?- main.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,819 ⟶ 2,234:
=={{header|Python}}==
{{trans|Go}}
<langsyntaxhighlight lang="python">import random
 
# Copied from https://rosettacode.org/wiki/Miller-Rabin_primality_test#Python
Line 1,896 ⟶ 2,311:
print "250th Pierpont prime of the second kind:", pp2[249]
 
main()</langsyntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,912 ⟶ 2,327:
250th Pierpont prime of the first kind: 62518864539857068333550694039553
250th Pierpont prime of the second kind: 4111131172000956525894875083702271</pre>
 
=={{header|Quackery}}==
 
Uses <code>smoothwith</code> from [[N-smooth numbers#Quackery]] and <code>isprime</code> from [[Primality by trial division#Quackery]].
 
Using trial division to determine primality makes finding the 250th Pierpont primes impractical. This should be updated when a coding of a more suitable test becomes available.
 
<syntaxhighlight lang="quackery"> [ stack ] is pierponts
[ stack ] is kind
[ stack ] is quantity
 
[ 1 - -2 * 1+ kind put
1+ quantity put
' [ -1 ] pierponts put
' [ 2 3 ] smoothwith
[ -1 peek
kind share +
dup isprime not iff
[ drop false ] done
pierponts share -1 peek
over = iff
[ drop false ] done
pierponts take
swap join
dup size swap
pierponts put
quantity share = ]
drop
quantity release
kind release
pierponts take
behead drop ] is pierpontprimes
 
say "Pierpont primes of the first kind." cr
50 1 pierpontprimes echo
cr cr
say "Pierpont primes of the second kind." cr
50 2 pierpontprimes echo</syntaxhighlight>
 
{{out}}
 
<pre>Pierpont primes of the first kind.
[ 2 3 5 7 13 17 19 37 73 97 109 163 193 257 433 487 577 769 1153 1297 1459 2593 2917 3457 3889 10369 12289 17497 18433 39367 52489 65537 139969 147457 209953 331777 472393 629857 746497 786433 839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057 ]
 
Pierpont primes of the second kind.
[ 2 3 5 7 11 17 23 31 47 53 71 107 127 191 383 431 647 863 971 1151 2591 4373 6143 6911 8191 8747 13121 15551 23327 27647 62207 73727 131071 139967 165887 294911 314927 442367 472391 497663 524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087 ]</pre>
 
=={{header|Raku}}==
Line 1,923 ⟶ 2,384:
an overabundance. No need to rely on magic numbers. No need to sort them. It
produces exactly what is needed, in order, on demand.
{{libheader|ntheory}}
 
<syntaxhighlight lang="raku" perl6line>use ntheory:from<Perl5> <is_prime>;
 
sub pierpont ($kind is copy = 1) {
Line 1,957 ⟶ 2,418:
say "\n250th Pierpont prime of the first kind: " ~ pierpont[249];
 
say "\n250th Pierpont prime of the second kind: " ~ pierpont(2)[249];</langsyntaxhighlight>
 
{{out}}
Line 1,983 ⟶ 2,444:
(Cut down output as it is exactly the same as the first version for {2,3} +1 and {2,3} -1; leaves room to demo some other options.)
 
<syntaxhighlight lang="raku" perl6line>sub smooth-numbers (*@list) {
cache my \Smooth := gather {
my %i = (flat @list) Z=> (Smooth.iterator for ^@list);
Line 2,011 ⟶ 2,472:
"\nOEIS: A077314:", (2,7), -1, 20,
"\nOEIS: A174144 - (\"Humble\" primes 1st):", (2,3,5,7), 1, 20,
"\nOEIS: A299171A347977 - (\"Humble\" primes 2nd):", (2,3,5,7), -1, 20,
"\nOEIS: A077499:", (2,11), 1, 20,
"\nOEIS: A077315:", (2,11), -1, 20,
Line 2,021 ⟶ 2,482:
say "$title smooth \{$primes\} {$add > 0 ?? '+' !! '-'} 1 ";
put smooth-numbers(|$primes).map( * + $add ).grep( *.is-prime )[^$count]
}</langsyntaxhighlight>
 
{{Out}}
Line 2,075 ⟶ 2,536:
The REXX language has a "big num" capability to handle almost any amount of decimal digits, &nbsp; but
<br>it lacks a robust &nbsp; '''isPrime''' &nbsp; function. &nbsp; Without that, verifying very large primes is problematic.
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays Pierpont primes of the first and second kinds. */
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
Line 2,110 ⟶ 2,571:
_= pu*pv + t; if _ >s then m= min(_, m)
end /*jv*/
end /*ju*/ /*see the RETURN (above). */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,129 ⟶ 2,590:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,186 ⟶ 2,647:
 
see "done2..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,298 ⟶ 2,759:
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'gmp'
 
def smooth_generator(ar)
return to_enum(__method__, ar) unless block_given?
next_smooth = 1
queues = ar.map{|num| [num, []] }
loop do
yield next_smooth
queues.each {|m, queue| queue << next_smooth * m}
next_smooth = queues.collect{|m, queue| queue.first}.min
queues.each{|m, queue| queue.shift if queue.first == next_smooth }
end
end
def pierpont(num = 1)
return to_enum(__method__, num) unless block_given?
smooth_generator([2,3]).each{|smooth| yield smooth+num if GMP::Z(smooth + num).probab_prime? > 0}
end
 
def puts_cols(ar, n=10)
ar.each_slice(n).map{|slice|puts slice.map{|n| n.to_s.rjust(10)}.join }
end
 
n, m = 50, 250
puts "First #{n} Pierpont primes of the first kind:"
puts_cols(pierpont.take(n))
puts "#{m}th Pierpont prime of the first kind: #{pierpont.take(250).last}",""
puts "First #{n} Pierpont primes of the second kind:"
puts_cols(pierpont(-1).take(n))
puts "#{m}th Pierpont prime of the second kind: #{pierpont(-1).take(250).last}"
</syntaxhighlight>
{{out}}
<pre>First 50 Pierpont primes of the first kind:
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
250th Pierpont prime of the first kind: 62518864539857068333550694039553
 
First 50 Pierpont primes of the second kind:
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func smooth_generator(primes) {
var s = primes.len.of { [1] }
{
Line 2,327 ⟶ 2,837:
say "\n#{n}th Pierpont prime of the 1st kind: #{p}"
say "#{n}th Pierpont prime of the 2nd kind: #{q}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,345 ⟶ 2,855:
1000th Pierpont prime of the 2nd kind: 1308088756227965581249669045506775407896673213729433892383353027814827286537163695213418982500477392209371001259166465228280492460735463423
</pre>
 
=={{header|Swift}}==
 
3-smooth version.
 
Using AttaSwift's BigInt library.
 
<syntaxhighlight lang="swift">import BigInt
import Foundation
 
public func pierpoint(n: Int) -> (first: [BigInt], second: [BigInt]) {
var primes = (first: [BigInt](repeating: 0, count: n), second: [BigInt](repeating: 0, count: n))
 
primes.first[0] = 2
 
var count1 = 1, count2 = 0
var s = [BigInt(1)]
var i2 = 0, i3 = 0, k = 1
var n2 = BigInt(0), n3 = BigInt(0), t = BigInt(0)
 
while min(count1, count2) < n {
n2 = s[i2] * 2
n3 = s[i3] * 3
 
if n2 < n3 {
t = n2
i2 += 1
} else {
t = n3
i3 += 1
}
 
if t <= s[k - 1] {
continue
}
 
s.append(t)
k += 1
t += 1
 
if count1 < n && t.isPrime(rounds: 10) {
primes.first[count1] = t
count1 += 1
}
 
t -= 2
 
if count2 < n && t.isPrime(rounds: 10) {
primes.second[count2] = t
count2 += 1
}
}
 
return primes
}
 
 
let primes = pierpoint(n: 250)
 
print("First 50 Pierpoint primes of the first kind: \(Array(primes.first.prefix(50)))\n")
print("First 50 Pierpoint primes of the second kind: \(Array(primes.second.prefix(50)))")
print()
print("250th Pierpoint prime of the first kind: \(primes.first[249])")
print("250th Pierpoint prime of the second kind: \(primes.second[249])")</syntaxhighlight>
 
{{out}}
 
<pre>First 50 Pierpoint primes of the first kind: [2, 3, 5, 7, 13, 17, 19, 37, 73, 97, 109, 163, 193, 257, 433, 487, 577, 769, 1153, 1297, 1459, 2593, 2917, 3457, 3889, 10369, 12289, 17497, 18433, 39367, 52489, 65537, 139969, 147457, 209953, 331777, 472393, 629857, 746497, 786433, 839809, 995329, 1179649, 1492993, 1769473, 1990657, 2654209, 5038849, 5308417, 8503057]
 
First 50 Pierpoint primes of the second kind: [2, 3, 5, 7, 11, 17, 23, 31, 47, 53, 71, 107, 127, 191, 383, 431, 647, 863, 971, 1151, 2591, 4373, 6143, 6911, 8191, 8747, 13121, 15551, 23327, 27647, 62207, 73727, 131071, 139967, 165887, 294911, 314927, 442367, 472391, 497663, 524287, 786431, 995327, 1062881, 2519423, 10616831, 17915903, 18874367, 25509167, 30233087]
 
250th Pierpoint prime of the first kind: 62518864539857068333550694039553
250th Pierpoint prime of the second kind: 4111131172000956525894875083702271</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-big}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
The 3-smooth version. Just the first 250 - a tolerable 14 seconds or so on my machine.
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./mathfmt" for MathFmt
import "/fmt" for Fmt
 
var pierpont = Fn.new { |n, first|
Line 2,394 ⟶ 2,975:
count2 = count2 + 1
}
count = Mathcount1.min(count1, count2)
}
}
Line 2,400 ⟶ 2,981:
}
 
var start = System.clock
var p = pierpont.call(250, true)
System.print("First 50 Pierpont primes of the first kind:")
Line 2,414 ⟶ 2,994:
 
System.print("\n250th Pierpont prime of the first kind: %(p[0][249])")
System.print("\n250th Pierpont prime of the second kind: %(p[1][249])")</syntaxhighlight>
System.print("Took %(System.clock - start)")</lang>
 
{{out}}
Line 2,436 ⟶ 3,015:
 
250th Pierpont prime of the second kind: 4111131172000956525894875083702271
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func IsPrime(N); \Return 'true' if N is prime
int N, D;
[if N < 2 then return false;
if (N&1) = 0 then return N = 2;
if rem(N/3) = 0 then return N = 3;
D:= 5;
while D*D <= N do
[if rem(N/D) = 0 then return false;
D:= D+2;
if rem(N/D) = 0 then return false;
D:= D+4;
];
return true;
];
 
func Pierpont(N); \Return 'true' if N is a multiple of 2^U*3^V
int N;
[while (N&1) = 0 do N:= N>>1;
while N>1 do
[N:= N/3;
if rem(0) # 0 then return false;
];
return true;
];
 
int Kind, N, Count;
[Format(9, 0);
Kind:= 1;
repeat Count:= 0; N:= 2;
loop [if IsPrime(N) then
[if Pierpont(N-Kind) then
[Count:= Count+1;
RlOut(0, float(N));
if rem(Count/10) = 0 then CrLf(0);
if Count >= 50 then quit;
];
];
N:= N+1;
];
CrLf(0);
Kind:= -Kind;
until Kind = 1;
]</syntaxhighlight>
{{out}}
<pre>
2 3 5 7 13 17 19 37 73 97
109 163 193 257 433 487 577 769 1153 1297
1459 2593 2917 3457 3889 10369 12289 17497 18433 39367
52489 65537 139969 147457 209953 331777 472393 629857 746497 786433
839809 995329 1179649 1492993 1769473 1990657 2654209 5038849 5308417 8503057
 
2 3 5 7 11 17 23 31 47 53
71 107 127 191 383 431 647 863 971 1151
2591 4373 6143 6911 8191 8747 13121 15551 23327 27647
62207 73727 131071 139967 165887 294911 314927 442367 472391 497663
524287 786431 995327 1062881 2519423 10616831 17915903 18874367 25509167 30233087
 
</pre>
 
Line 2,442 ⟶ 3,081:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
Using GMP's probabilistic primes makes it is easy and fast to test for primeness.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
var [const] one=BI(1), two=BI(2), three=BI(3);
 
Line 2,470 ⟶ 3,109:
}
return(pps1,pps2)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">pps1,pps2 := pierPonts(2_000);
 
println("The first 50 Pierpont primes (first kind):");
Line 2,482 ⟶ 3,121:
println("\n%4dth Pierpont prime, first kind: ".fmt(n), pps1[n-1]);
println( " second kind: ", pps2[n-1]);
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
9,476

edits