Coprimes: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Phix}}: added the 5-element test)
Line 37: Line 37:
<!--<lang Phix>(phixonline)-->
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">gcd1</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: #008080;">return</span> <span style="color: #7060A8;">gcd</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: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">gcd1</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: #008080;">return</span> <span style="color: #7060A8;">gcd</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: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">gcd1</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">({{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">23</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">60</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">22</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">31</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">143</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">gcd1</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</lang>-->
{{out}}
{{out}}
<pre>
<pre>
{{17,23},{18,29}}
{{17,23},{18,29},{21,22,25,31,143}}
</pre>
</pre>



Revision as of 15:15, 20 April 2021

Coprimes 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

p and q are coprimes if they have no common factors other than 1.
Let input: [21,15],[17,23],[36,12],[18,29],[60,15]


Go

Library: Go-rcu

Uses the same observation as the Wren entry. <lang go>package main

import (

   "fmt"
   "rcu"

)

func main() {

   pairs := [][2]int{{21, 15}, {17, 23}, {36, 12}, {18, 29}, {60, 15}}
   fmt.Println("The following pairs of numbers are coprime:")
   for _, pair := range pairs {
       if rcu.Gcd(pair[0], pair[1]) == 1 {
           fmt.Println(pair)
       }
   }

}</lang>

Output:
The following pairs of numbers are coprime:
[17 23]
[18 29]

Phix

function gcd1(sequence s) return gcd(s)=1 end function
?filter({{21,15},{17,23},{36,12},{18,29},{60,15},{21, 22, 25, 31, 143}},gcd1)
Output:
{{17,23},{18,29},{21,22,25,31,143}}

Raku

How do you determine if numbers are co-prime? Check to see if the Greatest common divisor is equal to one. Since we're duplicating tasks willy-nilly, lift code from that task, (or in this case, just use the builtin).

<lang perl6>say .raku, ( [gcd] |$_ ) == 1 ?? ' Coprime' !! for [21,15],[17,23],[36,12],[18,29],[60,15],[21,22,25,31,143]</lang>

[21, 15]
[17, 23] Coprime
[36, 12]
[18, 29] Coprime
[60, 15]
[21, 22, 25, 31, 143] Coprime

Ring

<lang ring> see "working..." + nl row = 0 Coprimes = [[21,15],[17,23],[36,12],[18,29],[60,15]] input = "input: [21,15],[17,23],[36,12],[18,29],[60,15]" see input + nl see "Coprimes are:" + nl

lncpr = len(Coprimes) for n = 1 to lncpr

   flag = 1
   if Coprimes[n][1] < Coprimes[n][2]
      test = Coprimes[n][1]
   else
      test = Coprimes[n][2]
   ok
   for m = 2 to test
       if Coprimes[n][1]%m = 0 and Coprimes[n][2]%m = 0 
          flag = 0
          exit
       ok 
   next  
   if flag = 1
      row = row + 1
      see "" + Coprimes[n][1] + " " + Coprimes[n][2] + nl
   ok     

next

see "Found " + row + " coprimes" + nl see "done..." + nl </lang>

Output:
working...
input: [21,15],[17,23],[36,12],[18,29],[60,15]
Coprimes are:
17 23
18 29
Found 2 coprimes
done...

Wren

Library: Wren-math

Two numbers are coprime if their GCD is 1. <lang ecmascript>import "/math" for Int

var pairs = [[21,15],[17,23],[36,12],[18,29],[60,15]] System.print("The following pairs of numbers are coprime:") for (pair in pairs) if (Int.gcd(pair[0], pair[1]) == 1) System.print(pair)</lang>

Output:
The following pairs of numbers are coprime:
[17, 23]
[18, 29]