[Python demo] [17] [Experimental Example] [exercise a positive integer decomposition of the quality factor]

Title: a positive integer decomposition of the quality factor. For example: the input 90, to print out 90 = 3 * 2 * 3 * 5.

 

 

 

My source code:

# ! / Usr / bin / Python 
# encoding = UTF. 8- 
# - * - Coding: UTF-. 8 - * - 

# a positive integer decomposition of the quality factor. For example: the input 90, to print out 90 = 3 * 2 * 3 * 5. 

A = int (INPUT ( " Please The INPUT Number: \ n- " )) 
B = A
 # A factor set 
La = [] 


L = [] 


C = int (A * 0.5) +2
 Print ( " C: == > " , C) 

for I in Range (2 , C):
     IF I = 2! and I% 2 == 0:
         Continue 
    elif i !=3 and i%3==0:
        continue
    elif i !=5 and i%5==0:
        continue
    elif i !=7 and i%7==0:
        continue
    elif i !=11 and i%11==0:
        continue
    elif i !=13 and i%13==0:
        continue
    elif i !=17 and i%17==0:
        continue
    elif i !=19 and i%19==0:
        continue
    elif i !=23 and i%23==0:
        continue
    elif i !=29 and i%29==0:
        continue
    
    #print(i,"###")
    if a%i==0:
        la.append(i)
        a = a/i
        while(1):
            if a%i==0:
                la.append(i)
                a =a/i
            else:
                break
if len(la) == 0:
    la.append(1)
    la.append(b)

    
print(b," = ",end=" ")
for i in range(len(la)):
    print(la[i],end=" ")
    if i==len(la)-1:
        print("\n")
    else :
        print(" * ",end=" ")

 

The drawbacks of this code is for a slightly larger number, but its prime factors are relatively small numbers, a waste of time to deal with them:

So thought, the processing logic for a large number of some:

1, first create a number y within the table primes

2, the priority Show all prime factors of the prime number table contained large numbers;

3, for the quality factor is not within the range of the list, and then perform a search program; (this time-consuming comparison)

 

Optimized code (for larger numbers, can be slightly faster time to processed):

# ! / Usr / bin / Python 
# encoding = UTF. 8- 
# - * - Coding: UTF-. 8 - * - 

# a positive integer decomposition of the quality factor. For example: the input 90, to print out 90 = 3 * 2 * 3 * 5. 

A = int (INPUT ( " Please INPUT The Number: \ n- " )) 
B = A
 # A factor set 
La = [] 
X = 0
 # Y = 1000000 # maximum threshold for judging whether the calculated prime number list 
Y = 300000 # calculating the maximum threshold is determined prime number list 



IF . 1 :
     # first build a list of prime numbers: 
    L = []   
     for I in Range (2 , Y): 
        K. 1 =
         IF I == 2 : 
            l.append (I) 
            Continue 
        the else : 
            m = int (0.5 ** I)   # Square the root 
            for J in Range (2 , m):
                 IF I% J == 0: 
                    K = 0 ;
                     # Print (I,. "a iS Not Prime") 
                    BREAK 
        IF K ==. 1 : 
            l.append (I) 
    # Print ( "list:", L) 
    
    
    # investigation prime factors corresponding to the number in this list: 
    
    for n- in Range (len (L)):
         #Print (n-, "###") 
        IF A% L [n-] == 0: 
            la.append (L [n-]) 
            A = A / L [n-]
             the while (. 1 ):
                 IF A% L [n-] == 0: 
                    la.append (L [n-]) 
                    a = a / L [n-]
                 the else :
                     BREAK 
        c =. 1 # product for marking the number of c is equal to the la list a 
        
        
        
        for O in Range (len (la )): 
            C = C * La [O]
         IF C == B: 
            X =. 1    # X mark whether a number of the prime factors of all found that, if 1, showing all found; X is 0 by default; 

            

            
            
# Step processed; 
D = int (a) 
F = int (a) 

" "" 
Print (B, "=", End = "") 
for I in Range (len (La)): 
    Print (La [I], End = "") 
    IF I == len (La) -1: 
        Print ( "! * the new new \ n-Number") 
    the else: 
        Print ( "*", End = "") 
"" " 
# Print (" ==> = D ", D) 
# next cycle of data points; 
# only for calculating a large prime factors. 

IF X == 0: 
    E = int (0.5 * D) +2 
     # Print ( "==> = E",
    
        I in Range (y, e):   # I have to go to or smaller than e y determined, ah; 
            Print (I, " ### " )
             IF D% I == 0: 
                la.append (I) 
                X = 2 # la list marked increase; 
                D = D / I
                 the while (. 1 ):
                     IF D% I == 0: 
                        la.append (I) 
                        D = D / I
                     the else :
                         BREAK 
    the else :
         for Iin Range (2, e):   # I have to go to or smaller than e y determined, ah; 
            Print (I, " ### " )
             IF D% I == 0: 
                la.append (I) 
                X = 2 # flag la list increased; 
                d = d / I
                 the while (. 1 ):
                     IF d% I == 0: 
                        la.append (I) 
                        d = d / I
                     the else :
                         BREAK 
    IF X == 0:     # show themselves d is a prime number, It is a prime factor;
        # Print ( "2:", La, "F:", F) # 
        la.append (F)
     # Print ( ". 1:", La) # 
    
    
# For the case where the digital input is a prime number; 
IF len (La) == 0: 
    la.append ( 1 ) 
    la.append (B) 
# there is actually a prime number as 1, if the default is not a prime number, then the output may be: a prime number; no prime factors; 



Print (B, " = " , End = "  " )
 for I in Range (len (La)):
     Print (La [I], End = "  " )
     IF I == len (La) -1 :
         Print ( " \ n- ")
    else :
        print(" * ",end=" ")

        

 

process result:

 

 

 

 

 

The original title given answers:

 

 

 

 

 

 


-------- (I am dividing line) --------

reference:

1. RUNOOB.COM:https://www.runoob.com/python/python-exercise-example14.html

 

 

Remarks:

Initial modified: September 29, 2019 19:14:24

Environment: Windows 7 / Python 3.7.2

 

Guess you like

Origin www.cnblogs.com/kaixin2018/p/11609558.html