All problems
Digamma Approximation
Problem
For a positive integer n, the digamma function satisfies psi(n) = -gamma + H_{n-1}, where gamma = 0.5772156649015329 is the Euler-Mascheroni constant and H_{n-1} is the (n-1)-th harmonic number.
Given n >= 1, return psi(n) using this exact integer formula.
Hint
psi(1) = -gamma and psi(n+1) = psi(n) + 1/n.
Test cases
case 1
in: [1]
expected: -0.5772156649015329
case 2
in: [2]
expected: 0.42278433509846713
case 3
in: [5]
expected: 1.5061176684318005
case 4
in: [10]
expected: 2.2517525890667214
Starter codePython
def digamma_integer(n):
# Your code here
pass4 test cases · run your code against them in the editor