Sum of two adjacent numbers are primes: Difference between revisions

Added Easylang
(Added Easylang)
 
(6 intermediate revisions by 6 users not shown)
Line 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 536 ⟶ 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 833 ⟶ 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 1,309 ⟶ 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,347 ⟶ 1,582:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
2,054

edits