Collect and sort square numbers in ascending order from three lists

From Rosetta Code
Revision as of 08:02, 10 December 2021 by CalmoSoft (talk | contribs) (Created page with "{{Draft task}} ;Task: <br> Add abd sort suare numbers in ascending order from three lists. <br>list[1] = [3,4,34,25,9,12,36,56,36] <br>list[2] = [2,8,81,169,34,55,76,49,7] <b...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Collect and sort square numbers in ascending order from three lists 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


Add abd 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]

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...