191010 python3 prime factor decomposition

# Title: a positive integer decomposition of the quality factor. For example: the input 90, to print out 90 = 3 * 2 * 3 * 5. 
# Program Analysis: n-decomposition quality factor, should find a smallest prime number k, then the following procedure is completed:
# (1) If the prime exactly equal to n, then the decomposition of the quality factor of the process has ended, printed out It can be.
# (2) if n <> k, k but n be divisible, it should print out the value of k and k with n divided by the quotient, and you as a new positive integer n, repeat the first step.
# (3) if n is not divisible by k, k + 1 is used as the value of k, the first step is repeatedly performed.
Method a: Add list append method using factor;
. 1 NUM = int (INPUT ( " Enter a composite number: " ))
 2 n-= NUM
 . 3 List1 = []
 . 4  
. 5  for I in Range (int (n-/ 2) + 1'd ):
 . 6      # Range object can only be positive integer, n / 2 possible decimal 
7      # of cycles: n biggest factor itself (prime number), the minimum factor of 2, 
8      # so the maximum number of cycles n / 2 can 
. 9      for J in Range (2 , n-) :
 10          IF NUM% J == 0:
 . 11              list1.append (J)
 12 is              NUM = NUM // J
 13 is              BREAK
14  '' ' 
15  for j in Range (2, n-):    
 16      # if j is divisible, it is a factor of j, if j is divisible times, but only recorded once;
 . 17      IF NUM = j% 0 =:
 18 is          list1.append (J)
 . 19          NUM = NUM J //
 20 is          Continue
 21 is  ' '' 
22 is  IF len (List1) == 0:
 23 is      Print ( " . this number is a prime number, please re-enter another number ." )
 24      Exit ()
 25  Print (List1)
 26 is  Print ( ' % D = ' % n-, End = '' )
 27  for Kin range (len (list1)):
 28      if for only a == (list1) -1 :
 29          print ( " % s " % list1 [k])
 30      else :
 31          print ( " % s * " % list1 [a] , end = '' )
View Code

Method 2: Using the method of adding factor format;

. 1  DEF reduceNum (n):
 2      Print ( ' {} = ' .format (n), End = '' )
 . 3      IF  Not the isinstance (n, int) or n <= 0:
 . 4          # the isinstance determines whether the data n int ; data type 
. 5          Print ( ' ! Please enter a correct number ' )
 . 6          Exit (0)
 . 7      elif n- in [. 1 ]:
 . 8          Print ( ' {} ' .format (n-))
 . 9      the while n- Not  in [1]:
10         for i in range(2,int(n+1)):
11             if n%i==0:
12                 n/=i
13                 if n==1:
14                     print(i)
15                 else:
16                     print('{}*'.format(i),end='')
17                 break
18 
19 #reduceNum(-90)
20 reduceNum(1)
View Code

 See rookie Tutorial: https: //www.runoob.com/python/python-exercise-example14.html

Guess you like

Origin www.cnblogs.com/jakye/p/11647574.html