Minimum primes: Difference between revisions
Thundergnat (talk | contribs) (→{{header|Raku}}: Add a Raku example) |
(Added Wren) |
||
Line 78: | Line 78: | ||
Minimum primes = [43,101,79,59,67] |
Minimum primes = [43,101,79,59,67] |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
{{libheader|Wren-math}} |
|||
<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> |
|||
{{out}} |
|||
<pre> |
|||
[43, 101, 79, 59, 67] |
|||
</pre> |
</pre> |
Revision as of 18:07, 27 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 and minPrime >= max
- Add minPrime to a new list (Primes)
- Show Primes on this page.
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;;]
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]