Pierpont primes: Difference between revisions

no edit summary
No edit summary
Line 940:
Took 1m40.781726122s
</pre>
=={{header|Haskell}}==
Uses arithmoi Library: https://hackage.haskell.org/package/arithmoi-0.11.0.0
<lang haskell>import Control.Monad (guard)
import Data.List (intercalate)
import Data.List.Split (chunksOf)
import Math.NumberTheory.Primes (Prime(..), unPrime, nextPrime)
import Math.NumberTheory.Primes.Testing (isPrime)
import Text.Printf (printf)
 
data PierPointKind = First | Second
 
merge :: Ord a => [a] -> [a] -> [a]
merge [] b = b
merge a@(x:xs) b@(y:ys) | x < y = x : merge xs b
| otherwise = y : merge a ys
 
nSmooth :: Integer -> [Integer]
nSmooth p = 1 : foldr u [] factors
where
factors = takeWhile (<=p) primes
primes = map unPrime [nextPrime 1..]
u n s = r
where
r = merge s (map (n*) (1:r))
 
pierpoints :: PierPointKind -> [Integer]
pierpoints k = do
n <- nSmooth 3
let x = case k of First -> n + 1
Second -> n - 1
guard (isPrime x)
[x]
 
main :: IO ()
main = do
printf "\nFirst 50 Pierpont primes of the first kind:\n"
mapM_ (\row -> mapM_ (printf "%12s" . commas) row >> printf "\n") (rows $ pierpoints First)
printf "\nFirst 50 Pierpont primes of the second kind:\n"
mapM_ (\row -> mapM_ (printf "%12s" . commas) row >> printf "\n") (rows $ pierpoints Second)
printf "\n250th Pierpont prime of the first kind: %s\n" (commas $ pierpoints First !! 249)
printf "\n250th Pierpont prime of the second kind: %s\n\n" (commas $ pierpoints Second !! 249)
where
rows = chunksOf 10 . take 50
commas = reverse . intercalate "," . chunksOf 3 . reverse . show</lang>
{{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 1,153 1,297
1,459 2,593 2,917 3,457 3,889 10,369 12,289 17,497 18,433 39,367
52,489 65,537 139,969 147,457 209,953 331,777 472,393 629,857 746,497 786,433
839,809 995,329 1,179,649 1,492,993 1,769,473 1,990,657 2,654,209 5,038,849 5,308,417 8,503,057
 
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 1,151
2,591 4,373 6,143 6,911 8,191 8,747 13,121 15,551 23,327 27,647
62,207 73,727 131,071 139,967 165,887 294,911 314,927 442,367 472,391 497,663
524,287 786,431 995,327 1,062,881 2,519,423 10,616,831 17,915,903 18,874,367 25,509,167 30,233,087
 
250th Pierpont prime of the first kind: 62,518,864,539,857,068,333,550,694,039,553
 
250th Pierpont prime of the second kind: 4,111,131,172,000,956,525,894,875,083,702,271
 
./pierpoints 0.04s user 0.01s system 20% cpu 0.215 total
</pre>
=={{header|Java}}==
<lang java>
Anonymous user