Even numbers which cannot be expressed as the sum of two twin primes: Difference between revisions

Added various BASIC dialects (BASIC256, FreeBASIC and Yabasic)
m (Added the solution to the task in the Wolfram Language)
(Added various BASIC dialects (BASIC256, FreeBASIC and Yabasic))
 
(6 intermediate revisions by 6 users not shown)
Line 31:
 
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F list_primality(n)
V result = [1B] * (n + 1)
result[0] = result[1] = 0B
L(i) 0 .. Int(sqrt(n))
I result[i]
L(j) (i * i .< result.len).step(i)
result[j] = 0B
R result
 
F nonpairsums(include1 = 0B, limit = 20'000)
V prim = list_primality(limit + 2)
 
‘ Even numbers which cannot be expressed as the sum of two twin primes ’
V tpri = (0 .< limit + 2).map(i -> @prim[i] & (@prim[i - 2] | @prim[i + 2]))
I include1
tpri[1] = 1B
V twinsums = [0B] * (limit * 2)
L(i) 0 .< limit
L(j) 0 .. limit - i
I tpri[i] & tpri[j]
twinsums[i + j] = 1B
 
R (2 .. limit).step(2).filter(i -> !@twinsums[i])
 
print(‘Non twin prime sums:’)
L(p) nonpairsums()
V k = L.index
print(f:‘{p:6}’, end' I (k + 1) % 10 == 0 {"\n"} E ‘’)
 
print("\n\nNon twin prime sums (including 1):")
L(p) nonpairsums(include1' 1B)
V k = L.index
print(f:‘{p:6}’, end' I (k + 1) % 10 == 0 {"\n"} E ‘’)
</syntaxhighlight>
 
{{out}}
<pre>
Non twin prime sums:
2 4 94 96 98 400 402 404 514 516
518 784 786 788 904 906 908 1114 1116 1118
1144 1146 1148 1264 1266 1268 1354 1356 1358 3244
3246 3248 4204 4206 4208
 
Non twin prime sums (including 1):
94 96 98 400 402 404 514 516 518 784
786 788 904 906 908 1114 1116 1118 1144 1146
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208
</pre>
 
=={{header|ALGOL 68}}==
Line 110 ⟶ 164:
Found 33
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">#include "isprime.kbs"
 
global limite
limite = 1e4
global TPTbl
dim TPTbl(limite)
global TPMax
 
i = 1
TPTbl[0] = 1
for n = 2 to limite
if isPrime(n) and isPrime(n+2) then
TPTbl[i] = n
i = i + 1
TPTbl[i] = n+2
i = i + 1
end if
next n
 
TPMax = i - 1
call Mostrar(1)
call Mostrar(0)
end
 
subroutine Mostrar(inicio)
dim ints(limite+1) fill 0
 
print "Non twin prime sums";
if inicio = 0 then print " (including 1 as a prime)";
print ":"
for i = inicio to TPMax
for j = i to TPMax
suma = TPTbl[i] + TPTbl[j]
if suma <= limite then
ints[suma] = 1
else
j = TPMax
end if
next j
next i
cnt = 0
for i = 2 to limite
if ints[i] = 0 then
print rjust(i, 5);
cnt += 1
if cnt mod 10 = 0 then print
end if
i = i + 1
next i
print : print "Found: "; cnt : print
end subroutine</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="vbnet">#include "isprime.bas"
 
Const limite = 1e4
Dim Shared As Uinteger TPTbl(limite)
Dim Shared As Uinteger TPMax
 
Sub Mostrar(inicio As Uinteger)
Dim As Uinteger i, j, suma, cnt
Dim As Uinteger ints(limite+1)
Print "Non twin prime sums";
If inicio = 0 Then Print " (including 1 as a prime)";
Print ":"
For i = inicio To TPMax
For j = i To TPMax
suma = TPTbl(i) + TPTbl(j)
If suma <= limite Then
ints(suma) = 1
Else
j = TPMax
End If
Next j
Next i
cnt = 0
For i = 2 To limite
If ints(i) = 0 Then
Print Using "#####"; i;
cnt += 1
If cnt Mod 10 = 0 Then Print
End If
i += 1
Next i
Print !"\nFound: "; cnt : Print
End Sub
 
Dim As Uinteger n, i = 1
TPTbl(0) = 1
For n = 2 To limite
If isPrime(n) Andalso isPrime(n+2) Then
TPTbl(i) = n
i += 1
TPTbl(i) = n+2
i += 1
End If
Next n
 
TPMax = i - 1
Mostrar(1)
Mostrar(0)
 
Sleep</syntaxhighlight>
{{out}}
<pre>Non twin prime sums:
2 4 94 96 98 400 402 404 514 516
518 784 786 788 904 906 908 1114 1116 1118
1144 1146 1148 1264 1266 1268 1354 1356 1358 3244
3246 3248 4204 4206 4208
Found: 35
 
Non twin prime sums (including 1 as a prime):
94 96 98 400 402 404 514 516 518 784
786 788 904 906 908 1114 1116 1118 1144 1146
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208
Found: 33</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">import isprime
 
limite = 10000
dim TPTbl(limite)
dim ints(limite+1)
 
TPTbl(0) = 1
i = 1
for n = 2 to limite
if isPrime(n) and isPrime(n+2) then
TPTbl(i) = n
i = i + 1
TPTbl(i) = n+2
i = i + 1
fi
next n
 
TPMax = i - 1
Mostrar(1)
Mostrar(0)
end
 
sub Mostrar(inicio)
local i, j, suma, cnt
print "Non twin prime sums";
if inicio = 0 print " (including 1 as a prime)";
print ":"
for i = inicio to TPMax
for j = i to TPMax
suma = TPTbl(i) + TPTbl(j)
if suma <= limite then
ints(suma) = 1
else
j = TPMax
fi
next j
next i
cnt = 0
for i = 2 to limite
if ints(i) = 0 then
print i using "#####";
cnt = cnt + 1
if mod(cnt, 10) = 0 print
fi
i = i + 1
next i
print "\nFound: ", cnt
print
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|C++}}==
Line 194 ⟶ 427:
Found 33
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
 
procedure ShowSumTwinPrimes(Memo: TMemo);
var Sieve: TPrimeSieve;
var Twins: TIntegerDynArray;
var S: string;
 
procedure GetTwinPrimes(WithOne: boolean);
{Build list of twin primes - include one, WithOne is true}
var I,P1,P2: integer;
begin
{Add one to list?}
if WithOne then
begin
SetLength(Twins,1);
Twins[0]:=1;
end
else SetLength(Twins,0);
{Look for numbers that P-1 or P+1 are two apart}
for I:=0 to Sieve.PrimeCount-1 do
if ((I>0) and ((Sieve.Primes[I]-Sieve.Primes[I-1]) = 2)) or
((I<(Sieve.PrimeCount-1)) and ((Sieve.Primes[I+1]-Sieve.Primes[I]) = 2)) then
begin
{Store twin}
SetLength(Twins,Length(Twins)+1);
Twins[High(Twins)]:=Sieve.Primes[I];
end;
end;
 
 
function IsNotTwinPrimeSum(N: integer): boolean;
{Test if number is NOT the sum of twin primes}
var I,J,Sum: integer;
begin
Result:=False;
{Test all combination of twin-prime sums}
for I:=0 to High(Twins) do
for J:=0 to High(Twins) do
begin
Sum:=Twins[I]+Twins[J];
if Sum>N then break;
if Sum=N then exit;
end;
Result:=True;
end;
 
 
function GetItems: string;
{Get first 5000 non twin prime sums}
var I,N,Cnt: integer;
begin
Result:=''; Cnt:=0;
for I:=1 to (5000 div 2)-1 do
begin
N:=I * 2;
if IsNotTwinPrimeSum(N) then
begin
Inc(Cnt);
Result:=Result+Format('%5d',[N]);
if (Cnt mod 10)=0 then Result:=Result+CRLF;
end;
end;
end;
 
begin
Sieve:=TPrimeSieve.Create;
try
Sieve.Intialize(1000000);
GetTwinPrimes(False);
Memo.Lines.Add('Non twin prime sums:');
Memo.Lines.Add(GetItems);
GetTwinPrimes(True);
Memo.Lines.Add('Non twin prime sums with one:');
Memo.Lines.Add(GetItems);
finally Sieve.Free; end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Non twin prime sums:
2 4 94 96 98 400 402 404 514 516
518 784 786 788 904 906 908 1114 1116 1118
1144 1146 1148 1264 1266 1268 1354 1356 1358 3244
3246 3248 4204 4206 4208
Non twin prime sums with one:
94 96 98 400 402 404 514 516 518 784
786 788 904 906 908 1114 1116 1118 1144 1146
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208
 
Elapsed Time: 37.563 ms.
</pre>
 
 
=={{header|Go}}==
Line 421 ⟶ 757:
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208
</pre>
 
=={{header|Nim}}==
{{trans|Wren}}
Instead of using a sieve to build the list of primes, we check directly if a number is prime.
<syntaxhighlight lang="Nim">import std/[strformat, sugar]
 
const Limit = 100_000
 
func isPrime(n: Natural): bool =
if n < 2: return false
if (n and 1) == 0: return n == 2
if n mod 3 == 0: return n == 3
var k = 5
var delta = 2
while k * k <= n:
if n mod k == 0: return false
inc k, delta
delta = 6 - delta
result = true
 
let primes = collect:
for n in countup(5, Limit, 2):
if n.isPrime: n
 
var twins = @[3]
for i in 0..<primes.high:
if primes[i + 1] - primes[i] == 2:
if twins[^1] != primes[i]:
twins.add primes[i]
twins.add primes[i + 1]
 
func nonTwinSums(twins: seq[int]): seq[int] =
var sieve = newSeq[bool](Limit + 1)
for i in 0..twins.high:
for j in i..twins.high:
let sum = twins[i] + twins[j]
if sum > Limit: break
sieve[sum] = true
var i = 2
while i <= Limit:
if not sieve[i]:
result.add i
inc i, 2
 
echo "Non twin prime sums:"
var ntps = nonTwinSums(twins)
for i, n in ntps:
stdout.write &"{n:4}"
stdout.write if i mod 10 == 9: '\n' else: ' '
echo &"\nFound {ntps.len}.\n"
 
echo "Non twin prime sums (including 1):"
twins.insert(1, 0)
ntps = nonTwinSums(twins)
for i, n in ntps:
stdout.write &"{n:4}"
stdout.write if i mod 10 == 9: '\n' else: ' '
echo &"\nFound {ntps.len}.\n"
</syntaxhighlight>
 
{{out}}
<pre>Non twin prime sums:
2 4 94 96 98 400 402 404 514 516
518 784 786 788 904 906 908 1114 1116 1118
1144 1146 1148 1264 1266 1268 1354 1356 1358 3244
3246 3248 4204 4206 4208
Found 35.
 
Non twin prime sums (including 1):
94 96 98 400 402 404 514 516 518 784
786 788 904 906 908 1114 1116 1118 1144 1146
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208
Found 33.
</pre>
 
Line 704 ⟶ 1,115:
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var limit = 1e4
 
func display(msg, sums) {
say msg
2..limit -> by(2).grep{ !sums.has(_) }.each_slice(10, {|*s|
say s.map{ '%4s' % _ }.join(' ')
})
}
 
var sums = Set()
var twins = primes(3, limit).cons(2).grep{ .diffs[0] == 2 }.flat.uniq
 
[twins, twins].cartesian{|a,b| sums << a+b }
display("Non twin prime sums:", sums)
 
sums << (2, twins.map{.inc}...)
display("\nNon twin prime sums (including 1):", sums)</syntaxhighlight>
{{out}}
<pre>
Non twin prime sums:
2 4 94 96 98 400 402 404 514 516
518 784 786 788 904 906 908 1114 1116 1118
1144 1146 1148 1264 1266 1268 1354 1356 1358 3244
3246 3248 4204 4206 4208
 
Non twin prime sums (including 1):
94 96 98 400 402 404 514 516 518 784
786 788 904 906 908 1114 1116 1118 1144 1146
1148 1264 1266 1268 1354 1356 1358 3244 3246 3248
4204 4206 4208
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./fmt" for Fmt
 
Line 849 ⟶ 1,293:
StringTemplate["Non twin prime sums (including 1 as a prime): `list`, found `length`"][<|"list" -> ListOfNonTwinPrimeSumsWith1, "length" -> Length[ListOfNonTwinPrimeSumsWith1]|>]</syntaxhighlight>
 
{{out}}<pre>Non twin prime sums: {2, 4, 94, 96, 98, 400, 402, 404, 514, 516, \518, 784, 786, 788, 904, 906, 908, 1114, 1116, 1118, 1144, 1146, 1148, 1264, 1266, 1268, 1354, 1356, 1358, 3244, 3246, 3248, 4204, 4206, 4208}, found 35
518, 784, 786, 788, 904, 906, 908, 1114, 1116, 1118, 1144, 1146, \
1148, 1264, 1266, 1268, 1354, 1356, 1358, 3244, 3246, 3248, 4204, \
4206, 4208}, found 35
 
Non twin prime sums (including 1 as a prime): {94, 96, 98, 400, 402, \404, 514, 516, 518, 784, 786, 788, 904, 906, 908, 1114, 1116, 1118, 1144, 1146, 1148, 1264, 1266, 1268, 1354, 1356, 1358, 3244, 3246, 3248, 4204, 4206, 4208}, found 33</pre>
404, 514, 516, 518, 784, 786, 788, 904, 906, 908, 1114, 1116, 1118, \
1144, 1146, 1148, 1264, 1266, 1268, 1354, 1356, 1358, 3244, 3246, \
3248, 4204, 4206, 4208}, found 33</pre>
2,130

edits