当前位置:网站首页>[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

边栏推荐
- 案例 ①|主机安全建设:3个层级,11大能力的最佳实践
- 【GET-4】
- Tencent cloud database public cloud market ranks top 2!
- Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model
- HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
- Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
- golang的超时处理使用技巧
- Force deduction brush question - 98 Validate binary search tree
- Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
- B-jiege's tree (pressed tree DP)
猜你喜欢

【GET-4】

【云原生与5G】微服务加持5G核心网

01 基础入门-概念名词

爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解

Ideas and methods of system and application monitoring

【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN

Node. Js: express + MySQL realizes registration, login and identity authentication
Tencent Android development interview, basic knowledge of Android Development
Tencent T2 Daniel explained in person and doubled his job hopping salary
![[Yann Lecun likes the red stone neural network made by minecraft]](/img/95/c3af40c7ecbd371dd674aea19b272a.png)
[Yann Lecun likes the red stone neural network made by minecraft]
随机推荐
Error analysis ~csdn rebound shell error
02 基础入门-数据包拓展
【云小课】EI第47课 MRS离线数据分析-通过Flink作业处理OBS数据
Tencent T2 Daniel explained in person and doubled his job hopping salary
Cesium 点击绘制圆形(动态绘制圆形)
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
RT-Thread 组件 FinSH 使用时遇到的问题
HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
Database specific interpretation of paradigm
AddressSanitizer 技术初体验
腾讯T4架构师,android面试基础
报错分析~csdn反弹shell报错
How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
22-07-05 七牛云存储图片、用户头像上传
Leetcode brush first_ Maximum Subarray
枚举根据参数获取值
Extraction rules and test objectives of performance test points
Rhcsa Road
为什么新手在编程社区提问经常得不到回答,甚至还会被嘲讽?
逻辑是个好东西