Haskell, 49 46 44 40 bytes
Thanks @xnor for another again named solution:
x%0=x
x%n=sum[1|1<-gcd x<$>[1..x]]%(n-1)
Old version:
The following is an unnamed pointless function that takes two arguments: (Thanks @Lynn for another two bytes!)
(!!).iterate(\y->sum[1|t<-[1..y],gcd y t<2])
To use it, you can e.g. assign it a name (f=(!!).(...
) and then call it via f 10 1
(as an example for the first test case).
Explanation: The lambda function (\y->sum[1|t<-[1..y],gcd y t<2])
is the totient function. iterate f x
produces an infinite list [x,f(x),f(f(x)),f(f(f(x))),...]
and !!
is just for accessing this list at a specific index.
Older version:
x#n=(iterate(\y->sum[1|t<-[1..y],gcd y t<2])x)!!n
Usage e.g. for the first test case: 10#1
.
1Extremely closely related, almost as closely related. – Peter Taylor – 2016-06-16T09:49:21.523
You know, people who does not use the proper name (totient) makes it hard to find. – Leaky Nun – 2016-06-16T09:51:35.253
1I found both of those by searching for totient – Peter Taylor – 2016-06-16T10:04:34.960
@PeterTaylor I see. I only searched the question. – Leaky Nun – 2016-06-16T10:08:55.507
5>
@Sp3000 Oops... let's assume that it is a phi, lol. – Leaky Nun – 2016-06-16T10:11:13.737
His avatar is actually the sum of divisors function - OEIS sequence A000203
– Mego – 2016-06-16T10:18:51.137