MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _subsum

Function _subsum

maths/bailey_borwein_plouffe.py:58–83  ·  view source on GitHub ↗

Private helper function to implement the summation functionality. @param digit_pos_to_extract: digit position to extract @param denominator_addend: added to denominator of fractions in the formula @param precision: same as precision in main function @return: floating-point n

(
    digit_pos_to_extract: int, denominator_addend: int, precision: int
)

Source from the content-addressed store, hash-verified

56
57
58def _subsum(
59 digit_pos_to_extract: int, denominator_addend: int, precision: int
60) -> float:
61 # only care about first digit of fractional part; don't need decimal
62 """
63 Private helper function to implement the summation
64 functionality.
65 @param digit_pos_to_extract: digit position to extract
66 @param denominator_addend: added to denominator of fractions in the formula
67 @param precision: same as precision in main function
68 @return: floating-point number whose integer part is not important
69 """
70 total = 0.0
71 for sum_index in range(digit_pos_to_extract + precision):
72 denominator = 8 * sum_index + denominator_addend
73 if sum_index < digit_pos_to_extract:
74 # if the exponential term is an integer and we mod it by the denominator
75 # before dividing, only the integer part of the sum will change;
76 # the fractional part will not
77 exponential_term = pow(
78 16, digit_pos_to_extract - 1 - sum_index, denominator
79 )
80 else:
81 exponential_term = pow(16, digit_pos_to_extract - 1 - sum_index)
82 total += exponential_term / denominator
83 return total
84
85
86if __name__ == "__main__":

Callers 1

bailey_borwein_plouffeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected