Jump to content

Cousin primes: Difference between revisions

(→‎{{header|ALGOL W}}: spelling... : ()
Line 959:
967: 971
TOTAL COUSINS: 41</pre>
 
=={{header|Nim}}==
<lang Nim>import sets, strutils, sugar
 
const N = 1000
 
func isPrime(n: Positive): bool {.compileTime.} =
if (n and 1) == 0: return n == 2
var m = 3
while m * m <= n:
if n mod m == 0: return false
inc m, 2
result = true
 
const
PrimeList = collect:
for n in 2..N:
if n.isPrime: n
PrimeSet = PrimeList.toHashSet
 
let cousinList = collect:
for n in PrimeList:
if (n + 4) in PrimeSet: (n, n + 4)
 
echo "Found $# cousin primes less than $#:".format(cousinList.len, N)
for i, cousins in cousinList:
stdout.write ($cousins).center(10)
stdout.write if (i+1) mod 7 == 0: '\n' else: ' '
echo()</lang>
 
{{out}}
<pre>Found 41 cousin primes less than 1000:
(3, 7) (7, 11) (13, 17) (19, 23) (37, 41) (43, 47) (67, 71)
(79, 83) (97, 101) (103, 107) (109, 113) (127, 131) (163, 167) (193, 197)
(223, 227) (229, 233) (277, 281) (307, 311) (313, 317) (349, 353) (379, 383)
(397, 401) (439, 443) (457, 461) (463, 467) (487, 491) (499, 503) (613, 617)
(643, 647) (673, 677) (739, 743) (757, 761) (769, 773) (823, 827) (853, 857)
(859, 863) (877, 881) (883, 887) (907, 911) (937, 941) (967, 971) </pre>
 
=={{header|Pascal}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.