Collect and sort square numbers in ascending order from three lists: Difference between revisions
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: note use of library 'ntheory') |
|||
Line 22: | Line 22: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
|||
<lang perl>#!/usr/bin/perl |
<lang perl>#!/usr/bin/perl |
||
Revision as of 06:20, 11 December 2021
- Task
Add and sort suare numbers in ascending order from three lists.
list[1] = [3,4,34,25,9,12,36,56,36]
list[2] = [2,8,81,169,34,55,76,49,7]
list[3] = [75,121,75,144,35,16,46,35]
Julia
vcat is list "addition" in Julia. <lang julia>lists = [
[3,4,34,25,9,12,36,56,36], [2,8,81,169,34,55,76,49,7], [75,121,75,144,35,16,46,35]
]
squares = reduce(vcat, [[s for s in list if isqrt(s)^2 == s] for list in lists]) sort!(squares) println(squares) </lang>
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Add_and_sort_square_numbers_in_ascending_order_from_three_lists use warnings; use ntheory qw( is_square ); use List::Util qw( sum );
my @lists = (
[3,4,34,25,9,12,36,56,36], [2,8,81,169,34,55,76,49,7], [75,121,75,144,35,16,46,35]);
my $sum = sum my @squares = grep is_square($_), sort { $a <=> $b } map @$_, @lists; print "sum $sum - @squares\n";</lang>
- Output:
sum 690 - 4 9 16 25 36 36 49 81 121 144 169
Phix
with javascript_semantics procedure squares(sequence lists) sequence res = sort(join(lists)), sq = {1} while res[$]>sq[$] do sq &= power(length(sq)+1,2) end while res = filter(res,"in",sq) printf(1,"Added:%,d, Sorted:%V\n",{sum(res),res}) end procedure squares({{3,4,34,25,9,12,36,56,36}, {2,8,81,169,34,55,76,49,7}, {75,121,75,144,35,16,46,35}})
- Output:
Added:690, Sorted:{4,9,16,25,36,36,49,81,121,144,169}
Raku
<lang perl6>my $s = cache sort ( my $l = ( cache flat
[3,4,34,25,9,12,36,56,36], [2,8,81,169,34,55,76,49,7], [75,121,75,144,35,16,46,35]
)).grep: * ∈ cache {$++²}…*>$l.max;
put 'Added - Sorted'; put ' ' ~ $s.sum ~ ' ' ~ $s.gist;</lang>
Added - Sorted 690 (4 9 16 25 36 36 49 81 121 144 169)
Ring
<lang ring> load "stdlib.ring"
see "working..." + nl list = list(3) list[1] = [3,4,34,25,9,12,36,56,36] list[2] = [2,8,81,169,34,55,76,49,7] list[3] = [75,121,75,144,35,16,46,35] Primes = []
for n = 1 to 3
for m = 1 to len(list[n]) if issquare(list[n][m]) add(Primes,list[n][m]) ok next
next
Primes = sort(Primes) showArray(Primes)
see nl + "done..." + nl
func issquare(x)
for n = 1 to sqrt(x) if x = pow(n,2) return 1 ok next return 0
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... [4,9,16,25,36,36,49,81,121,144,169] done...
Wren
<lang ecmascript>var lists = [
[3,4,34,25,9,12,36,56,36], [2,8,81,169,34,55,76,49,7], [75,121,75,144,35,16,46,35]
]
var squares = [] for (list in lists) {
for (e in list) { var s = e.sqrt.floor if (s * s == e) squares.add(e) }
} squares.sort() System.print(squares)</lang>
- Output:
[4, 9, 16, 25, 36, 36, 49, 81, 121, 144, 169]