Sum of two adjacent numbers are primes: Difference between revisions
Sum of two adjacent numbers are primes (view source)
Revision as of 17:51, 28 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 10:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
PR read "primes.incl.a68" PR # include prime utilities #
[]BOOL prime = PRIMESIEVE 200; # should be enough primes #
Line 22:
FI
OD
END</
{{out}}
<pre>
Line 47:
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SUM_OF_TWO_ADJACENT_NUMBERS_ARE_PRIMES.AWK
BEGIN {
Line 75:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 105:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<
if v < 2 then return False
if v mod 2 = 0 then return v = 2
Line 132:
end if
end while
end</
{{out}}
<pre>
Line 139:
==={{header|FreeBASIC}}===
<
If ValorEval <= 1 Then Return False
For i As Integer = 2 To Int(Sqr(ValorEval))
Line 161:
End If
Loop
Sleep</
{{out}}
<pre>The first 20 pairs of numbers whose sum is prime:
Line 186:
==={{header|PureBasic}}===
<
If v <= 1 : ProcedureReturn #False
ElseIf v < 4 : ProcedureReturn #True
Line 222:
ForEver
CloseConsole()
EndIf</
{{out}}
<pre>
Line 232:
{{works with|QuickBasic|4.5}}
{{trans|FreeBASIC}}
<
IF ValorEval <= 1 THEN isPrime = 0
FOR i = 2 TO INT(SQR(ValorEval))
Line 255:
END IF
LOOP
END</
{{out}}
<pre>
Line 262:
==={{header|True BASIC}}===
<
IF ValorEval <= 1 THEN LET isPrime = 0
FOR i = 2 TO INT(SQR(ValorEval))
Line 289:
END IF
LOOP
END</
{{out}}
<pre>
Line 296:
==={{header|Yabasic}}===
<
if v < 2 then return False : fi
if mod(v, 2) = 0 then return v = 2 : fi
Line 322:
end if
loop
end</
{{out}}
<pre>
Line 332:
=={{header|C}}==
{{trans|Go}}
<
#include <stdlib.h>
#include <math.h>
Line 385:
free(primes);
return 0;
}</
{{out}}
Line 417:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<
// 2n+1 is prime. Nigel Galloway: Januuary 22nd., 2022
primes32()|>Seq.skip 1|>Seq.take 20|>Seq.map(fun n->n/2)|>Seq.iteri(fun n g->printfn "%2d: %2d + %2d=%d" (n+1) g (g+1) (g+g+1))
</syntaxhighlight>
{{out}}
<pre>
Line 447:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
math.primes.lists sequences ;
20 lprimes cdr [ 2/ dup 1 + 2array ] lmap-lazy ltake
[ dup sum suffix "%d + %d = %d\n" vprintf ] leach</
{{out}}
<pre>
Line 478:
=={{header|Go}}==
{{libheader|Go-rcu}}
<
import (
Line 499:
hp := p / 2
fmt.Printf("%2d + %2d = %2d\n", hp, hp+1, p)
}</
{{out}}
Line 533:
Here, we generate the first 20 odd primes, divide by 2, drop the fractional part and add 0 and 1 to that value. Then we format that pair of numbers with their sum and with decorations indicating the relevant arithmetic:
<
3=1+2
5=2+3
Line 553:
67=33+34
71=35+36
73=36+37</
=={{header|jq}}==
Line 562:
and so a naive approach to computing the first 20 numbers satisfying
the condition is appropriate.
<
. as $n
| if ($n < 2) then false
Line 586:
| "\($i) + \($i + 1) = \($q)" );
naive</
{{out}}
<pre>
Line 612:
=={{header|Julia}}==
<
using Primes
Line 630:
println("Ten millionth: $i + $(i + 1) = $(i + i + 1)")
end
</
<pre>
1 + 2 = 3
Line 656:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Row[{Floor[#/2], " + ", Ceiling[#/2], " = ", #}] &[Prime[10^7 + 1]]</
{{out}}
<pre>1 + 2 = 3
Line 683:
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use ntheory 'is_prime';
my($n,$c);
while () { is_prime(1 + 2*++$n) and printf "%2d + %2d = %2d\n", $n, $n+1, 1+2*$n and ++$c == 20 and last }</
{{out}}
<pre> 1 + 2 = 3
Line 713:
=={{header|Phix}}==
''Every'' prime p greater than 2 is odd, hence p/2 is k.5 and the adjacent numbers needed are k and k+1. DOH.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">doh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
Line 720:
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">21</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$],</span><span style="color: #000000;">doh</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">doh</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 748:
=={{header|Python}}==
<
def isPrime(n):
Line 769:
print('{:2}'.format(n), "+", '{:2}'.format(n+1), "=", '{:2}'.format(suma))
else:
break</
{{out}}
<pre>The first 20 pairs of numbers whose sum is prime:
Line 795:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my @wanted = @n-n1-triangular.grep: *.[2].is-prime;
printf "%2d + %2d = %2d\n", |.list for @wanted.head(20);</
{{out}}
<pre>
Line 825:
=={{header|Ring}}==
<
load "stdlibcore.ring"
see "working..." + nl
Line 845:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 873:
=={{header|Sidef}}==
<
.grep { .tail.is_prime })
wanted.first(20).each_2d {|a,b,c|
printf("%2d + %2d = %2d\n", a,b,c)
}</
{{out}}
Line 908:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
Line 922:
var p = primes[1e7]
var hp = (p/2).floor
Fmt.print("$2d + $2d = $2d", hp, hp + 1, p)</
{{out}}
Line 954:
=={{header|XPL0}}==
{{trans|Ring}}
<syntaxhighlight lang="xpl0">
include xpllib;
int N, Num, Sum;
Line 972:
];
Text(0, "Done...^M^J");
]</
{{out}}
|