##Haskell, 28 bytes
Haskell, 28 bytes
f n=sum[1|1<-gcd n<$>[1..n]]
Uses Haskell's pattern matching of constants. The tricks here are fairly standard for golfing, but I'll explain to a general audience.
The expression gcd n<$>[1..n]
maps gcd n
onto [1..n]
. In other words, it computes the gcd
with n
of each number from 1
to n
:
[gcd n i|i<-[1..n]]
From here, the desired output is the number of 1
entries, but Haskell lacks a count
function. The idiomatic way to filter
to keep only 1
's, and take the resulting length
, which is much is too long for golfing.
Instead, the filter
is simulated by a list comprehension [1|1<-l]
with the resulting list l
. Usually, list comprehensions bind values onto variable like in [x*x|x<-l]
, but Haskell allows a pattern to be matched against, in this case the constant 1
.
So, [1|1<-l]
generating a 1
on each match of 1
, effectively extracting just the 1
's of the original list. Calling sum
on it gives its length.