Start the python Tour for Newbies
//Given a radius, find the area and circumference of a circle r = int(input('plz input a nunber:')) p = 2 * 3.14 * r s = 3.14 * r ** r print('The premeter of circle is %d'%p) print('The square of circle is %d'%s) #Print in ascending order from small to large after entering two numbers to compare sizes x = int(input('plz input a nunber:')) y = int(input('plz input the other nunber:')) if x >= y: print('The ascending sort is %d>%d'%(x,y)) else: print('The ascending sort is %d>%d'%(y,x)) #Input a number of integers in turn, print out the maximum value, if the input is empty, then exit the program. _max = None for i in range(100): y = input('plz input a nunber:') if not y: break x = int(y) if i == 0: _max = x if x >= _max: _max = x if _max == None: print('The first input is None') else: print("The greattest number is %d"%_max) #Number of input n, average number of input n times sum = 0 n = int(input('plz input the count:')) for i in range(n): num = int(input('plz input a number:')) sum += num print(sum / n) #Print a square with a side length of n n = 5 for i in range(1,n+1): if i == 1: print('*' * n) elif i == n: print('*' * n) else: print('*' + ' ' * (n - 1) + '*') #Find the sum of all odd numbers in 100 sum = 0 for i in range(1,101): if i & 1: sum += i print(sum) #Judging students'grades, grade A-E... score = int(input('plz input a number:')) if score < 60: print('the level is %s'%'E') elif score >= 60 and score < 70: print('the level is %s'%'D') elif score >= 70 and score < 80: print('the level is %s'%'C') elif score >= 80 and score < 90: print('the level is %s'%'B') else: print('the level is %s'%'A') #Find the sum of factorial 1 to 5 sum = 0 j = 1 for i in range(1,6): j = j * i #print(j) sum += j print(sum) #Solve whether the number of inputs is prime x = int(input('>>>')) for i in range(2,x): if x == 2: print(x,'is a prime number') if x % i == 0: print(x,'is not a prime number') break else: print(x,'is a prime number') #Print Nine-Nine Multiplication Table for i in range(1,10): for j in range(1,i+1): print('%d*%d=%d'%(i,j,i*j),end=' ') print() #Print the diamond as shown below #Print out the diamond as shown below for i in range(0,7): if i == 7 // 2: print('*' * (2 * i+1)) elif i < 7 // 2: print(' ' * (3 - i) + '*' * (2 * i + 1) + ' ' * (3 - i)) else: a = 6 - i print(' ' * (3 - a) + '*' * (2 * a + 1) + ' ' * (3 - a))
Interesting Algorithmic Implementation
Problem Description: Solving item 101 of Fibolacci sequence
- Non-recursive implementation
#Solving item 101 of Fibolacci sequence, non-recursive implementation for a in range(0,101): if a == 0: i = 1 #print(i,end=' ') elif a == 1: j = 1 #print(j,end=' ') elif a % 2 == 0:# When a is even, the value of i is output. i = i + j #print(i,end=' ')#When a is odd, the value of j is output. else: j = j + i #print(j,end=' ') if a == 100: print(i)
- Recursive implementation
def Factorial(n): if n==1: return 1 elif n == 2: return 1 else: return Factorial(n-2) + Factorial(n-1) result=Factorial(101) print('The value of item 101 of the Fibolacci sequence is%d'%result)
Problem Description: Solve all prime numbers within 100000
Difficulty analysis: In order to operate data on the scale of 10000, we need to take into account the time complexity optimization of the program. There are three algorithms to implement them. We can count the execution time of the two algorithms. Let's compare how different algorithms affect the performance.
- The first algorithm
#Solve all prime numbers within 10 W import datetime count = 0 start = datetime.datetime.now() for i in range(2,100000): flag = True#Sign position if i == 2: print(i) else: for j in range(2,i): if i % j == 0: flag = False break if flag == True: print(i) count += 1 if i >= 100000: break end = datetime.datetime.now() count += 1 print(end - start) print("From 0 to 100 includes %d prime numeber"%count)
- The second algorithm
#Solve all prime numbers within 10 W (increasing time complexity by the number of the last prime number is 1,3,7,9?) #It is also possible to determine whether the number is odd first, and then within the range of odd numbers to determine whether it can only be divisible by 1 or itself, which will not be repeated here. count = 0 for i in range(2,100): flag = True#Sign position if i == 2 or i == 5: print(i) else: #Number of digits for judging i for a in range(1,6): if i // 10 ** a == 0: bit = a break #Determine whether the last position of i is 1, 3, 7, 9 for x in range(1,10): if(i - x) // 10 == 0: last_bit = x #Screen out numbers that are not prime numbers if last_bit == 1 or last_bit == 3 or last_bit == 7 or last_bit == 9:#It can also be judged that the end is odd and not equal to 5. #Get prime numbers for j in range(2,i): if i % j == 0: flag = False break if flag == True: print(i) count += 1 count += 2 print("From 0 to 100 includes %d prime numeber"%count)
- The third algorithm
#Solving prime numbers within 1000000 import datetime print(2) print(3) pn = [3] count = 1 start = datetime.datetime.now() for i in range(5,100000,2): flag = True#Sign position for j in range(count): if i % pn[j] == 0: flag = False break if flag == True: print(i) pn.append(i) count += 1 if i >= 100000: break end = datetime.datetime.now() count += 1 print(end - start) print("From 0 to 100 includes %d prime numeber"%count)
Readers may wonder which of the three algorithms has the shortest execution time.
Okay, here's the answer:
- The execution time of the first algorithm is 0:00:41.071175.
- The execution time of the third algorithm is 0:00:40.491875.
- The execution time of the third algorithm is 0:00:05.785494.
Summary 1: The execution time of the first and second algorithms is close to each other. Under the data scale of 10w, the execution time of the first two algorithms is eight times as much as that of the third algorithm. Imagine how important the optimization of algorithms and performance is when the data scale reaches hundreds of millions or even tens of billions!
Summary 2: The third algorithm idea:
- Definition of prime number: A prime number is a number that cannot be divisible by other numbers except 1 and itself.
- Conjugate Definition: Conjugate must be divisible by Prime number, and a number that is not a prime number must be divisible by its own prime number (except 1).
- The prime number must not be even, the prime number must be odd, and the odd number may not be prime.