How to check if a sequence of numbers is a Harmonic progression (reciprocals Arithmetic progression) in Python

1 Answer

0 votes
# a, b, c, d are HP if 1/a, 1/b, 1/c, 1/d are AP -> 1/a, 1/(a+diff), 1/(a+2*diff)...

def is_harmonic_progression(lst):
    size = len(lst)
    if lst == 1:
        return True

    diff = (1.0 / lst[1]) - (1.0 / lst[0])

    for i in range(1, size):
        if (1.0 / lst[i]) - (1.0 / lst[i - 1]) != diff:
            return False

    return True

lst = [1.0 / 5, 1.0 / 10, 1.0 / 15, 1.0 / 20, 1.0 / 25]

print("Yes" if is_harmonic_progression(lst) else "No")



'''
run:

Yes

'''

 



answered Sep 22, 2024 by avibootz
...