This is my homework on the college lab.
Experiment One
decomposes a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.
First of all, let's talk about my design ideas:
Design ideas:
1. First you need to determine whether the number entered is a digit ( with isdigit ()) If it is a number continue to go down, otherwise re-enter
2. Write a decomposition factorization function and return the result to a list
3. Use a For loop to get the format of the topic in the form of a string addition
4. Finally print out the results
Here is my code, first time learning, a little bit long.
1 Related code:2 defjudeg_prime (num):3 " "determines if num is a prime number, or returns False if it returns True" "4number_list=[]5 forIinchRange (1,num+1):6 ifnum%i==0:7 number_list.append (i)8 ifLen (number_list) ==2 andNumber_list[0]==1 andnumber_list[1]==Num:9 returnTrueTen Else: One returnFalse
View Code
1 defA (num):2 " "decomposes num (not 0 and non-prime number) factorization and returns a list" "3 forIinchRange (2,num+1):4 ifnum%i==0:5 prime_list.append (i)6Num=int (num/i)7 ifjudeg_prime (num):8 #If NUM is a prime number, it is added after the list and returns9 prime_list.append (num)Ten Break One Else:#Otherwise, the function A () continues to execute A returnA (num) - returnPrime_list
View Code
1 " "execution port of the program" "2 whileTrue:3Prime_list = []4 Print('-----Decomposition Factorization-----')5Number=input ('Enter a positive integer (enter 0 to exit the program):')6 ifNumber.isdigit ():#determine if number is numeric to avoid error7Number=Int (number)8 ifnumber==0:9 Print('The program is over! ')Ten Break One elifNumber = = 1:#if the input is 1 A Print('1=1*1') - elifJudeg_prime (number):#if the input is a prime number - Print('%s=1*%s'%(number, number)) the Else: -Count=1 -Prime_list =A (number) - #Divide number into factorization and return to Prime_list +PRIMER=STR (number) +'=' - forIinchprime_list: + ifcount==Len (prime_list): Aprimer=primer+Str (i) at Else: -Primer = primer + str (i) +'*' -Count+=1 - Print(primer)#Print Results - Else: - Print('Please enter a positive integer')
View Code
I feel that writing is not concise, please forgive me
Python Learning Path 2