当前位置:网站首页>2021-03-04 COMP9021第六节课笔记
2021-03-04 COMP9021第六节课笔记
2022-06-24 06:56:00 【紫云无堤】
复习
获取字符的ASCII码

排序


lambda表达式



经典题 - 素因数 - Prime Factor
给一个正整数,打印出该数的素因数。
不知道什么是素因数,看这里素因数,质因数。

from math import sqrt
def primeFactors(m):
'''return the prime factors of the input number m '''
prime_factors = [] # 用于存储质因数
factor = 2 # 最小的质因数
upper_bound = int(sqrt(n)) # 一个数的因数中的上限
while factor <= upper_bound: # 从2开始遍历到上限
while m % factor == 0: # 判断当前值是否为因数
m //= factor
prime_factors.append(factor)
factor += 1
if m != 1:# 如果最后这个数不为1,则这个数也是素因数 # 感谢来自哇嘞同学的提醒,此处已修改
prime_factors.append(m) # 把自己放进结果里
prime_factors = [str(x) for x in prime_factors]
return f"{n} = {'*'.join(prime_factors)}"
n = 231084
print(primeFactors(n))
统计素因数个数

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:# 如果最后这个数不为1,则这个数也是素因数 # 感谢来自哇嘞同学的提醒,此处已修改
prime_factors[m] = 1
return prime_factors
n = 5432
primeterFactorInfo(n)
统计素因数个数之collections.counter
精度问题 - precision

来自罗同学的提醒:
这个round并不是上面说的五舍六入,有时候舍有时候会进,而是python自身的表示小数的精度问题。
测试和官方文档如下


- 测试精度

下面这段代码是把每一行的结果(除了等式)都输出,和matlab差不多。
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
计算n以内的素数个数

计算程序运行时长
- timeit模块
官方文档:timeit - 计算小段代码模块运行时长
- globals函数
globals函数会返回当前环境的所有全局变量。在timeit中使用这个函数可以避免手动输入参数的麻烦。
官方文档:在页面的最下面最下面,最后一个
在程序中使用:
边栏推荐
- [teacher zhaoyuqiang] use the Oracle tracking file
- Introduction to software engineering - Chapter 2 - feasibility study
- Standing at the center of the storm: how to change the engine of Tencent
- 宝塔面板安装php7.2安装phalcon3.3.2
- Case examples of corpus data processing (cases related to sentence retrieval)
- Phonics
- Swift foundation features unique to swift
- 从 jsonpath 和 xpath 到 SPL
- Interview tutorial - multi thread knowledge sorting
- Sql语句内运算问题
猜你喜欢
![[nilm] non intrusive load decomposition module nilmtk installation tutorial](/img/d0/bc5ea1cbca9ee96a2fe168484ffec4.png)
[nilm] non intrusive load decomposition module nilmtk installation tutorial

Installation and use of selenium IDE

Shader common functions

Vulnhub target: boredhackerblog_ CLOUD AV

Vulnhub靶机:BOREDHACKERBLOG: SOCIAL NETWORK
![3D数学基础[十七] 平方反比定理](/img/59/bef931d96883288766fc94e38e0ace.png)
3D数学基础[十七] 平方反比定理

宝塔面板安装php7.2安装phalcon3.3.2

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

Swift 基础 闭包/Block的使用(源码)

OC extension detects whether an app is installed on the mobile phone (source code)
随机推荐
软件工程导论——第二章——可行性研究
Swift 基础 Swift才有的特性
Analysis of abnormal problems in domain name resolution in kubernetes
工控机防破解
你还只知道测试金字塔?
How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
Online education fades
单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案
Unity culling related technologies
OC Extension 检测手机是否安装某个App(源码)
Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
Echart's experience (I): about y axis yaxis attribute
不止于观测|阿里云可观测套件正式发布
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
C语言_字符串与指针的爱恨情仇
Question 1: the container that holds the most water
All you know is the test pyramid?
[ACNOI2022]做过也不会
Chapter 4 line operation of canvas
Easyplayerpro win configuration full screen mode can not be full screen why
