TheAlgorithms-Python

Форк
0
51 строка · 1.4 Кб
1
"""
2
This is a pure Python implementation of the P-Series algorithm
3
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series
4
For doctests run following command:
5
python -m doctest -v p_series.py
6
or
7
python3 -m doctest -v p_series.py
8
For manual testing run:
9
python3 p_series.py
10
"""
11

12
from __future__ import annotations
13

14

15
def p_series(nth_term: float | str, power: float | str) -> list[str]:
16
    """
17
    Pure Python implementation of P-Series algorithm
18
    :return: The P-Series starting from 1 to last (nth) term
19
    Examples:
20
    >>> p_series(5, 2)
21
    ['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25']
22
    >>> p_series(-5, 2)
23
    []
24
    >>> p_series(5, -2)
25
    ['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04']
26
    >>> p_series("", 1000)
27
    ['']
28
    >>> p_series(0, 0)
29
    []
30
    >>> p_series(1, 1)
31
    ['1']
32
    """
33
    if nth_term == "":
34
        return [""]
35
    nth_term = int(nth_term)
36
    power = int(power)
37
    series: list[str] = []
38
    for temp in range(int(nth_term)):
39
        series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1")
40
    return series
41

42

43
if __name__ == "__main__":
44
    import doctest
45

46
    doctest.testmod()
47

48
    nth_term = int(input("Enter the last number (nth term) of the P-Series"))
49
    power = int(input("Enter the power for  P-Series"))
50
    print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p")
51
    print(p_series(nth_term, power))
52

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.