当前位置:网站首页>[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
[weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
2022-07-06 20:19:00 【Crossin's programming classroom】
Zero basis python Introductory tutorial :python666.cn
Hello everyone , Welcome to Crossin Programming classroom of !
This week we continue to work with “ Prime number ” screwing .
Please listen to :
Divide a positive integer into prime factors . If it's a prime number , The output “ This is a prime number ”
About Decomposing prime factor : Each sum can be written in the form of multiplication of several prime numbers , Each prime number is a factor of the sum , Express a sum in the form of multiplication of prime factors , It's called the decomposition prime factor . The decomposition prime factor is only for the sum .
for example :
Input 90, Output 2 3 3 5
Input 23, Output This is a prime number
Detailed answers and reference codes will be given in the next column , You can also refer to the code in the message of other students .
I look forward to your answers , I hope you can complete the whole series .
Simple code can be submitted directly in the message , Long code is recommended paste.ubuntu.com or
codeshare.io And other code sharing sites , Just copy the code and save it , You can get a share address , Very convenient .
For past questions, click the collection at the beginning of the article “ Every Monday ” Enter the view .
【 answer 】 Calculation 100 Sum of prime numbers within
The general idea of this question is :
Traverse 2~100:
Judge whether the current number is a prime number
If it's a prime number , Add the value to the result
Output results
The basic method of judging prime numbers is :
Traverse 2~ Number to be judged :
Determine whether it can be divided by the current number
If you can divide , Is not a prime number
If there is nothing divisible after traversal , Is a prime number
however , There are many places that can be optimized , Let the program get the result with less calculation times . For example, judge a number N Whether prime numbers do not need to be traversed 2~N, Just go to √N that will do .
You can look at the last issue @ A stone The answer to , It is a good optimization scheme :
def primeSum(N=100):
initial=[]
prime=0
node=int(N**0.5)+1
for i in range(2,node):
if 0 not in [i%pr for pr in initial]:
prime+=i
initial.append(i)
for i in range(node,N):
if 0 not in [i%pr for pr in initial]:
prime+=i
return prime
print(primeSum())
I mentioned it last time , Let's talk about an interesting solution . The idea of this solution is contrary to the conventional , It's not to judge who is prime , It's to get rid of those that are not prime numbers :
establish 2~100 A list of L
If the list L There's still value in , Then continue the cycle :
hold L[0] The value of is added to the result
For a list of L The elements in , Can be L[0] Don't divide everything , The rest becomes new L
Code :
def primeSum(N=100):
initial = list(range(2,N+1))
prime = 0
while len(initial) > 0:
i = initial[0]
prime += i
initial = [num for num in initial if num % i != 0]
return prime
print(primeSum())
The result is 1060
_ Previous articles are recommended _
【 Every Monday 】 Calculation 100 Sum of prime numbers within
If you need to know Paid premium courses And Teaching Q & a service
Please be there. Crossin Programming classroom of Internal reply : 666
边栏推荐
- 5. Wireless in vivo nano network: top ten "feasible?" problem
- 【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
- Gui Gui programming (XIII) - event handling
- How to handle the timeout of golang
- 【每周一坑】信息加密 +【解答】正整数分解质因数
- Poj1149 pigs [maximum flow]
- 小微企业难做账?智能代账小工具快用起来
- Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
- [Yann Lecun likes the red stone neural network made by minecraft]
- 电子游戏的核心原理
猜你喜欢
夏志刚介绍
腾讯T4架构师,android面试基础
OceanBase社区版之OBD方式部署方式单机安装
B-jiege's tree (pressed tree DP)
【计网】第三章 数据链路层(3)信道划分介质访问控制
22-07-05 七牛云存储图片、用户头像上传
An East SMS login resurrection installation and deployment tutorial
02 基础入门-数据包拓展
Boder radius has four values, and boder radius exceeds four values
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
随机推荐
String长度限制?
Catch ball game 1
电子游戏的核心原理
In unity space, an object moves around a fixed point on the sphere at a fixed speed
Digital triangle model acwing 1018 Minimum toll
Extraction rules and test objectives of performance test points
永磁同步电机转子位置估算专题 —— 基波模型与转子位置角
Gui Gui programming (XIII) - event handling
使用ssh连接被拒
Learn to punch in Web
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
Synchronization of data create trigger synchronization table for each site
A5000 vgpu display mode switching
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
技术分享 | 抓包分析 TCP 协议
POJ 3207 Ikki' s Story IV – Panda' s Trick (2-SAT)
Guangzhou's first data security summit will open in Baiyun District
Introduction of Xia Zhigang
[Yann Lecun likes the red stone neural network made by minecraft]