f | from decimal import Decimal, getcontext, ROUND_HALF_UP | f | from decimal import Decimal, getcontext, ROUND_HALF_UP |
| import sys | | import sys |
| | | |
n | def compute_pi(precision): | n | def compute_pi(prec): |
| getcontext().prec = precision + 5 | | getcontext().prec = prec + 5 |
| coeff = 426880 * Decimal(10005).sqrt() | | C = 426880 * Decimal(10005).sqrt() |
| M = Decimal(1) | | M = Decimal(1) |
| L = Decimal(13591409) | | L = Decimal(13591409) |
| X = Decimal(1) | | X = Decimal(1) |
| K = Decimal(6) | | K = Decimal(6) |
n | total_sum = L | n | S = L |
| for k in range(1, precision): | | for k in range(1, prec): |
| M = M * (K ** 3 - 16 * K) / k ** 3 | | M = M * (K ** 3 - 16 * K) / k ** 3 |
| L += 545140134 | | L += 545140134 |
| X *= -262537412640768000 | | X *= -262537412640768000 |
| term = M * L / X | | term = M * L / X |
n | total_sum += term | n | S += term |
| if term == 0: | | if term == 0: |
| break | | break |
| K += 12 | | K += 12 |
n | return coeff / total_sum | n | pi = C / S |
| | | return pi |
| | | |
n | def series_sine(x, num_terms): | n | def sin_series(x, terms): |
| getcontext().prec += 2 | | getcontext().prec += 2 |
| x_power = x | | x_power = x |
| factorial = Decimal(1) | | factorial = Decimal(1) |
n | sine_result = x | n | sin_x = x |
| sign = -1 | | sign = -1 |
n | for i in range(3, num_terms * 2, 2): | n | for i in range(3, terms * 2, 2): |
| x_power *= x * x | | x_power *= x * x |
| factorial *= i * (i - 1) | | factorial *= i * (i - 1) |
| term = x_power / factorial | | term = x_power / factorial |
n | sine_result += sign * term | n | sin_x += sign * term |
| sign *= -1 | | sign *= -1 |
| if term == 0: | | if term == 0: |
| break | | break |
| getcontext().prec -= 2 | | getcontext().prec -= 2 |
n | return +sine_result | n | return +sin_x |
| | | |
n | def series_cosine(x, num_terms): | n | def cos_series(x, terms): |
| getcontext().prec += 2 | | getcontext().prec += 2 |
| x_power = Decimal(1) | | x_power = Decimal(1) |
| factorial = Decimal(1) | | factorial = Decimal(1) |
n | cosine_result = Decimal(1) | n | cos_x = Decimal(1) |
| sign = -1 | | sign = -1 |
n | for i in range(2, num_terms * 2, 2): | n | for i in range(2, terms * 2, 2): |
| x_power *= x * x | | x_power *= x * x |
| factorial *= i * (i - 1) | | factorial *= i * (i - 1) |
| term = x_power / factorial | | term = x_power / factorial |
n | cosine_result += sign * term | n | cos_x += sign * term |
| sign *= -1 | | sign *= -1 |
| if term == 0: | | if term == 0: |
| break | | break |
| getcontext().prec -= 2 | | getcontext().prec -= 2 |
n | return +cosine_result | n | return +cos_x |
| | | |
n | def significant_digit_round(value, digits): | n | def round_to_significant_digits(x, digits): |
| if value.is_zero(): | | if x.is_zero(): |
| return Decimal(0) | | return Decimal(0) |
| else: | | else: |
n | sign, digits_tuple, exponent = value.as_tuple() | n | sign, digits_tuple, exponent = x.as_tuple() |
| num_digits = len(digits_tuple) | | num_digits = len(digits_tuple) |
| adjusted_exponent = exponent + num_digits - digits | | adjusted_exponent = exponent + num_digits - digits |
| quantize_exp = Decimal('1e{}'.format(adjusted_exponent)) | | quantize_exp = Decimal('1e{}'.format(adjusted_exponent)) |
n | return value.quantize(quantize_exp, rounding=ROUND_HALF_UP) | n | return x.quantize(quantize_exp, rounding=ROUND_HALF_UP) |
| | | |
n | def perform_calculations(): | n | def main(): |
| angle_degrees = Decimal(sys.stdin.readline().strip()) | | A = Decimal(sys.stdin.readline().strip()) |
| precision = int(sys.stdin.readline().strip()) | | E = int(sys.stdin.readline().strip()) |
| if precision < 4 or precision > 1000: | | if E < 4 or E > 1000: |
| print('Precision must be between 4 and 1000') | | print('Precision E must be between 4 and 1000') |
| return | | return |
n | getcontext().prec = precision + 10 | n | getcontext().prec = E + 10 |
| pi_result = compute_pi(getcontext().prec) | | pi = compute_pi(getcontext().prec) |
| angle_radians = angle_degrees * pi_result / Decimal(200) | | angle_rad = A * pi / Decimal(200) |
| terms_required = precision + 5 | | terms = E + 5 |
| sine_value = series_sine(angle_radians, terms_required) | | sin_x = sin_series(angle_rad, terms) |
| cosine_value = series_cosine(angle_radians, terms_required) | | cos_x = cos_series(angle_rad, terms) |
| tangent_value = sine_value / cosine_value | | tan_x = sin_x / cos_x |
| tangent_value = significant_digit_round(tangent_value, precision) | | tan_x = round_to_significant_digits(tan_x, E) |
| tangent_string = '{0}'.format(tangent_value.normalize()) | | tan_str = '{0}'.format(tan_x.normalize()) |
| if '.' in tangent_string: | | if '.' in tan_str: |
| integer_part, fractional_part = tangent_string.split('.') | | integer_part, fractional_part = tan_str.split('.') |
| significant_digits = len(integer_part.lstrip('-') + fractional_p | | significant_digits = len(integer_part.lstrip('-') + fractional_p |
| art) | | art) |
| else: | | else: |
n | integer_part = tangent_string.lstrip('-') | n | integer_part = tan_str.lstrip('-') |
| significant_digits = len(integer_part) | | significant_digits = len(integer_part) |
n | if significant_digits < precision: | n | if significant_digits < E: |
| zeros_needed = precision - significant_digits | | zeros_needed = E - significant_digits |
| if '.' in tangent_string: | | if '.' in tan_str: |
| tangent_string += '0' * zeros_needed | | tan_str += '0' * zeros_needed |
| else: | | else: |
n | tangent_string += '.' + '0' * zeros_needed | n | tan_str += '.' + '0' * zeros_needed |
| print(tangent_string) | | print(tan_str) |
| if __name__ == '__main__': | | if __name__ == '__main__': |
t | perform_calculations() | t | main() |