Skip to content

Sync totient() recipe with the standard library docs #859

@rhettinger

Description

@rhettinger

The update step is now n -= n // prime which is a little simpler and faster.

def totient(n):
   "Count of natural numbers up to n that are coprime to n."
   # https://mathworld.wolfram.com/TotientFunction.html
   # totient(12) → 4 because len([1, 5, 7, 11]) == 4
   for prime in set(factor(n)):
       n -= n // prime
   return n

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions