Sum of two adjacent numbers are primes: Difference between revisions

Added Easylang
(Added Algol W)
(Added Easylang)
 
(12 intermediate revisions by 8 users not shown)
Line 3:
;Task:
<br>
Show on this page the first 20 numbersprime and sumsums of two adjacentconsecutive numbers which sum is primeintegers.
;Extra credit
Show the ten millionth such prime sum.
Line 61:
 
=={{header|ALGOL 68}}==
Using Pete Lomax's observation that all odd primes are n + n+1 for some n - see [[#Phix]]
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find the first 20 primes which are n + ( n + 1 ) for some n #
Line 66 ⟶ 67:
[]BOOL prime = PRIMESIEVE 200; # should be enough primes #
INT p count := 0;
FOR np FROM 3 BY 2 WHILE p count < 20 DO
INT n1 = n + 1;
INT p = n + n1;
IF prime[ p ] THEN
INT n = p OVER 2;
INT n1 = n + 1;
p count +:= 1;
print( ( whole( n, -2 ), " + ", whole( n1, -2 ), " = ", whole( p, -3 ), newline ) )
FI
OD
END
END</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 166 ⟶ 168:
20 : 36 + 37 = 73
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">loop.with:'i
select.first:20 1..∞ 'x -> prime? x + x+1
'x -> print [
(pad to :string i+1 2)++":"
(pad to :string x 2) "+" (pad.right to :string x+1 2) "=" x + x+1
]</syntaxhighlight>
 
{{out}}
 
<pre> 1: 1 + 2 = 3
2: 2 + 3 = 5
3: 3 + 4 = 7
4: 5 + 6 = 11
5: 6 + 7 = 13
6: 8 + 9 = 17
7: 9 + 10 = 19
8: 11 + 12 = 23
9: 14 + 15 = 29
10: 15 + 16 = 31
11: 18 + 19 = 37
12: 20 + 21 = 41
13: 21 + 22 = 43
14: 23 + 24 = 47
15: 26 + 27 = 53
16: 29 + 30 = 59
17: 30 + 31 = 61
18: 33 + 34 = 67
19: 35 + 36 = 71
20: 36 + 37 = 73</pre>
 
=={{header|AWK}}==
Line 534 ⟶ 568:
89712345 + 89712346 = 179424691
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
procedure AdjacentPrimeSums(Memo: TMemo);
var I,Sum,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=0 to High(I) do
if IsPrime(I+I+1) then
begin
Inc(Cnt);
Memo.Lines.Add(Format('%3d %3d+%3d=%4d',[Cnt,I,I+1,I+I+1]));
if Cnt>=20 then break
end;
end;
 
 
 
 
</syntaxhighlight>
{{out}}
<pre>
1: 1 + 2 = 3
2: 2 + 3 = 5
3: 3 + 4 = 7
4: 5 + 6 = 11
5: 6 + 7 = 13
6: 8 + 9 = 17
7: 9 + 10 = 19
8: 11 + 12 = 23
9: 14 + 15 = 29
10: 15 + 16 = 31
11: 18 + 19 = 37
12: 20 + 21 = 41
13: 21 + 22 = 43
14: 23 + 24 = 47
15: 26 + 27 = 53
16: 29 + 30 = 59
17: 30 + 31 = 61
18: 33 + 34 = 67
19: 35 + 36 = 71
20: 36 + 37 = 73
 
Elapsed Time: 25.093 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
<syntaxhighlight>
fastfunc isprim num .
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
print "The first 20 pairs of numbers whose sum is prime:"
repeat
n += 1
sum = 2 * n + 1
if isprim sum = 1
print n & " + " & n + 1 & " = " & sum
cnt += 1
.
until cnt = 20
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 648 ⟶ 761:
89712345 + 89712346 = 179424691
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import Data.Numbers.Primes
 
primeSumsOfConsecutiveNumbers :: Integral a => [(a, (a, a))]
primeSumsOfConsecutiveNumbers =
let go a b = [(n, (a, b)) | let n = a + b, isPrime n]
in concat $ zipWith go [1 ..] [2 ..]
 
main :: IO ()
main = mapM_ print $ take 20 primeSumsOfConsecutiveNumbers</syntaxhighlight>
{{Out}}
<pre>(3,(1,2))
(5,(2,3))
(7,(3,4))
(11,(5,6))
(13,(6,7))
(17,(8,9))
(19,(9,10))
(23,(11,12))
(29,(14,15))
(31,(15,16))
(37,(18,19))
(41,(20,21))
(43,(21,22))
(47,(23,24))
(53,(26,27))
(59,(29,30))
(61,(30,31))
(67,(33,34))
(71,(35,36))
(73,(36,37))</pre>
 
=={{header|J}}==
Line 799 ⟶ 944:
35 + 36 = 71
36 + 37 = 73
89712345 + 89712346 = 179424691</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/[bitops, math, strformat, strutils]
 
type Sieve = object
data: seq[byte]
 
func `[]`(sieve: Sieve; idx: Positive): bool =
## Return value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
result = sieve.data[iByte].testBit(iBit)
 
func `[]=`(sieve: var Sieve; idx: Positive; val: bool) =
## Set value of element at index "idx".
let idx = idx shr 1
let iByte = idx shr 3
let iBit = idx and 7
if val: sieve.data[iByte].setBit(iBit)
else: sieve.data[iByte].clearBit(iBit)
 
