程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python training camp] Python daily practice -- day 21: promised score (GCD function application)

編輯:Python


Hello, Hello, my name is Dream ah , An interesting Python Blogger , Please go easy on me
2021 Blog star of the year TOP100,2021 Blog star of the year TOP5,Python Quality creators in the field , Welcome to cooperate with me ( At the end of the article VX Want to join the learning exchange group or Learning materials welcome +++)
Introductory notes : This paradise never lacks genius , Hard work is your final ticket !
Last , May we all shine where we can't see , Let's make progress together
“ Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place ”, It's me ! Ha ha ha ~

Preface :【Python Training camp 】 Is aimed at Python A war created by language learning Brush questions and revel party! If you don't grasp the solid foundation of knowledge , Welcome to this course :Python Public class It's best to use it together ~ If you like, hurry up and subscribe ! If you don't have self-control or motivation to learn and communicate together , Welcome to send a private letter or add my... At the end of the text VX, I will pull you into the learning exchange group , Let's exchange and study together , Group punch in

Python Practice every day :

  • Title Description
  • Their thinking
  • The source code to share
  • Learning summary
  • The articles ---- Good article recommends

Title Description

Title Description
This question is to fill in the blanks , Just calculate the result , Use the output statement in the code to output the filled results .

If the greatest common divisor of the numerator and denominator of a fraction is 1, This fraction is called a reduced fraction .

Excuse me, , How many committed scores are there , The numerator and denominator are 1 To 2020 Integer between ( Include 1 and 2020)?

 Operation limit
Maximum operation time :2s
Maximum running memory : 128M

Their thinking

  • A two-level loop traverses two numbers to find their greatest common multiple , yes 1 It meets the requirements
  • math.gcd() Method returns two integers int1 and int2 Maximum common divisor of .math.gcd(int1,int2)

The source code to share

# Time : 2022/2/15 13:07
# File : reduced fraction .py
# Author : yes Dream ah !
# VX : Xu18300396393
# Ten thousand times sad , There will still be Dream, I've been waiting for you in the warmest place !
import math
count = 0
for i in range(1,2021):
for j in range(1,2021):
if math.gcd(i,j) == 1:
count += 1
print(count)

Learning summary

1. greatest common divisor greatest common divisor

import math
# Find the greatest common divisor of these two integers 
print (math.gcd(3, 6))
print (math.gcd(6, 12))
print (math.gcd(12, 36))
print (math.gcd(-12, -36))
print (math.gcd(5, 12))
print (math.gcd(10, 0))
print (math.gcd(0, 34))
print (math.gcd(0, 0))

math.gcd() Method returns two integers int1 and int2 Maximum common divisor of .

Tips :gcd(0,0) return 0.

2. Minimum common multiple lowest common multiple
Pay special attention here ,math There is nothing like finding the greatest common divisor in a function gcd Function to find the least common multiple !!!
lcm() Function in math It doesn't exist in the library , So we need to rely on gcd() Library to indirectly complete the work of finding the least common multiple !
Because division has a decimal point , So we need to add int Convert to an integer .

import math
a = 25
b = 35
print(type((a * b) / math.gcd(a, b)))
# <class 'float'>
import math
a = 25
b = 35
print(int((a * b) / math.gcd(a, b)))

Today is my day Python The first of the training camp 21 God , I hope to see the best of you every day

The articles ---- Good article recommends

[Python Public class ] Zero base play Python The basic chapter ---- Section 1 :Python The introduction of

[Python Public class ] Zero base play Python Advanced ---- Section 1 :Python File operations in

Come on, let's have a problem swiping Carnival party Well !----【Python Training camp 】
All right. , That's all I want to share with you today
️️️ If you like , Don't be stingy with your one key triple connection ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved