Coprimes: Difference between revisions

Content added Content deleted
m (→‎{{header|Ruby}}: change variable names)
Line 874: Line 874:
60 and 15 are not coprime.
60 and 15 are not coprime.
</pre>
</pre>

=={{header|Racket}}==

There is a coprime? function in the math/number-theory library to show off (more useful if you're using typed racket).

<lang racket>#lang racket/base

;; Rename only necessary so we can distinguish it
(require (rename-in math/number-theory [coprime? number-theory/coprime?]))

(define (gcd/coprime? . ns)
(= 1 (apply gcd ns)))

(module+ main
(define ((Coprimes name coprime?) test)
(printf "~a: ~a -> ~a~%" name (cons 'coprime? test) (apply coprime? test)))
(define tests '([21 15] [17 23] [36 12] [18 29] [60 15] [21 15 27] [17 23 46]))

(for-each (λ (n f) (for-each (Coprimes n f) tests))
(list "math/number-theory"
"named gcd-based function"
"anonymous gcd-based function")
(list number-theory/coprime?
gcd/coprime?
(λ ns (= 1 (apply gcd ns))))))</lang>

{{out}}

<pre>math/number-theory: (coprime? 21 15) -> #f
math/number-theory: (coprime? 17 23) -> #t
math/number-theory: (coprime? 36 12) -> #f
math/number-theory: (coprime? 18 29) -> #t
math/number-theory: (coprime? 60 15) -> #f
math/number-theory: (coprime? 21 15 27) -> #f
math/number-theory: (coprime? 17 23 46) -> #t
named gcd-based function: (coprime? 21 15) -> #f
named gcd-based function: (coprime? 17 23) -> #t
named gcd-based function: (coprime? 36 12) -> #f
named gcd-based function: (coprime? 18 29) -> #t
named gcd-based function: (coprime? 60 15) -> #f
named gcd-based function: (coprime? 21 15 27) -> #f
named gcd-based function: (coprime? 17 23 46) -> #t
anonymous gcd-based function: (coprime? 21 15) -> #f
anonymous gcd-based function: (coprime? 17 23) -> #t
anonymous gcd-based function: (coprime? 36 12) -> #f
anonymous gcd-based function: (coprime? 18 29) -> #t
anonymous gcd-based function: (coprime? 60 15) -> #f
anonymous gcd-based function: (coprime? 21 15 27) -> #f
anonymous gcd-based function: (coprime? 17 23 46) -> #t</pre>


=={{header|Raku}}==
=={{header|Raku}}==