func newSieve(lim: Positive): Sieve =
## Create a sieve with given maximal index.
result.data = newSeq[byte]((lim + 16) shr 4)
 
func initPrimes(lim: Positive): seq[Natural] =
## Initialize the list of primes from 3 to "lim".
var composite = newSieve(lim)
composite[1] = true
for n in countup(3, sqrt(lim.toFloat).int, 2):
if not composite[n]:
for k in countup(n * n, lim, 2 * n):
composite[k] = true
for n in countup(3, lim, 2):
if not composite[n]:
result.add n
 
let primes = initPrimes(200_000_000)
 
echo "The first 20 pairs of natural numbers whose sum is prime are:"
var count = 0
for p in primes:
inc count
if count <= 20:
echo &"{(p-1) div 2} + {(p+1) div 2} = {p}"
if count == 20: echo()
elif count == 10_000_000:
echo "The 10 millionth such pair is:"
echo &"{(p-1) div 2} + {(p+1) div 2} = {p}"
break
</syntaxhighlight>
 
{{out}}
<pre>The first 20 pairs of natural numbers whose sum is prime are:
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
 
The 10 millionth such pair is:
89712345 + 89712346 = 179424691</pre>
 
Line 966 ⟶ 1,189:
 
=={{header|Python}}==
===Procedural===
<syntaxhighlight lang="python">#!/usr/bin/python
 
Line 1,010 ⟶ 1,234:
35 + 36 = 71
36 + 37 = 73</pre>
 
===Functional===
<syntaxhighlight lang="python">'''Prime sums of two consecutive integers'''
 
from itertools import chain, count, islice
 
 
# primeSumsOfTwoConsecutiveIntegers :: [Int]
def primeSumsOfTwoConsecutiveIntegers():
'''Infinite series of prime sums of
two consecutive integers.
'''
def go(a, b):
n = a + b
return [(n, (a, b))] if isPrime(n) else []
 
return chain.from_iterable(
map(go, count(1), count(2))
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 20 prime sums of two consecutive integers.'''
print(
'\n'.join([
f'{n} = {a} + {b}' for n, (a, b) in islice(
primeSumsOfTwoConsecutiveIntegers(),
20
)
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
 
def p(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
 
 
or as a list comprehension (the walrus operator requires Python 3.8+)
 
<syntaxhighlight lang="python">'''Prime sums of two consecutive integers'''
 
from itertools import count, islice
 
 
# primeSumsOfTwoConsecutiveIntegers :: [Int]
def primeSumsOfTwoConsecutiveIntegers():
'''As a list comprehension.'''
return (
(n, (a, b))
for a in count(1)
if isPrime(n := a + (b := 1 + a))
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First 20 prime sums of two consecutive integers.'''
print(
'\n'.join([
f'{n} = {a} + {b}' for n, (a, b) in islice(
primeSumsOfTwoConsecutiveIntegers(),
20
)
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# isPrime :: Int -> Bool
def isPrime(n):
'''True if n is prime.'''
if n in (2, 3):
return True
if 2 > n or 0 == n % 2:
return False
if 9 > n:
return True
if 0 == n % 3:
return False
 
def p(x):
return 0 == n % x or 0 == n % (2 + x)
 
return not any(map(p, range(5, 1 + int(n ** 0.5), 6)))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
 
{{Out}}
<pre>3 = 1 + 2
5 = 2 + 3
7 = 3 + 4
11 = 5 + 6
13 = 6 + 7
17 = 8 + 9
19 = 9 + 10
23 = 11 + 12
29 = 14 + 15
31 = 15 + 16
37 = 18 + 19
41 = 20 + 21
43 = 21 + 22
47 = 23 + 24
53 = 26 + 27
59 = 29 + 30
61 = 30 + 31
67 = 33 + 34
71 = 35 + 36
73 = 36 + 37</pre>
 
=={{header|Quackery}}==
Line 1,138 ⟶ 1,498:
36 + 37 = 73
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ { } 2
'''WHILE''' OVER SIZE 20 < '''REPEAT'''
NEXTPRIME
DUP 2 IQUOT →STR
LASTARG SWAP "+" + SWAP 1 + + "=" + OVER +
ROT SWAP + SWAP
'''END''' DROP
≫ 'TASK' STO
{{out}}
<pre>
1: {"1+2=3" "2+3=5" "3+4=7" "5+6=11" "6+7=13" "8+9=17" "9+10=19" "11+12=23" "14+15=29" "15+16=31" "18+19=37" "20+21=41" "21+22=43" "23+24=47" "26+27=53" "29+30=59" "30+31=61" "33+34=67" "35+36=71" "36+37=73"}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
primes = Prime.each
primes.next # skip 2
primes.first(20).each{|pr| puts "%3d + %3d = %3d" % [pr/2, pr/2+1, pr]}
</syntaxhighlight>
{{out}}
<pre>
1 + 2 = 3
2 + 3 = 5
3 + 4 = 7
5 + 6 = 11
6 + 7 = 13
8 + 9 = 17
9 + 10 = 19
11 + 12 = 23
14 + 15 = 29
15 + 16 = 31
18 + 19 = 37
20 + 21 = 41
21 + 22 = 43
23 + 24 = 47
26 + 27 = 53
29 + 30 = 59
30 + 31 = 61
33 + 34 = 67
35 + 36 = 71
36 + 37 = 73
</pre>
 
Line 1,176 ⟶ 1,582:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
2,046

edits