Pierpont primes: Difference between revisions
Content deleted Content added
(13 intermediate revisions by 9 users not shown) | |||
Line 32:
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}}
<
# and second kind (2^u3^v - 1) #
# construct a sieve of primes up to 10 000 #
Line 127:
print pierpoint( pfirst, "First" );
print pierpoint( psecond, "Second" )
END</
{{out}}
<pre>
Line 147:
=={{header|C}}==
{{trans|D}}
<
#include <stdint.h>
#include <stdio.h>
Line 270:
}
printf("\n");
}</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 287:
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Numerics;
Line 459:
}
}
}</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 480:
=={{header|C++}}==
{{libheader|GMP}}
<
#include <algorithm>
#include <iomanip>
Line 572:
std::cout << "250th Pierpont prime of the second kind: " << p2 << '\n';
return 0;
}</
{{out}}
Line 596:
=={{header|D}}==
{{trans|C#}}
<
import std.bigint;
import std.random;
Line 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]);
}</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 788:
{{Trans|Go}}
Thanks Rudy Velthuis for the [https://github.com/rvelthuis/DelphiBigNumbers Velthuis.BigIntegers.Primes and Velthuis.BigIntegers] library.<br>
<syntaxhighlight lang="delphi">
program Pierpont_primes;
Line 859:
readln;
end.</
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
fastfunc isprim num .
if num mod 2 = 0
if num = 2
return 1
.
return 0
.
i = 3
while i <= sqrt num
if num mod i = 0
return 0
.
i += 2
.
return 1
.
fastfunc mod2x3x n .
while n mod 2 = 0
n /= 2
.
while n mod 3 = 0
n /= 3
.
return n
.
i = 2
cnt = 1
write 2 & " "
while cnt < 50
if mod2x3x i = 1 and isprim (i + 1) = 1
cnt += 1
write i + 1 & " "
.
i += 2
.
print ""
print ""
i = 4
write 2 & " "
cnt = 1
while cnt < 50
if mod2x3x i = 1 and isprim (i - 1) = 1
cnt += 1
write i - 1 & " "
.
i += 2
.
</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|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br>
<
// Pierpont primes . Nigel Galloway: March 19th., 2021
let fN g=let mutable g=g in ((fun()->g),fun()->g<-g+g;())
Line 872 ⟶ 929:
pierpontT1|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
pierpontT2|>Seq.take 50|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 878 ⟶ 935:
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}}==
<
math.primes prettyprint sequences sorting ;
Line 907 ⟶ 965:
"250th Pierpont prime of the second kind: " write
249 second nth .
]</
{{out}}
<pre>
Line 930 ⟶ 988:
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define NPP 50
Function isPrime(Byval n As Ulongint) As Boolean
End Function
End Function
Function isPierpont(n As Uinteger) As Uinteger
End Function
Dim As Uinteger x = 1, j
While np(1) <= NPP Or np(2) <= NPP
If j Mod 2 = 1 Then
np(1) += 1
np(2) += 1
Wend
If j Mod 10 = 0 Then Print
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 992 ⟶ 1,073:
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.
<
import (
Line 1,053 ⟶ 1,134:
fmt.Println("\n250th Pierpont prime of the first kind:", pp[249])
fmt.Println("\n250th Pierpont prime of the second kind:", pp2[249])
}</
{{out}}
Line 1,085 ⟶ 1,166:
These timings are for my Celeron @1.6GHz and should therefore be much faster on a more modern machine.
<
import (
Line 1,177 ⟶ 1,258:
elapsed := time.Now().Sub(start)
fmt.Printf("\nTook %s\n", elapsed)
}</
{{out}}
Line 1,213 ⟶ 1,294:
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]]
<
import Data.List (intercalate)
import Data.List.Split (chunksOf)
Line 1,253 ⟶ 1,334:
where
rows = chunksOf 10 . take 50
commas = reverse . intercalate "," . chunksOf 3 . reverse . show</
{{out}}
<pre>
Line 1,276 ⟶ 1,357:
./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}}==
<
import java.math.BigInteger;
import java.text.NumberFormat;
Line 1,344 ⟶ 1,449:
}
</syntaxhighlight>
{{out}}
Line 1,383 ⟶ 1,488:
'''Preliminaries'''
<
. as $n
| if ($n < 2) then false
Line 1,407 ⟶ 1,512:
def table($ncols; $colwidth):
nwise($ncols) | map(lpad($colwidth)) | join(" ");</
'''Pierpont primes'''
<
# 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),
Line 1,473 ⟶ 1,578:
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))</
{{out}}
<pre>
Line 1,493 ⟶ 1,598:
=={{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.
<
function pierponts(N, firstkind = true)
Line 1,525 ⟶ 1,630:
println("\nThe 2000th Pierpont prime (second kind) is: ", pierponts(2000, false)[2000])
</
<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,546 ⟶ 1,651:
=={{header|Kotlin}}==
{{trans|Go}}
<
import kotlin.math.min
Line 1,623 ⟶ 1,728:
println("\n2000th Pierpont prime of the first kind: ${p[0][1999]}")
println("\n2000th Pierpont prime of the first kind: ${p[1][1999]}")
}</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,652 ⟶ 1,757:
=={{header|Lua}}==
<
if n < 2 then return false end
if n % 2 == 0 then return n==2 end
Line 1,701 ⟶ 1,806:
for i, p in ipairs(p2) do
io.write(string.format("%8d%s", p, (i%10==0 and "\n" or " ")))
end</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,719 ⟶ 1,824:
=={{header|M2000 Interpreter}}==
{{trans|freebasic}}
<syntaxhighlight lang="m2000 interpreter">Module Pierpoint_Primes {
Form 80
Set Fast !
Line 1,776 ⟶ 1,879:
end function
}
Pierpoint_Primes</syntaxhighlight>
{{out}}
Line 1,797 ⟶ 1,899:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
FindUpToMax[max_Integer, b_Integer] := Module[{res, num},
res = {};
Line 1,823 ⟶ 1,925:
Part[FindUpToMax[10^130, +1], 1000]
Print["1000th Piermont prime of the second kind:"]
Part[FindUpToMax[10^150, -1], 1000]</
{{out}}
<pre>Piermont primes of the first kind:
Line 1,840 ⟶ 1,942:
=={{header|Nim}}==
<
func isPrime(n: int): bool =
Line 1,896 ⟶ 1,998:
stdout.write ($n).align(9)
inc count
if count mod 10 == 0: stdout.write '\n'</
{{out}}
Line 1,916 ⟶ 2,018:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 1,968 ⟶ 2,070:
say "\n250th Pierpont prime of the first kind: " . $pierpont_1st[249];
say "\n250th Pierpont prime of the second kind: " . $pierpont_2nd[249];</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 1,992 ⟶ 2,094:
{{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.
<!--<
<span style="color: #000080;font-style:italic;">-- demo/rosetta/Pierpont_primes.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,060 ⟶ 2,162:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"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>
<!--</
{{out}}
<pre>
Line 2,090 ⟶ 2,192:
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
?- use_module(library(heaps)).
Line 2,179 ⟶ 2,281:
?- main.
</syntaxhighlight>
{{Out}}
<pre>
Line 2,190 ⟶ 2,292:
=={{header|Python}}==
{{trans|Go}}
<
# Copied from https://rosettacode.org/wiki/Miller-Rabin_primality_test#Python
Line 2,267 ⟶ 2,369:
print "250th Pierpont prime of the second kind:", pp2[249]
main()</
{{out}}
<pre>First 50 Pierpont primes of the first kind:
Line 2,283 ⟶ 2,385:
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 2,294 ⟶ 2,442:
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"
sub pierpont ($kind is copy = 1) {
Line 2,328 ⟶ 2,476:
say "\n250th Pierpont prime of the first kind: " ~ pierpont[249];
say "\n250th Pierpont prime of the second kind: " ~ pierpont(2)[249];</
{{out}}
Line 2,354 ⟶ 2,502:
(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"
cache my \Smooth := gather {
my %i = (flat @list) Z=> (Smooth.iterator for ^@list);
Line 2,382 ⟶ 2,530:
"\nOEIS: A077314:", (2,7), -1, 20,
"\nOEIS: A174144 - (\"Humble\" primes 1st):", (2,3,5,7), 1, 20,
"\nOEIS:
"\nOEIS: A077499:", (2,11), 1, 20,
"\nOEIS: A077315:", (2,11), -1, 20,
Line 2,392 ⟶ 2,540:
say "$title smooth \{$primes\} {$add > 0 ?? '+' !! '-'} 1 ";
put smooth-numbers(|$primes).map( * + $add ).grep( *.is-prime )[^$count]
}</
{{Out}}
Line 2,446 ⟶ 2,594:
The REXX language has a "big num" capability to handle almost any amount of decimal digits, but
<br>it lacks a robust '''isPrime''' function. Without that, verifying very large primes is problematic.
<
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
Line 2,481 ⟶ 2,629:
_= pu*pv + t; if _ >s then m= min(_, m)
end /*jv*/
end /*ju*/ /*see the RETURN (above). */</
{{out|output|text= when using the default input:}}
<pre>
Line 2,500 ⟶ 2,648:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,557 ⟶ 2,705:
see "done2..." + nl
</syntaxhighlight>
Output:
<pre>
Line 2,670 ⟶ 2,818:
=={{header|Ruby}}==
<
def smooth_generator(ar)
Line 2,700 ⟶ 2,848:
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:
Line 2,719 ⟶ 2,867:
</pre>
=={{header|Sidef}}==
<
var s = primes.len.of { [1] }
{
Line 2,747 ⟶ 2,895:
say "\n#{n}th Pierpont prime of the 1st kind: #{p}"
say "#{n}th Pierpont prime of the 2nd kind: #{q}"
}</
{{out}}
<pre>
Line 2,772 ⟶ 2,920:
Using AttaSwift's BigInt library.
<
import Foundation
Line 2,828 ⟶ 2,976:
print()
print("250th Pierpoint prime of the first kind: \(primes.first[249])")
print("250th Pierpoint prime of the second kind: \(primes.second[249])")</
{{out}}
Line 2,842 ⟶ 2,990:
{{trans|Go}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
The 3-smooth version. Just the first 250 - a tolerable 14 seconds or so on my machine.
<
import "./
var pierpont = Fn.new { |n, first|
Line 2,887 ⟶ 3,033:
count2 = count2 + 1
}
count =
}
}
Line 2,893 ⟶ 3,039:
}
var p = pierpont.call(250, true)
System.print("First 50 Pierpont primes of the first kind:")
Line 2,907 ⟶ 3,052:
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>
{{out}}
Line 2,929 ⟶ 3,073:
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,935 ⟶ 3,139:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
Using GMP's probabilistic primes makes it is easy and fast to test for primeness.
<
var [const] one=BI(1), two=BI(2), three=BI(3);
Line 2,963 ⟶ 3,167:
}
return(pps1,pps2)
}</
<
println("The first 50 Pierpont primes (first kind):");
Line 2,975 ⟶ 3,179:
println("\n%4dth Pierpont prime, first kind: ".fmt(n), pps1[n-1]);
println( " second kind: ", pps2[n-1]);
}</
{{out}}
<pre style="font-size:83%">
|