Minimum primes: Difference between revisions
m (→{{header|Ring}}: streamlined) |
(Undo revision 345818 by Enter your username (talk)) |
||
Line 178: | Line 178: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<lang ring> |
<lang ring> |
||
load "stdlib.ring" |
|||
see "working..." + nl |
|||
Primes = [] |
|||
Num1 = [ 5,45,23,21,67] |
|||
Numbers1 = [5,45,23,21,67] |
|||
Numbers2 = [43,22,78,46,38] |
|||
Numbers3 = [9,98,12,54,53] |
|||
n = len(Num1) |
|||
Nums = list(n) |
|||
⚫ | |||
Nums[i] = nxtPrime(max([Num1[i], Num2[i], Num3[i]])) |
|||
⚫ | |||
⚫ | |||
? "The maximum prime numbers of three lists = " + fmtArray(Nums) |
|||
⚫ | |||
⚫ | |||
add(Temp,Numbers1[n]) |
|||
add(Temp,Numbers2[n]) |
|||
add(Temp,Numbers3[n]) |
|||
max = max(Temp) |
|||
max-- |
|||
while true |
|||
max++ |
|||
if isprime(max) |
|||
⚫ | |||
ok |
|||
end |
|||
add(Primes,max) |
|||
⚫ | |||
see "Minimum primes = " |
|||
func fmtArray(ar) |
|||
see showArray(Primes) |
|||
⚫ | |||
see nl + "done..." + nl |
|||
for n = 2 to len(ar) rv += "," + ar[n] next |
|||
⚫ | |||
func |
func showArray(array) |
||
txt = "" |
|||
see "[" |
|||
for n = 1 to len(array) |
|||
txt = txt + array[n] + "," |
|||
next |
|||
⚫ | |||
txt = left(txt,len(txt)-1) |
|||
end return string(x)</pre> |
|||
⚫ | |||
see txt |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
working... |
|||
Minimum primes = [43,101,79,59,67] |
|||
⚫ | |||
</pre> |
|||
=={{header|Wren}}== |
=={{header|Wren}}== |
Revision as of 03:01, 29 October 2021
- 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).
- Select minPrime for that minPrime is a minimal prime where 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. <lang algol68>BEGIN # show the minimum prime >= the maximum elements of three lists #
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 numbers2 = ( 43, 22, 78, 46, 38 ); []INT numbers3 = ( 9, 98, 12, 54, 53 ); [ 1 : UPB numbers1 ]INT prime list; 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; # 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</lang>
- Output:
[ 43 101 79 59 67 ]
C
<lang 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;
}</lang>
- Output:
43 101 79 59 67
Factor
<lang factor>USING: arrays math math.primes prettyprint sequences ;
{ 5 45 23 21 67 } { 43 22 78 46 38 } { 9 98 12 54 53 } 3array flip [ supremum 1 - next-prime ] map .</lang>
- Output:
{ 43 101 79 59 67 }
Go
<lang 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)
}</lang>
- Output:
[43 101 79 59 67]
Julia
<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)))
</lang>
- Output:
[43; 101; 79; 59; 67;;]
Perl
<lang 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";</lang>
- 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... ¯\_(ツ)_/¯
<lang perl6>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 }</lang>
- Output:
(43 101 79 59 67)
Ring
<lang ring> 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
</lang>
- Output:
working... Minimum primes = [43,101,79,59,67] done...
Wren
<lang ecmascript>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)</lang>
- Output:
[43, 101, 79, 59, 67]