Skip to main content
Commonmark migration
Source Link

##Python 2, 44 bytes

Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

##Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

##Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius functiongolf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

##Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

##Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

edited body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

##Python 2, 4544 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

##Python 2, 45 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

##Python 2, 44 bytes

f=lambda n,d=1:d/n or-f(d)*(n%d<1)-~f(n,d+1) 

Less golfed:

f=lambda n:n-sum(f(d)for d in range(1,n)if n%d<1) 

Uses the formula that the Euler totients of the divisors of n have a sum of n:

enter image description here

The value of ϕ(n) can then be recursively computed as n minus the sum over nontrivial divisors. Effectively, this is doing Möbius inversion on the identity function. I used the same method in a golf to compute the Möbius function.

Thanks to Dennis for saving 1 byte with a better base case, spreading the initial value of +n into +1 for each of the n loops, done as -~.

added 277 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
edited body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
added 73 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading