Minimum primes: Difference between revisions
Content added Content deleted
(Realize in F#) |
(Added Easylang) |
||
(20 intermediate revisions by 17 users not shown) | |||
Line 18: | Line 18: | ||
{{Trans|Wren}} |
{{Trans|Wren}} |
||
Can handle the possibility of the maximum elements being negative, 0, 1 or 2. |
Can handle the possibility of the maximum elements being negative, 0, 1 or 2. |
||
{{libheader|ALGOL 68-primes}} |
|||
<lang algol68>BEGIN # show the minimum prime >= the maximum elements of three lists # |
|||
<syntaxhighlight lang="algol68">BEGIN # show the minimum prime >= the maximum elements of three lists # |
|||
PR read "primes.incl.a68" PR |
PR read "primes.incl.a68" PR |
||
[]BOOL prime = PRIMESIEVE 1000; # should be enough primes for this task... # |
|||
[]INT numbers1 = ( 5, 45, 23, 21, 67 ); |
[]INT numbers1 = ( 5, 45, 23, 21, 67 ); |
||
[]INT numbers2 = ( 43, 22, 78, 46, 38 ); |
[]INT numbers2 = ( 43, 22, 78, 46, 38 ); |
||
[]INT numbers3 = ( 9, 98, 12, 54, 53 ); |
[]INT numbers3 = ( 9, 98, 12, 54, 53 ); |
||
[ 1 : UPB numbers1 ]INT prime list; |
[ 1 : UPB numbers1 ]INT prime list; |
||
INT max element := numbers1[ 1 ]; |
|||
FOR i TO UPB numbers1 DO |
FOR i TO UPB numbers1 DO |
||
INT m := numbers1[ i ]; |
INT m := numbers1[ i ]; |
||
IF numbers2[ i ] > m THEN m := numbers2[ i ] FI; |
IF numbers2[ i ] > m THEN m := numbers2[ i ] FI; |
||
IF numbers3[ i ] > m THEN m := numbers3[ i ] FI; |
IF numbers3[ i ] > m THEN m := numbers3[ i ] FI; |
||
IF m > max element THEN max element := m FI; |
|||
prime list[ i ] := m |
|||
OD; |
|||
# construct a sieve of primes big enough for the maximum element # |
|||
[]BOOL prime = PRIMESIEVE ( max element * 2 ); |
|||
# replace the elements of prime list wih the smallest prime >= the element # |
|||
FOR i TO UPB prime list DO |
|||
INT m := prime list[ i ]; |
|||
# find the next prime >= m # |
# find the next prime >= m # |
||
IF m <= 2 THEN m := 2 |
IF m <= 2 THEN m := 2 |
||
Line 40: | Line 49: | ||
FOR i TO UPB prime list DO print( ( " ", whole( prime list[ i ], 0 ) ) ) OD; |
FOR i TO UPB prime list DO print( ( " ", whole( prime list[ i ], 0 ) ) ) OD; |
||
print( ( " ]" ) ) |
print( ( " ]" ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[ 43 101 79 59 67 ] |
[ 43 101 79 59 67 ] |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">lists: [ |
|||
[ 5 45 23 21 67] |
|||
[43 22 78 46 38] |
|||
[ 9 98 12 54 53] |
|||
] |
|||
print map 0..dec size first lists 'i -> |
|||
first select.first (max map lists 'l -> l\[i])..∞ => prime?</syntaxhighlight> |
|||
{{out}} |
|||
<pre>43 101 79 59 67</pre> |
|||
=={{header|AWK}}== |
|||
<syntaxhighlight lang="awk"> |
|||
# syntax: GAWK -f MINIMUM_PRIMES.AWK |
|||
BEGIN { |
|||
n1 = split("5,45,23,21,67",numbers1,",") |
|||
n2 = split("43,22,78,46,38",numbers2,",") |
|||
n3 = split("9,98,12,54,53",numbers3,",") |
|||
if (n1 != n2 || n1 != n3) { |
|||
print("error: arrays must be same length") |
|||
exit(1) |
|||
} |
|||
for (i=1; i<=n1; i++) { |
|||
m = max(max(numbers1[i],numbers2[i]),numbers3[i]) |
|||
if (m % 2 == 0) { m++ } |
|||
while (!is_prime(m)) { m += 2 } |
|||
primes[i] = m |
|||
printf("%d ",primes[i]) |
|||
} |
|||
printf("\n") |
|||
exit(0) |
|||
} |
|||
function is_prime(x, i) { |
|||
if (x <= 1) { |
|||
return(0) |
|||
} |
|||
for (i=2; i<=int(sqrt(x)); i++) { |
|||
if (x % i == 0) { |
|||
return(0) |
|||
} |
|||
} |
|||
return(1) |
|||
} |
|||
function max(x,y) { return((x > y) ? x : y) } |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
43 101 79 59 67 |
|||
</pre> |
|||
=={{header|BASIC256}}== |
|||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="basic256">dim Num1 = { 5,45,23,21,67} |
|||
dim Num2 = {43,22,78,46,38} |
|||
dim Num3 = { 9,98,12,54,53} |
|||
print "The minimum prime numbers of three lists" |
|||
print "[ "; |
|||
for n = 0 to 4 |
|||
maxi = max(Num1[n], max(Num2[n], Num3[n])) |
|||
if maxi mod 2 = 0 then maxi += 1 |
|||
while not isPrime(maxi) |
|||
maxi += 2 |
|||
end while |
|||
print maxi; " "; |
|||
next n |
|||
print "]" |
|||
end |
|||
function max(a, b) |
|||
if a > b then return a else return b |
|||
end function |
|||
function isPrime(v) |
|||
if v < 2 then return False |
|||
if v mod 2 = 0 then return v = 2 |
|||
if v mod 3 = 0 then return v = 3 |
|||
d = 5 |
|||
while d * d <= v |
|||
if v mod d = 0 then return False else d += 2 |
|||
end while |
|||
return True |
|||
end function</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#define TRUE 1 |
#define TRUE 1 |
||
Line 88: | Line 185: | ||
printf("\n"); |
printf("\n"); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 98: | Line 195: | ||
== {{header|C#|CSharp}} == |
== {{header|C#|CSharp}} == |
||
{{trans|Ring}}...solution #1. |
{{trans|Ring}}...solution #1. |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Linq; |
using System.Linq; |
||
using static System.Console; |
using static System.Console; |
||
Line 119: | Line 216: | ||
Nums[i] = nxtPrime(new int[]{ Num1[i], Num2[i], Num3[i] }.Max()); |
Nums[i] = nxtPrime(new int[]{ Num1[i], Num2[i], Num3[i] }.Max()); |
||
WriteLine("The minimum prime numbers of three lists = [{0}]", string.Join(",", Nums)); |
WriteLine("The minimum prime numbers of three lists = [{0}]", string.Join(",", Nums)); |
||
Write("done..."); } }</ |
Write("done..."); } }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Same as Ring. |
Same as Ring. |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
<syntaxhighlight lang="Delphi"> |
|||
function IsPrime(N: int64): boolean; |
|||
{Fast, optimised prime test} |
|||
var I,Stop: int64; |
|||
begin |
|||
if (N = 2) or (N=3) then Result:=true |
|||
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false |
|||
else |
|||
begin |
|||
I:=5; |
|||
Stop:=Trunc(sqrt(N+0.0)); |
|||
Result:=False; |
|||
while I<=Stop do |
|||
begin |
|||
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit; |
|||
Inc(I,6); |
|||
end; |
|||
Result:=True; |
|||
end; |
|||
end; |
|||
type TIntArray = array of integer; |
|||
type TNumList = array [0..2, 0..4] of integer; |
|||
const NumLists: TNumList = ( |
|||
(5,45,23,21,67), |
|||
(43,22,78,46,38), |
|||
(9,98,12,98,53)); |
|||
procedure GetColPrimes(NumList: TNumList; var ColPrimes: TIntArray); |
|||
{Get the Maxium value, find next prime and store result in array} |
|||
var X,Y,I,M: integer; |
|||
var Highest: integer; |
|||
begin |
|||
for X:=0 to High(NumLists[0]) do |
|||
begin |
|||
Highest:=0; |
|||
for Y:=0 to High(NumList) do |
|||
if NumLists[Y,X]>Highest then Highest:=NumList[Y,X]; |
|||
SetLength(ColPrimes,Length(ColPrimes)+1); |
|||
ColPrimes[High(ColPrimes)]:=Highest; |
|||
end; |
|||
for I:=0 to High(ColPrimes) do |
|||
begin |
|||
M:=ColPrimes[I]; |
|||
if (M mod 2)=0 then Inc(M); |
|||
while not IsPrime(M) do Inc(M,2); |
|||
ColPrimes[I]:=M; |
|||
end; |
|||
end; |
|||
procedure ShowColumnPrimes(Memo: TMemo); |
|||
{Show min value for columns in NumLists} |
|||
var ColPrimes: TIntArray; |
|||
var I: integer; |
|||
var S: string; |
|||
begin |
|||
GetColPrimes(NumLists,ColPrimes); |
|||
S:='['; |
|||
for I:=0 to High(ColPrimes) do |
|||
begin |
|||
if I<>0 then S:=S+' '; |
|||
S:=S+IntToStr(ColPrimes[I]); |
|||
end; |
|||
S:=S+']'; |
|||
Memo.Lines.Add(S); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[43 101 79 101 67] |
|||
</pre> |
|||
=={{header|EasyLang}}== |
|||
{{trans|Ring}} |
|||
<syntaxhighlight> |
|||
func nxtprim p . |
|||
j = 2 |
|||
while j * j <= p |
|||
if p mod j = 0 |
|||
j = 1 |
|||
p += 1 |
|||
. |
|||
j += 1 |
|||
. |
|||
return p |
|||
. |
|||
num1[] = [ 5 45 23 21 67 ] |
|||
num2[] = [ 43 22 78 46 38 ] |
|||
num3[] = [ 9 98 12 54 53 ] |
|||
n = len num1[] |
|||
len nums[] n |
|||
for i to n |
|||
nums[i] = nxtprim higher higher num1[i] num2[i] num3[i] |
|||
. |
|||
print nums[] |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[ 43 101 79 59 67 ] |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Minimum primes. Nigel Galloway: October 29th., 2021 |
// Minimum primes. Nigel Galloway: October 29th., 2021 |
||
let N1,N2,N3=[5;45;23;21;67],[43;22;78;46;38],[9;98;12;54;53] |
let N1,N2,N3=[5;45;23;21;67],[43;22;78;46;38],[9;98;12;54;53] |
||
let fN g=primes32()|>Seq.find((<=)g) |
let fN g=primes32()|>Seq.find((<=)g) |
||
printfn "%A" (List.zip3 N1 N2 N3|>List.map(fun(n,g,l)->fN(max (max n g) l))) |
printfn "%A" (List.zip3 N1 N2 N3|>List.map(fun(n,g,l)->fN(max (max n g) l))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[43; 101; 79; 59; 67] |
[43; 101; 79; 59; 67] |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: math math.order math.primes prettyprint sequences ; |
||
{ 5 45 23 21 67 } { 43 22 78 46 38 } { 9 98 12 54 53 } |
{ 5 45 23 21 67 } { 43 22 78 46 38 } { 9 98 12 54 53 } |
||
[ max max 1 - next-prime ] 3map .</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
{ 43 101 79 59 67 } |
{ 43 101 79 59 67 } |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">#define MAX(a, b) iif((a) > (b), (a), (b)) |
|||
Function isPrime(Byval ValorEval As Integer) As Boolean |
|||
If ValorEval < 2 Then Return False |
|||
If ValorEval Mod 2 = 0 Then Return ValorEval = 2 |
|||
If ValorEval Mod 3 = 0 Then Return ValorEval = 3 |
|||
Dim d As Integer = 5 |
|||
While d * d <= ValorEval |
|||
If ValorEval Mod d = 0 Then Return False Else d += 2 |
|||
Wend |
|||
Return True |
|||
End Function |
|||
Dim As Integer Num1(5) = { 5,45,23,21,67} |
|||
Dim As Integer Num2(5) = {43,22,78,46,38} |
|||
Dim As Integer Num3(5) = { 9,98,12,54,53} |
|||
Print "The minimum prime numbers of three lists..." |
|||
Print "["; |
|||
For n As Integer = 0 To 4 |
|||
Dim As Integer maxi = MAX(num1(n), MAX(num2(n), num3(n))) |
|||
If (maxi Mod 2 = 0) Then maxi += 1 |
|||
While Not isPrime(maxi) |
|||
maxi += 2 |
|||
Wend |
|||
Print maxi; ", "; |
|||
Next n |
|||
Print !"\b\b ]" |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[ 43, 101, 79, 59, 67 ] |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
{{libheader|Go-rcu}} |
{{libheader|Go-rcu}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 170: | Line 424: | ||
} |
} |
||
fmt.Println(primes) |
fmt.Println(primes) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 176: | Line 430: | ||
[43 101 79 59 67] |
[43 101 79 59 67] |
||
</pre> |
</pre> |
||
=={{header|J}}== |
|||
<syntaxhighlight lang="j"> ] numbers =. 3 5 $ 5 45 23 21 67 43 22 78 46 38 9 98 12 54 53 |
|||
5 45 23 21 67 |
|||
43 22 78 46 38 |
|||
9 98 12 54 53 |
|||
4 p: <: >./ numbers |
|||
43 101 79 59 67</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 184: | Line 447: | ||
Two solutions are presented following these preliminaries: |
Two solutions are presented following these preliminaries: |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
include "is_prime"; # reminder |
include "is_prime"; # reminder |
||
Line 195: | Line 458: | ||
if m%2 == 0 then primes(m+1;n) |
if m%2 == 0 then primes(m+1;n) |
||
else range(m; n; 2) | select(is_prime) |
else range(m; n; 2) | select(is_prime) |
||
end;</ |
end;</syntaxhighlight> |
||
'''Explicit Iteration''' |
'''Explicit Iteration''' |
||
< |
<syntaxhighlight lang="jq">[range(0;5) |
||
| [Numbers1[.], Numbers2[.], Numbers3[.]] | max |
| [Numbers1[.], Numbers2[.], Numbers3[.]] | max |
||
| first(primes(.; infinite))]</ |
| first(primes(.; infinite))]</syntaxhighlight> |
||
'''Functional''' |
'''Functional''' |
||
< |
<syntaxhighlight lang="jq">[Numbers1, Numbers2, Numbers3] |
||
| transpose |
| transpose |
||
| [map(max | first(primes(.; infinite)))] </ |
| [map(max | first(primes(.; infinite)))] </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 210: | Line 473: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
println(nextprime.(maximum(hcat([5,45,23,21,67], [43,22,78,46,38], [9,98,12,54,53]), dims=2))) |
println(nextprime.(maximum(hcat([5,45,23,21,67], [43,22,78,46,38], [9,98,12,54,53]), dims=2))) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre>[43; 101; 79; 59; 67;;]</pre> |
<pre>[43; 101; 79; 59; 67;;]</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">minPrime[x_List] := |
|||
If[PrimeQ@Max@x, Max@x, NextPrime@Max@x] |
|||
MapThread[ |
|||
minPrime@{##} &, {{5., 45, 23, 21, 67}, {43, 22, 78, 46, 38}, {9, 98, |
|||
12, 54, 53}}]</syntaxhighlight> |
|||
{{out}}<pre> |
|||
{43,101,79,59,67} |
|||
</pre> |
|||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">const |
|||
Numbers1 = [ 5, 45, 23, 21, 67] |
|||
Numbers2 = [43, 22, 78, 46, 38] |
|||
Numbers3 = [ 9, 98, 12, 54, 53] |
|||
var numbers: array[0..Numbers1.high, int] |
|||
template isEven(n: int): bool = (n and 1) == 0 |
|||
func isPrime(n: Positive): bool = |
|||
if n < 2: return false |
|||
if n.isEven: 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 |
|||
func minPrime(n: int): int = |
|||
if n == 2: return 2 |
|||
result = if n.isEven: n + 1 else: n |
|||
while not result.isPrime(): |
|||
inc result, 2 |
|||
for i in 0..numbers.high: |
|||
let m = max(max(Numbers1[i], Numbers2[i]), Numbers3[i]) |
|||
numbers[i] = minPrime(m) |
|||
echo numbers</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[43, 101, 79, 59, 67]</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Minimum_primes |
use strict; # https://rosettacode.org/wiki/Minimum_primes |
||
Line 233: | Line 544: | ||
} 0 .. 4; |
} 0 .. 4; |
||
print "@Primes\n";</ |
print "@Primes\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
43 101 79 59 67 |
43 101 79 59 67 |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
<span style="color: #008080;">function</span> <span style="color: #000000;">nextprime</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))</span> |
|||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</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: #008080;">do</span> |
|||
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_prime</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">maxsq</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
|||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</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;">"%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">nextprime</span><span style="color: #0000FF;">({{</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">45</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">67</span><span style="color: #0000FF;">},</span> |
|||
<span style="color: #0000FF;">{</span><span style="color: #000000;">43</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">22</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">78</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">46</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">38</span><span style="color: #0000FF;">},</span> |
|||
<span style="color: #0000FF;">{</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">98</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">54</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">53</span><span style="color: #0000FF;">}})})</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
|||
<pre> |
|||
{43,101,79,59,67} |
|||
</pre> |
|||
=={{header|Quackery}}== |
|||
<code>transpose</code> is defined at [[Matrix transposition#Quackery]]. |
|||
<code>isprime</code> is defined at [[Primality by trial division#Quackery]]. |
|||
<syntaxhighlight lang="Quackery"> ' [ 5 45 23 21 67 ] |
|||
' [ 43 22 78 46 38 ] |
|||
' [ 9 98 12 54 53 ] |
|||
3 pack transpose |
|||
[] swap witheach |
|||
[ unpack max max 1 - |
|||
[ 1+ dup isprime until ] |
|||
join ] |
|||
echo |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[ 43 101 79 59 67 ]</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Seems kind of pointless to specify a maximum of 5 terms when there are only 5 elements in each list but... ¯\_(ツ)_/¯ |
Seems kind of pointless to specify a maximum of 5 terms when there are only 5 elements in each list but... ¯\_(ツ)_/¯ |
||
<lang |
<syntaxhighlight lang="raku" line>say ([Zmax] <5 45 23 21 67>, <43 22 78 46 38>, <9 98 12 54 53>)».&next-prime[^5]; |
||
sub next-prime { ($^m..*).first: &is-prime }</ |
sub next-prime { ($^m..*).first: &is-prime }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(43 101 79 59 67)</pre> |
<pre>(43 101 79 59 67)</pre> |
||
Line 250: | Line 601: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
===Solution #1=== |
===Solution #1=== |
||
< |
<syntaxhighlight lang="ring">? "working..." |
||
Num1 = [ 5,45,23,21,67] |
Num1 = [ 5,45,23,21,67] |
||
Line 276: | Line 627: | ||
else j++ ok |
else j++ ok |
||
if j * j > x exit ok |
if j * j > x exit ok |
||
end return string(x)</ |
end return string(x)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 284: | Line 635: | ||
</pre> |
</pre> |
||
===Solution #2=== |
===Solution #2=== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
see "working..." + nl |
see "working..." + nl |
||
Line 322: | Line 673: | ||
txt = txt + "]" |
txt = txt + "]" |
||
see txt |
see txt |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 328: | Line 679: | ||
Minimum primes = [43,101,79,59,67] |
Minimum primes = [43,101,79,59,67] |
||
done... |
done... |
||
</pre> |
|||
=={{header|RPL}}== |
|||
====By the letter==== |
|||
{{works with|HP|49g}} |
|||
≪ {5 45 23 21 67} {43 22 78 46 38} {9 98 12 54 53} → numbers1 numbers2 numbers3 |
|||
≪ numbers1 numbers2 numbers3 3 ≪ MAX MAX ≫ DOLIST |
|||
{ } → max primes |
|||
≪ 1 max SIZE '''FOR''' j |
|||
max j GET |
|||
'''IF''' DUP ISPRIME? NOT '''THEN''' NEXTPRIME '''END''' |
|||
'primes' SWAP STO+ |
|||
'''NEXT''' primes |
|||
≫ ≫ ≫ '<span style="color:blue">TASK</span>' STO |
|||
{{out}} |
|||
<pre> |
|||
1: {43 101 79 59 67} |
|||
</pre> |
|||
====Idiomatic==== |
|||
No need for local variables. |
|||
≪ {5 45 23 21 67} {43 22 78 46 38} {9 98 12 54 53} |
|||
3 ≪ MAX MAX ≫ DOLIST |
|||
≪ '''IF''' DUP ISPRIME? NOT '''THEN''' NEXTPRIME '''END''' ≫ MAP |
|||
≫ '<span style="color:blue">TASK</span>' STO |
|||
=={{header|Ruby}}== |
|||
<syntaxhighlight lang="ruby">require "prime" |
|||
numbers1 = [ 5, 45, 23, 21, 67] |
|||
numbers2 = [43, 22, 78, 46, 38] |
|||
numbers3 = [ 9, 98, 12, 54, 53] |
|||
p [numbers1, numbers2, numbers3].transpose.map{|ar| (ar.max..).find(&:prime?) }</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[43, 101, 79, 59, 67] |
|||
</pre> |
|||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby">var lists = [ |
|||
[ 5,45,23,21,67], |
|||
[43,22,78,46,38], |
|||
[ 9,98,12,54,53], |
|||
] |
|||
say lists.zip.map { next_prime(.max - 1) }</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[43, 101, 79, 59, 67] |
|||
</pre> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
< |
<syntaxhighlight lang="wren">import "./math" for Int |
||
var numbers1 = [ 5, 45, 23, 21, 67] |
var numbers1 = [ 5, 45, 23, 21, 67] |
||
Line 344: | Line 741: | ||
primes[n] = max |
primes[n] = max |
||
} |
} |
||
System.print(primes)</ |
System.print(primes)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[43, 101, 79, 59, 67] |
[43, 101, 79, 59, 67] |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is a prime number |
|||
int N, I; |
|||
[if N <= 1 then return false; |
|||
for I:= 2 to sqrt(N) do |
|||
if rem(N/I) = 0 then return false; |
|||
return true; |
|||
]; |
|||
int Numbers1, Numbers2, Numbers3, N, Max; |
|||
[Numbers1:= [5,45,23,21,67]; |
|||
Numbers2:= [43,22,78,46,38]; |
|||
Numbers3:= [9,98,12,54,53]; |
|||
for N:= 0 to 4 do |
|||
[Max:= Numbers1(N); |
|||
if Numbers2(N) > Max then Max:= Numbers2(N); |
|||
if Numbers3(N) > Max then Max:= Numbers3(N); |
|||
while not IsPrime(Max) do Max:= Max+1; |
|||
IntOut(0, Max); ChOut(0, ^ ); |
|||
]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
43 101 79 59 67 |
|||
</pre> |
</pre> |
Latest revision as of 13:35, 9 May 2024
Minimum primes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Given three lists:
- Numbers1 = [5,45,23,21,67]
- Numbers2 = [43,22,78,46,38]
- Numbers3 = [9,98,12,54,53]
then:
- Select the maximum (max) of Numbers[n], Numbers2[n] and Numbers3[n], where n <= 5 (one based).
- For each value of max, find the least prime, minPrime, such that minPrime >= max
- Add minPrime to a new list (Primes)
- Show Primes on this page.
ALGOL 68
Can handle the possibility of the maximum elements being negative, 0, 1 or 2.
BEGIN # show the minimum prime >= the maximum elements of three lists #
PR read "primes.incl.a68" PR
[]INT numbers1 = ( 5, 45, 23, 21, 67 );
[]INT numbers2 = ( 43, 22, 78, 46, 38 );
[]INT numbers3 = ( 9, 98, 12, 54, 53 );
[ 1 : UPB numbers1 ]INT prime list;
INT max element := numbers1[ 1 ];
FOR i TO UPB numbers1 DO
INT m := numbers1[ i ];
IF numbers2[ i ] > m THEN m := numbers2[ i ] FI;
IF numbers3[ i ] > m THEN m := numbers3[ i ] FI;
IF m > max element THEN max element := m FI;
prime list[ i ] := m
OD;
# construct a sieve of primes big enough for the maximum element #
[]BOOL prime = PRIMESIEVE ( max element * 2 );
# replace the elements of prime list wih the smallest prime >= the element #
FOR i TO UPB prime list DO
INT m := prime list[ i ];
# find the next prime >= m #
IF m <= 2 THEN m := 2
ELSE
IF NOT ODD m THEN m +:= 1 FI;
WHILE NOT prime[ m ] DO m +:= 2 OD
FI;
prime list[ i ] := m
OD;
print( ( "[" ) );
FOR i TO UPB prime list DO print( ( " ", whole( prime list[ i ], 0 ) ) ) OD;
print( ( " ]" ) )
END
- Output:
[ 43 101 79 59 67 ]
Arturo
lists: [
[ 5 45 23 21 67]
[43 22 78 46 38]
[ 9 98 12 54 53]
]
print map 0..dec size first lists 'i ->
first select.first (max map lists 'l -> l\[i])..∞ => prime?
- Output:
43 101 79 59 67
AWK
# syntax: GAWK -f MINIMUM_PRIMES.AWK
BEGIN {
n1 = split("5,45,23,21,67",numbers1,",")
n2 = split("43,22,78,46,38",numbers2,",")
n3 = split("9,98,12,54,53",numbers3,",")
if (n1 != n2 || n1 != n3) {
print("error: arrays must be same length")
exit(1)
}
for (i=1; i<=n1; i++) {
m = max(max(numbers1[i],numbers2[i]),numbers3[i])
if (m % 2 == 0) { m++ }
while (!is_prime(m)) { m += 2 }
primes[i] = m
printf("%d ",primes[i])
}
printf("\n")
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
function max(x,y) { return((x > y) ? x : y) }
- Output:
43 101 79 59 67
BASIC256
dim Num1 = { 5,45,23,21,67}
dim Num2 = {43,22,78,46,38}
dim Num3 = { 9,98,12,54,53}
print "The minimum prime numbers of three lists"
print "[ ";
for n = 0 to 4
maxi = max(Num1[n], max(Num2[n], Num3[n]))
if maxi mod 2 = 0 then maxi += 1
while not isPrime(maxi)
maxi += 2
end while
print maxi; " ";
next n
print "]"
end
function max(a, b)
if a > b then return a else return b
end function
function isPrime(v)
if v < 2 then return False
if v mod 2 = 0 then return v = 2
if v mod 3 = 0 then return v = 3
d = 5
while d * d <= v
if v mod d = 0 then return False else d += 2
end while
return True
end function
C
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int isPrime(int n) {
int d;
if (n < 2) return FALSE;
if (n%2 == 0) return n == 2;
if (n%3 == 0) return n == 3;
d = 5;
while (d*d <= n) {
if (!(n%d)) return FALSE;
d += 2;
if (!(n%d)) return FALSE;
d += 4;
}
return TRUE;
}
int max(int a, int b) {
if (a > b) return a;
return b;
}
int main() {
int n, m;
int numbers1[5] = { 5, 45, 23, 21, 67};
int numbers2[5] = {43, 22, 78, 46, 38};
int numbers3[5] = { 9, 98, 12, 54, 53};
int primes[5] = {};
for (n = 0; n < 5; ++n) {
m = max(max(numbers1[n], numbers2[n]), numbers3[n]);
if (!(m % 2)) m++;
while (!isPrime(m)) m += 2;
primes[n] = m;
printf("%d ", primes[n]);
}
printf("\n");
return 0;
}
- Output:
43 101 79 59 67
C#
...solution #1.
using System;
using System.Linq;
using static System.Console;
class Program {
static int nxtPrime(int x) {
int j = 2; do {
if (x % j == 0) { j = 2; x++; }
else j += j < 3 ? 1 : 2;
} while (j * j <= x); return x; }
static void Main(string[] args) {
WriteLine("working...");
int[] Num1 = new int[]{ 5, 45, 23, 21, 67 },
Num2 = new int[]{ 43, 22, 78, 46, 38 },
Num3 = new int[]{ 9, 98, 12, 54, 53 };
int n = Num1.Length; int[] Nums = new int[n];
for (int i = 0; i < n; i++)
Nums[i] = nxtPrime(new int[]{ Num1[i], Num2[i], Num3[i] }.Max());
WriteLine("The minimum prime numbers of three lists = [{0}]", string.Join(",", Nums));
Write("done..."); } }
- Output:
Same as Ring.
Delphi
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
type TIntArray = array of integer;
type TNumList = array [0..2, 0..4] of integer;
const NumLists: TNumList = (
(5,45,23,21,67),
(43,22,78,46,38),
(9,98,12,98,53));
procedure GetColPrimes(NumList: TNumList; var ColPrimes: TIntArray);
{Get the Maxium value, find next prime and store result in array}
var X,Y,I,M: integer;
var Highest: integer;
begin
for X:=0 to High(NumLists[0]) do
begin
Highest:=0;
for Y:=0 to High(NumList) do
if NumLists[Y,X]>Highest then Highest:=NumList[Y,X];
SetLength(ColPrimes,Length(ColPrimes)+1);
ColPrimes[High(ColPrimes)]:=Highest;
end;
for I:=0 to High(ColPrimes) do
begin
M:=ColPrimes[I];
if (M mod 2)=0 then Inc(M);
while not IsPrime(M) do Inc(M,2);
ColPrimes[I]:=M;
end;
end;
procedure ShowColumnPrimes(Memo: TMemo);
{Show min value for columns in NumLists}
var ColPrimes: TIntArray;
var I: integer;
var S: string;
begin
GetColPrimes(NumLists,ColPrimes);
S:='[';
for I:=0 to High(ColPrimes) do
begin
if I<>0 then S:=S+' ';
S:=S+IntToStr(ColPrimes[I]);
end;
S:=S+']';
Memo.Lines.Add(S);
end;
- Output:
[43 101 79 101 67]
EasyLang
func nxtprim p .
j = 2
while j * j <= p
if p mod j = 0
j = 1
p += 1
.
j += 1
.
return p
.
num1[] = [ 5 45 23 21 67 ]
num2[] = [ 43 22 78 46 38 ]
num3[] = [ 9 98 12 54 53 ]
n = len num1[]
len nums[] n
for i to n
nums[i] = nxtprim higher higher num1[i] num2[i] num3[i]
.
print nums[]
- Output:
[ 43 101 79 59 67 ]
F#
This task uses Extensible Prime Generator (F#)
// Minimum primes. Nigel Galloway: October 29th., 2021
let N1,N2,N3=[5;45;23;21;67],[43;22;78;46;38],[9;98;12;54;53]
let fN g=primes32()|>Seq.find((<=)g)
printfn "%A" (List.zip3 N1 N2 N3|>List.map(fun(n,g,l)->fN(max (max n g) l)))
- Output:
[43; 101; 79; 59; 67]
Factor
USING: math math.order math.primes prettyprint sequences ;
{ 5 45 23 21 67 } { 43 22 78 46 38 } { 9 98 12 54 53 }
[ max max 1 - next-prime ] 3map .
- Output:
{ 43 101 79 59 67 }
FreeBASIC
#define MAX(a, b) iif((a) > (b), (a), (b))
Function isPrime(Byval ValorEval As Integer) As Boolean
If ValorEval < 2 Then Return False
If ValorEval Mod 2 = 0 Then Return ValorEval = 2
If ValorEval Mod 3 = 0 Then Return ValorEval = 3
Dim d As Integer = 5
While d * d <= ValorEval
If ValorEval Mod d = 0 Then Return False Else d += 2
Wend
Return True
End Function
Dim As Integer Num1(5) = { 5,45,23,21,67}
Dim As Integer Num2(5) = {43,22,78,46,38}
Dim As Integer Num3(5) = { 9,98,12,54,53}
Print "The minimum prime numbers of three lists..."
Print "[";
For n As Integer = 0 To 4
Dim As Integer maxi = MAX(num1(n), MAX(num2(n), num3(n)))
If (maxi Mod 2 = 0) Then maxi += 1
While Not isPrime(maxi)
maxi += 2
Wend
Print maxi; ", ";
Next n
Print !"\b\b ]"
Sleep
- Output:
[ 43, 101, 79, 59, 67 ]
Go
package main
import (
"fmt"
"rcu"
)
func main() {
numbers1 := [5]int{5, 45, 23, 21, 67}
numbers2 := [5]int{43, 22, 78, 46, 38}
numbers3 := [5]int{9, 98, 12, 54, 53}
primes := [5]int{}
for n := 0; n < 5; n++ {
max := rcu.Max(rcu.Max(numbers1[n], numbers2[n]), numbers3[n])
if max % 2 == 0 {
max++
}
for !rcu.IsPrime(max) {
max += 2
}
primes[n] = max
}
fmt.Println(primes)
}
- Output:
[43 101 79 59 67]
J
] numbers =. 3 5 $ 5 45 23 21 67 43 22 78 46 38 9 98 12 54 53
5 45 23 21 67
43 22 78 46 38
9 98 12 54 53
4 p: <: >./ numbers
43 101 79 59 67
jq
Works with gojq, the Go implementation of jq
This entry uses `is_prime` as defined, for example, at Erdős-primes#jq.
Two solutions are presented following these preliminaries:
include "is_prime"; # reminder
def Numbers1: [5,45,23,21,67];
def Numbers2: [43,22,78,46,38];
def Numbers3: [9,98,12,54,53];
# Generate primes in range(m;n) provided m>=2
def primes(m; n):
if m%2 == 0 then primes(m+1;n)
else range(m; n; 2) | select(is_prime)
end;
Explicit Iteration
[range(0;5)
| [Numbers1[.], Numbers2[.], Numbers3[.]] | max
| first(primes(.; infinite))]
Functional
[Numbers1, Numbers2, Numbers3]
| transpose
| [map(max | first(primes(.; infinite)))]
- Output:
[43,101,79,59,67]
Julia
using Primes
println(nextprime.(maximum(hcat([5,45,23,21,67], [43,22,78,46,38], [9,98,12,54,53]), dims=2)))
- Output:
[43; 101; 79; 59; 67;;]
Mathematica / Wolfram Language
minPrime[x_List] :=
If[PrimeQ@Max@x, Max@x, NextPrime@Max@x]
MapThread[
minPrime@{##} &, {{5., 45, 23, 21, 67}, {43, 22, 78, 46, 38}, {9, 98,
12, 54, 53}}]
- Output:
{43,101,79,59,67}
Nim
const
Numbers1 = [ 5, 45, 23, 21, 67]
Numbers2 = [43, 22, 78, 46, 38]
Numbers3 = [ 9, 98, 12, 54, 53]
var numbers: array[0..Numbers1.high, int]
template isEven(n: int): bool = (n and 1) == 0
func isPrime(n: Positive): bool =
if n < 2: return false
if n.isEven: 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
func minPrime(n: int): int =
if n == 2: return 2
result = if n.isEven: n + 1 else: n
while not result.isPrime():
inc result, 2
for i in 0..numbers.high:
let m = max(max(Numbers1[i], Numbers2[i]), Numbers3[i])
numbers[i] = minPrime(m)
echo numbers
- Output:
[43, 101, 79, 59, 67]
Perl
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Minimum_primes
use warnings;
use ntheory qw( next_prime );
use List::Util qw( max );
my @Numbers1 = (5,45,23,21,67);
my @Numbers2 = (43,22,78,46,38);
my @Numbers3 = (9,98,12,54,53);
my @Primes = map {
next_prime( max( $Numbers1[$_], $Numbers2[$_], $Numbers3[$_] ) - 1 )
} 0 .. 4;
print "@Primes\n";
- Output:
43 101 79 59 67
Phix
with javascript_semantics function nextprime(sequence s) sequence res = repeat(0,length(s[1])) for i=1 to length(res) do res[i] = get_prime(length(get_primes_le(maxsq(vslice(s,i))-1))+1) end for return res end function printf(1,"%v\n",{nextprime({{ 5, 45, 23, 21, 67}, {43, 22, 78, 46, 38}, { 9, 98, 12, 54, 53}})})
- Output:
{43,101,79,59,67}
Quackery
transpose
is defined at Matrix transposition#Quackery.
isprime
is defined at Primality by trial division#Quackery.
' [ 5 45 23 21 67 ]
' [ 43 22 78 46 38 ]
' [ 9 98 12 54 53 ]
3 pack transpose
[] swap witheach
[ unpack max max 1 -
[ 1+ dup isprime until ]
join ]
echo
- Output:
[ 43 101 79 59 67 ]
Raku
Seems kind of pointless to specify a maximum of 5 terms when there are only 5 elements in each list but... ¯\_(ツ)_/¯
say ([Zmax] <5 45 23 21 67>, <43 22 78 46 38>, <9 98 12 54 53>)».&next-prime[^5];
sub next-prime { ($^m..*).first: &is-prime }
- Output:
(43 101 79 59 67)
Ring
Solution #1
? "working..."
Num1 = [ 5,45,23,21,67]
Num2 = [43,22,78,46,38]
Num3 = [ 9,98,12,54,53]
n = len(Num1)
Nums = list(n)
for i = 1 to n
Nums[i] = nxtPrime(max([Num1[i], Num2[i], Num3[i]]))
next
? "The minimum prime numbers of three lists = " + fmtArray(Nums)
put "done..."
func fmtArray(ar)
rv = ar[1]
for n = 2 to len(ar) rv += "," + ar[n] next
return "[" + rv + "]"
func nxtPrime(x)
j = 2
while true
if x % j = 0 j = 2 x++
else j++ ok
if j * j > x exit ok
end return string(x)
- Output:
working... The minimum prime numbers of three lists = [43,101,79,59,67] done...
Solution #2
load "stdlib.ring"
see "working..." + nl
Primes = []
Numbers1 = [5,45,23,21,67]
Numbers2 = [43,22,78,46,38]
Numbers3 = [9,98,12,54,53]
for n = 1 to len(Numbers1)
Temp = []
add(Temp,Numbers1[n])
add(Temp,Numbers2[n])
add(Temp,Numbers3[n])
max = max(Temp)
max--
while true
max++
if isprime(max)
exit
ok
end
add(Primes,max)
next
see "Minimum primes = "
see showArray(Primes)
see nl + "done..." + nl
func showArray(array)
txt = ""
see "["
for n = 1 to len(array)
txt = txt + array[n] + ","
next
txt = left(txt,len(txt)-1)
txt = txt + "]"
see txt
- Output:
working... Minimum primes = [43,101,79,59,67] done...
RPL
By the letter
≪ {5 45 23 21 67} {43 22 78 46 38} {9 98 12 54 53} → numbers1 numbers2 numbers3
≪ numbers1 numbers2 numbers3 3 ≪ MAX MAX ≫ DOLIST
{ } → max primes
≪ 1 max SIZE FOR j
max j GET
IF DUP ISPRIME? NOT THEN NEXTPRIME END
'primes' SWAP STO+
NEXT primes
≫ ≫ ≫ 'TASK' STO
- Output:
1: {43 101 79 59 67}
Idiomatic
No need for local variables.
≪ {5 45 23 21 67} {43 22 78 46 38} {9 98 12 54 53}
3 ≪ MAX MAX ≫ DOLIST
≪ IF DUP ISPRIME? NOT THEN NEXTPRIME END ≫ MAP
≫ 'TASK' STO
Ruby
require "prime"
numbers1 = [ 5, 45, 23, 21, 67]
numbers2 = [43, 22, 78, 46, 38]
numbers3 = [ 9, 98, 12, 54, 53]
p [numbers1, numbers2, numbers3].transpose.map{|ar| (ar.max..).find(&:prime?) }
- Output:
[43, 101, 79, 59, 67]
Sidef
var lists = [
[ 5,45,23,21,67],
[43,22,78,46,38],
[ 9,98,12,54,53],
]
say lists.zip.map { next_prime(.max - 1) }
- Output:
[43, 101, 79, 59, 67]
Wren
import "./math" for Int
var numbers1 = [ 5, 45, 23, 21, 67]
var numbers2 = [43, 22, 78, 46, 38]
var numbers3 = [ 9, 98, 12, 54, 53]
var primes = List.filled(5, 0)
for (n in 0..4) {
var max = numbers1[n].max(numbers2[n]).max(numbers3[n])
if (max % 2 == 0) max = max + 1
while(!Int.isPrime(max)) max = max + 2
primes[n] = max
}
System.print(primes)
- Output:
[43, 101, 79, 59, 67]
XPL0
func IsPrime(N); \Return 'true' if N is a prime number
int N, I;
[if N <= 1 then return false;
for I:= 2 to sqrt(N) do
if rem(N/I) = 0 then return false;
return true;
];
int Numbers1, Numbers2, Numbers3, N, Max;
[Numbers1:= [5,45,23,21,67];
Numbers2:= [43,22,78,46,38];
Numbers3:= [9,98,12,54,53];
for N:= 0 to 4 do
[Max:= Numbers1(N);
if Numbers2(N) > Max then Max:= Numbers2(N);
if Numbers3(N) > Max then Max:= Numbers3(N);
while not IsPrime(Max) do Max:= Max+1;
IntOut(0, Max); ChOut(0, ^ );
];
]
- Output:
43 101 79 59 67