Sexy primes: Difference between revisions

m (Added Delphi reference to Pascal code)
Line 1,275:
1 quintuplets, ending with: (5 11 17 23 29)
48627 unsexy, ending with: 999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003</pre>
 
=={{header|Lua}}==
{{trans|Kotlin}}
This Nim version uses Kotlin algorithm with several differences. In particular, we have chosen to store only the first term of groups as others can be retrieved by computation. But it complicates somewhat the printing of results.
 
<lang Nim>import math, strformat, strutils
 
const Lim = 1_000_035
 
type Group {.pure.} = enum # "ord" gives the number of terms.
Unsexy = (1, "unsexy primes")
Pairs = (2, "sexy prime pairs")
Triplets = (3, "sexy prime triplets")
Quadruplets = (4, "sexy prime quadruplets")
Quintuplets = (5, "sexy prime quintuplets")
 
 
# Sieve of Erathosthenes.
var composite: array[1..Lim, bool] # Default is false.
composite[1] = true
for p in countup(3, sqrt(Lim.toFloat).int, 2): # Ignore even numbers.
if not composite[p]:
for k in countup(p * p, Lim, p):
composite[k] = true
 
template isPrime(n: int): bool = not composite[n]
 
 
proc expandGroup(n: int; group: Group): string =
## Given the first term of a group, return the full group
## representation as a string.
var n = n
for _ in 1..ord(group):
result.addSep(", ")
result.add $n
inc n, 6
if group != Unsexy: result = '(' & result & ')'
 
 
proc printResult(group: Group; values: seq[int]; count: int) =
## Print a result.
 
echo &"\nNumber of {group} less than {Lim}: {values.len}"
let last = min(values.len, count)
let verb = if last == 1: "is" else: "are"
echo &"The last {last} {verb}:"
 
var line = ""
for i in countdown(last, 1):
line.addSep(", ")
line.add expandGroup(values[^i], group)
echo " ", line
 
 
var
pairs, trips, quads, quints: seq[int] # Keep only the first prime of the group.
unsexy = @[2, 3]
 
for n in countup(3, Lim, 2):
if composite[n]: continue
 
if n in 7..(Lim - 8) and composite[n - 6] and composite[n + 6]:
unsexy.add n
continue
 
if n < Lim - 6 and isPrime(n + 6):
pairs.add n
else: continue
 
if n < Lim - 12 and isPrime(n + 12):
trips.add n
else: continue
 
if n < Lim - 18 and isPrime(n + 18):
quads.add n
else: continue
 
if n < Lim - 24 and isPrime(n + 24):
quints.add n
 
printResult(Pairs, pairs, 5)
printResult(Triplets, trips, 5)
printResult(Quadruplets, quads, 5)
printResult(Quintuplets, quints, 5)
printResult(Unsexy, unsexy, 10)</lang>
 
{{out}}
<pre>Number of sexy prime pairs less than 1000035: 16386
The last 5 are:
(999371, 999377), (999431, 999437), (999721, 999727), (999763, 999769), (999953, 999959)
 
Number of sexy prime triplets less than 1000035: 2900
The last 5 are:
(997427, 997433, 997439), (997541, 997547, 997553), (998071, 998077, 998083), (998617, 998623, 998629), (998737, 998743, 998749)
 
Number of sexy prime quadruplets less than 1000035: 325
The last 5 are:
(977351, 977357, 977363, 977369), (983771, 983777, 983783, 983789), (986131, 986137, 986143, 986149), (990371, 990377, 990383, 990389), (997091, 997097, 997103, 997109)
 
Number of sexy prime quintuplets less than 1000035: 1
The last 1 is:
(5, 11, 17, 23, 29)
 
Number of unsexy primes less than 1000035: 48627
The last 10 are:
999853, 999863, 999883, 999907, 999917, 999931, 999961, 999979, 999983, 1000003</pre>
 
=={{header|Pascal}}==
Anonymous user