Totient function: Difference between revisions

Content added Content deleted
(→‎{{header|BQN}}: Link to primes.bqn version; editing)
Line 393: Line 393:
GCD function is taken from BQNcrate.
GCD function is taken from BQNcrate.


The totient function is similar to APL and J, except it is made as a train. An explicit version is <code>{+´1=𝕩GCD¨1+↕𝕩}</code>
The totient function is similar to APL and J, except it is made as a train. An explicit version of <code>Totient</code> is <code>{+´1=𝕩GCD¨1+↕𝕩}</code>
<lang bqn>GCD ← {𝕨(|𝕊⍟(>⟜0)⊣)𝕩}
<lang bqn>GCD ← {𝕨(|𝕊⍟(>⟜0)⊣)𝕩}
Totient ← +´1=⊢GCD¨1+↕</lang>
(function block)
{{out|Usage}}
Totient ← +´1=⊢GCD¨1+
<lang bqn> Totient¨1+↕25
+´1=⊢(function block)¨1+↕
Totient¨1+↕25
⟨ 1 1 2 2 4 2 6 4 6 4 10 4 12 6 8 8 16 6 18 8 12 10 22 8 20 ⟩
⟨ 1 1 2 2 4 2 6 4 6 4 10 4 12 6 8 8 16 6 18 8 12 10 22 8 20 ⟩


Line 409: Line 406:
"Prime?" 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0
"Prime?" 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 0
┘</lang>
┘</lang>

The [https://github.com/mlochbaum/bqn-libs/blob/master/primes.bqn primes] library from bqn-libs includes a <code>Totient</code> function based on factoring that's much faster for numbers in the hundreds and above.


=={{header|C}}==
=={{header|C}}==