当前位置:网站首页>2021-03-04 comp9021 class 6 notes

2021-03-04 comp9021 class 6 notes

2022-06-24 08:22:00 Purple cloud without dike

review

  1. Get the ASCII code
     Insert picture description here

  2. Sort
     Insert picture description here
     Insert picture description here

  3. lambda expression
     Insert picture description here
     Insert picture description here
     Insert picture description here

Classic questions - Prime factor - Prime Factor

Give a positive integer , Print out the prime factor of the number .
I don't know what a prime factor is , Look here Prime factor , Prime factor .

 Insert picture description here

from math import sqrt

def primeFactors(m):
    '''return the prime factors of the input number m '''
    prime_factors = [] #  Used to store prime factors 
    factor = 2 #  The smallest prime factor 
    upper_bound = int(sqrt(n)) #  The upper limit of the factor of a number 

    while factor <= upper_bound: #  from 2 Start traversing to the upper limit 
        while m % factor == 0:  #  Determine whether the current value is a factor 
            m //= factor
            prime_factors.append(factor)
        factor += 1
    if m != 1:#  If the last number is not 1, Then this number is also a prime factor  #  Thanks for the reminder from wow le , Modified here 
        prime_factors.append(m) #  Put yourself in the results 
    
    prime_factors = [str(x) for x in prime_factors]
    return f"{n} = {'*'.join(prime_factors)}"

n = 231084
print(primeFactors(n))

Number of statistical prime factors

 Insert picture description here

from math import sqrt

def primeterFactorInfo(m):
    prime_factors = {
    }
    factor = 2
    upper_bound = int(sqrt(n))

    while factor <= upper_bound:
        while m % factor == 0:
            m //= factor
            if factor not in prime_factors:
                prime_factors[factor] = 1
            else:
                prime_factors[factor] += 1
        factor += 1
    if m != 1:#  If the last number is not 1, Then this number is also a prime factor  #  Thanks for the reminder from wow le , Modified here 
        prime_factors[m] = 1
    return prime_factors
        
n = 5432
primeterFactorInfo(n)

Count the number of prime factors collections.counter
 Insert picture description here

Precision problem - precision

 Insert picture description here
A reminder from Mr. Luo :
This round It's not round off as mentioned above , Sometimes I give up and sometimes I go in , It is python The precision of the decimal representation of itself .
The test and official documentation are as follows

 Insert picture description here
 Insert picture description here

  • Test accuracy
     Insert picture description here
    The following code is the result of each line ( Except for the equation ) All output , and matlab almost .
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

Calculation n The prime number within

 Insert picture description here

Calculate program run time

原网站

版权声明
本文为[Purple cloud without dike]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240442429096.html