Skip to main content
added 318 characters in body
Source Link
miles
  • 17k
  • 2
  • 30
  • 94

J, 149 bytes

*[:*/1(-1%~~:)&.@qq:

Instead of using the gcd formula, I useThis is based on the product formula

Formula http://i.imgur.com/MX9dJQS.pngJsoftware's essay on totient functions.

Given n = p1e1p2e2 ∙∙∙ pkek where p iterates over eachk is a prime factor of the distinct primes that divide n, the totient function φ(n) = φ(p1e1) ∙ φ(p2e2) ∙∙∙ φ(pkek) = (p1 - 1) p1e1 - 1 ∙ (p2 - 1) p2e2 - 1 ∙∙∙ (pk - 1) pkek - 1.

Usage

Note: Extra commands used for formatting multiple input/output.

   f =: *[:*/1(-1%~~:)&.@qq:
   (,.f"0) 1 2 3 8 9 26 44 105
  1  1
  2  1
  3  2
  8  4
  9  6
 26 12
 44 20
105 48
   f 12345
6576

Explanation

*[:*/1(-1%~~:)&.@qq:  Input: integer n
            q:  Prime decomposition. Get the prime factors of n
   whose product is n
(   ~.@ )&   Get only theOperate distincton valuesthem
 in that~: list
       1% Nub-sieve. Create a mask where 1 Computeis the reciprocal for eachfirst primeoccurrence
     1-        of Subtracta eachunique primevalue reciprocaland from0 1elsewhere
 [:*/ -          Reduce theSubtract listelementwise usingbetween multiplication
*the prime factors and the mask
     &.q:  Perform the inverse Multiplyof thatprime productdecomposition by(Product nof andthe returnvalues)

J, 14 bytes

*[:*/1-1%~.@q:

Instead of using the gcd formula, I use the product formula

Formula http://i.imgur.com/MX9dJQS.png

where p iterates over each of the distinct primes that divide n.

Usage

Note: Extra commands used for formatting multiple input/output.

   f =: *[:*/1-1%~.@q:
   (,.f"0) 1 2 3 8 9 26 44 105
  1  1
  2  1
  3  2
  8  4
  9  6
 26 12
 44 20
105 48
   f 12345
6576

Explanation

*[:*/1-1%~.@q:  Input: n
            q:  Get the prime factors of n
         ~.@    Get only the distinct values in that list
       1%       Compute the reciprocal for each prime
     1-         Subtract each prime reciprocal from 1
 [:*/           Reduce the list using multiplication
*               Multiply that product by n and return

J, 9 bytes

(-~:)&.q:

This is based on the Jsoftware's essay on totient functions.

Given n = p1e1p2e2 ∙∙∙ pkek where pk is a prime factor of n, the totient function φ(n) = φ(p1e1) ∙ φ(p2e2) ∙∙∙ φ(pkek) = (p1 - 1) p1e1 - 1 ∙ (p2 - 1) p2e2 - 1 ∙∙∙ (pk - 1) pkek - 1.

Usage

   f =: (-~:)&.q:
   (,.f"0) 1 2 3 8 9 26 44 105
  1  1
  2  1
  3  2
  8  4
  9  6
 26 12
 44 20
105 48
   f 12345
6576

Explanation

(-~:)&.q:  Input: integer n
       q:  Prime decomposition. Get the prime factors whose product is n
(   )&     Operate on them
  ~:         Nub-sieve. Create a mask where 1 is the first occurrence
             of a unique value and 0 elsewhere
 -           Subtract elementwise between the prime factors and the mask
     &.q:  Perform the inverse of prime decomposition (Product of the values)
Source Link
miles
  • 17k
  • 2
  • 30
  • 94

J, 14 bytes

*[:*/1-1%~.@q:

Instead of using the gcd formula, I use the product formula

Formula http://i.imgur.com/MX9dJQS.png

where p iterates over each of the distinct primes that divide n.

Usage

Note: Extra commands used for formatting multiple input/output.

   f =: *[:*/1-1%~.@q:
   (,.f"0) 1 2 3 8 9 26 44 105
  1  1
  2  1
  3  2
  8  4
  9  6
 26 12
 44 20
105 48
   f 12345
6576

Explanation

*[:*/1-1%~.@q:  Input: n
            q:  Get the prime factors of n
         ~.@    Get only the distinct values in that list
       1%       Compute the reciprocal for each prime
     1-         Subtract each prime reciprocal from 1
 [:*/           Reduce the list using multiplication
*               Multiply that product by n and return