The third simulation game of the 12th Blue Bridge Cup-Mutual Quality

1. Problem description:

From 1 to 2020, how many numbers are relatively prime to 2020, that is, how many numbers and 2020 have the greatest common divisor of 1.
Answer submission
This is a result of filling in the blanks, you only need to calculate the result and submit it. The result of this question is an integer. Only fill in this integer when submitting the answer, and fill in the extra content will not be scored.

2. Thinking analysis:

Analyzing the problem, we can know that we can traverse the range from 1 to 2019, and calculate the greatest common divisor with 2020. If the greatest common divisor is found to be 1, then it is relatively prime, just add 1 to the count. The essence is to solve the greatest common divisor

3. The code is as follows:

# 求解最大公约数
def gcd(a: int, b: int):
    while b != 0:
        t = b
        b = a % b
        a = t
    return a


if __name__ == '__main__':
    count = 0
    for i in range(1, 2020):
        if gcd(2020, i) == 1:
            count += 1
            # print(i)
    print(count)

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/115139839