Triplet of three numbers: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 8:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V n = 6000
V p = [0B] * 6000
 
Line 20:
L.continue
E
print(f:‘{i:4}: {i - 1:4} {i + 3:4} {i + 5:4}’)</langsyntaxhighlight>
 
{{out}}
Line 74:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
PROC Main()
Line 91:
OD
PrintF("%E%EThere are %I triplets",count)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Triplet_of_three_numbers.png Screenshot from Atari 8-bit computer]
Line 107:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
 
procedure Triplets is
Line 151:
end if;
end loop;
end Triplets;</langsyntaxhighlight>
{{out}}
<pre>
Line 204:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find numbers n where n-1, n+3 and n+5 are prime #
# sieve the primes up to the maximum number for the task #
PR read "primes.incl.a68" PR
Line 221:
OD;
print( ( newline, "Found ", TOSTRING n count, " triplets", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 241:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">lst: select 3..6000 'x
-> all? @[prime? x-1 prime? x+3 prime? x+5]
 
loop split.every: 10 lst 'a ->
print map a => [pad to :string & 5]</langsyntaxhighlight>
 
{{out}}
Line 256:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TRIPLET_OF_THREE_NUMBERS.AWK
BEGIN {
Line 283:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 338:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z: N=6000
20 DIM P(N+5)
30 FOR I=2 TO SQR(N)
Line 346:
70 IF P(I-1) OR P(I+3) OR P(I+5) GOTO 90
80 PRINT USING "####,: ####, ####, ####,";I;I-1;I+3;I+5
90 NEXT</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'> 8: 7 11 13
Line 398:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
N = 6000
dim p(N+6)
Line 418:
next i
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 425:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
manifest $( limit = 6000 $)
 
Line 451:
writef("%I4: %I4, %I4, %I4*N", i, i-1, i+3, i+5)
freevec(prime)
$)</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'> 8: 7, 11, 13
Line 501:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 536:
free(p);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'> 8: 7, 11, 13
Line 587:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <vector>
 
Line 624:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 8: 7, 11, 13
Line 675:
=={{header|C#|CSharp}}==
How about some upper limits above 6000?
<langsyntaxhighlight lang="csharp">using System; using System.Collections.Generic; using System.Linq;
using T3 = System.Tuple<int, int, int>; using static System.Console;
class Program { static void Main() {
Line 696:
for (int k = s, i = j << 1; k < l; k += i) f[k] = true; }
for (; j < l; lllj = llj, llj = lj, lj = j, j += 2)
if (isPrT(lllj, lj, j)) yield return new T3(lllj, lj, j); } }</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'> "N": Prime Triplet Adjacent (to previous)
Line 754:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">LIMIT = 6000
 
isqrt = proc (s: int) returns (int)
Line 799:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 8: 7, 11, 13
Line 849:
 
=={{header|Comal}}==
<langsyntaxhighlight lang="comal">0010 lim#:=6000
0020 DIM prime#(lim#)
0030 FOR i#:=2 TO lim# DO prime#(i#):=TRUE
Line 860:
0100 ENDIF
0110 ENDFOR i#
0120 END</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 8: 7, 11, 13
Line 910:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
const LIMIT := 6000;
Line 946:
end if;
i := i + 1;
end loop;</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'>8: 7, 11, 13
Line 997:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<langsyntaxhighlight lang="fsharp">
// Prime triplets: Nigel Galloway. May 18th., 2021
primes32()|>Seq.takeWhile((>)6000)|>Seq.filter(fun n->isPrime(n+4)&&isPrime(n+6))|>Seq.iter((+)1>>printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,008:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: combinators formatting grouping kernel math math.primes
math.statistics sequences ;
 
Line 1,018:
"..., %4d, [%4d], __, __, %4d, __, %4d, ...\n" printf ;
 
6000 4,2-gaps [ first3 [ dup 1 + ] 2dip triplet. ] each</langsyntaxhighlight>
{{out}}
<pre style="height:14em">
Line 1,071:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: prime? ( n -- ? ) here + c@ 0= ;
: notprime! ( n -- ) here + 1 swap c! ;
 
Line 1,114:
 
6000 main
bye</langsyntaxhighlight>
 
{{out}}
Line 1,171:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
Dim As Integer N = 6000
Dim As Integer p(N)
Line 1,190:
Next i
Sleep
</syntaxhighlight>
</lang>
<pre>
8: 7 11 13
Line 1,244:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,268:
}
fmt.Printf("\n%d such numbers found.\n", len(numbers))
}</langsyntaxhighlight>
 
{{out}}
Line 1,324:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">triplet=: (1 *./@p: _1 3 5+])"0
echo (0 _1 3 5+])"0 (triplet#]) i.6000
exit ''</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'> 8 7 11 13
Line 1,379:
'''Works with gojq, the Go implementation of jq'''
 
<langsyntaxhighlight lang="jq">def is_prime:
if . == 2 then true
else
Line 1,390:
end ;
 
range(3;6000) | select( all( .-1, .+3, .+5; is_prime))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,403:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
makesprimetriplet(n) = all(isprime, [n - 1, n + 3, n + 5])
println(" N Prime Triplet\n--------------------------")
foreach(n -> println(rpad(n, 6), [n - 1, n + 3, n + 5]), filter(makesprimetriplet, 2:6005))
</langsyntaxhighlight>{{out}}
<pre>
N Prime Triplet
Line 1,461:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 1,511:
IFS=${oldIFS}
unset arr
done</langsyntaxhighlight>
{{out}}<pre>
8: 7,11,13
Line 1,561:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
BOOLEAN PRIME
DIMENSION PRIME(6005)
Line 1,585:
 
VECTOR VALUES FMT = $I4,3H =,3(I5)*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre style='height:50ex'> 8 = 7 11 13
Line 1,635:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Select[Range[5999], PrimeQ[# - 1] && PrimeQ[# + 3] && PrimeQ[# + 5] &]</langsyntaxhighlight>
{{out}}
<pre>{8, 14, 38, 68, 98, 104, 194, 224, 278, 308, 458, 614, 824, 854, 878, 1088, 1298, 1424, 1448, 1484, 1664, 1694, 1784, 1868, 1874, 1994, 2084, 2138, 2378, 2684, 2708, 2798, 3164, 3254, 3458, 3464, 3848, 4154, 4514, 4784, 5228, 5414, 5438, 5648, 5654, 5738}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
const
Line 1,666:
inc count
 
echo &"\nFound {count} triplets for n < {N+1}."</langsyntaxhighlight>
 
{{out}}
Line 1,721:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,728:
 
is_prime($_ - 4) and printf "%5d" x 4 . "\n", $_ - 3, $_ - 4, $_, $_ + 2
for @{ twin_primes( 6000 ) };</langsyntaxhighlight>
{{out}}
<pre>
Line 1,780:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">trio</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span><span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">))=</span><span style="color: #000000;">3</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">trio</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d found: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(assumes you can add {-1,3,5} to each number in your head easily enough)</small>
Line 1,792:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 1,852:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre style='height:50ex'>8: 7, 11, 13
Line 1,904:
=={{header|Python}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="python">
#!/usr/bin/python3
 
Line 1,921:
else:
print(i, ': ', i-1, ' ', i+3, ' ', i+5)
</syntaxhighlight>
</lang>
<pre>
Similar a la entrada de FreeBASIC.
Line 1,928:
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
 
[ dup 2 < iff
Line 1,943:
else drop ]
else drop ]
echo</langsyntaxhighlight>
 
{{out}}
Line 1,953:
A weird combination of [[Cousin primes]] and [[Twin primes]] that are siblings, but known by their neighbor.... I shall dub these Alabama primes.
 
<syntaxhighlight lang="raku" perl6line>say "{.[0]+1}: ",$_ for grep *.all.is-prime, ^6000 .race.map: { $_-1, $_+3, $_+5 };</langsyntaxhighlight>
{{out}}
<pre>8: (7 11 13)
Line 2,003:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX pgm finds prime triplets: n-1, n+3, n+5 are primes, and n < some specified #.*/
parse arg hi cols . /*obtain optional argument from the CL.*/
if hi=='' | hi=="," then hi= 6000 /*Not specified? Then use the default.*/
Line 2,047:
end /*k*/ /* [↑] only process numbers ≤ √ J */
#= #+1; @.#= j; sq.#= j*j; !.j= 1 /*bump # of Ps; assign next P; P²; P# */
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,070:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
see "working..." + nl
Line 2,092:
see "Found " + row + " prime triplets" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,149:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: isPrime (in integer: number) is func
Line 2,183:
end for;
writeln("\nFound " <& count <& " triplets for n < 6000.");
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 2,239:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">^6000 -> grep {|n| [-1, 3, 5].all {|k| n + k -> is_prime } }.say</langsyntaxhighlight>
{{out}}
<pre>
Line 2,248:
=={{header|True BASIC}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="basic">
LET n = 6000
 
Line 2,270:
NEXT i
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,278:
=={{header|Vlang}}==
{{trans|Python}}
<langsyntaxhighlight lang="vlang">import math
 
const n = 6000
Line 2,299:
}
}
}</langsyntaxhighlight>
<pre>
Similar to Go
Line 2,307:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/math" for Int
import "/fmt" for Fmt
 
Line 2,319:
}
for (n in numbers) Fmt.print("$,6d => $,6d", n, [n-1, n+3, n+5])
System.print("\nFound %(numbers.count) such numbers.")</langsyntaxhighlight>
 
{{out}}
Line 2,375:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 2,401:
Text(0, " prime triplets found below 6000.
");
]</langsyntaxhighlight>
 
{{out}}
Line 2,420:
=={{header|Yabasic}}==
{{trans|Python}}
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Triplet_of_three_numbers
// by Galileo, 04/2022
 
Line 2,436:
for i = 3 to N
if not (p(i-1) or p(i+3) or p(i+5)) print i, ": ", i-1, " ", i+3, " ", i+5
next</langsyntaxhighlight>
{{out}}
<pre>8: 7 11 13
10,327

edits