当前位置:网站首页>[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
[weekly pit] calculate the sum of primes within 100 + [answer] output triangle
2022-07-06 20:23:00 【Crossin's programming classroom】
Zero basis python Introductory tutorial :python666.cn
Hello everyone , Welcome to Crossin Programming classroom of !
Today's topic is very short :
Calculation 1 To 100 The sum of all prime numbers within
The so-called prime number , Except 1 And the number itself , A number that is not divided by other natural numbers .
To judge whether a number is a prime number , This is not difficult
find 1~100 Within the scope of , All prime numbers , This is very simple
Add up the found prime numbers , This is even less difficult
Top up 3 Step together OK La !
But if you are interested , You can further consider the algorithm complexity of your method , See whose method is simpler .
in addition , For this question , I have an interesting solution , Not exactly according to the above idea , To be announced next week .( I don't know if anyone's answer will be the same as I think )
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 】 Output triangle
The key to this problem is to calculate well The number of spaces in each line And The current number of lines Correspondence of .
Output multiple spaces , You can go through the loop , It can also be directly passed through “ String multiplication ” Realization , namely :
' Original string ' * n
You can get n individual ' Original string ' New string composed of .
Refer to the answer :( come from @KE10)
def ntriangle(n):
print('*'*n)
n -= 1
while n > 1:
print('*'+' '*(n-2)+'*')
n -= 1
print('*')
ntriangle(5)
_ Previous articles are recommended _
If you need to know Paid premium courses And Teaching Q & a service
Please be there. Crossin Programming classroom of Internal reply : 666
边栏推荐
- Tencent Android development interview, basic knowledge of Android Development
- Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
- Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
- [weekly pit] information encryption + [answer] positive integer factorization prime factor
- Detailed introduction of distributed pressure measurement system VIII: basic introduction of akka actor model
- 数字三角形模型 AcWing 1018. 最低通行费
- [network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
- 青龙面板白屏一键修复
- Cesium 点击绘制圆形(动态绘制圆形)
- 案例 ①|主机安全建设:3个层级,11大能力的最佳实践
猜你喜欢
"Penalty kick" games
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
Rhcsa Road
【计网】第三章 数据链路层(3)信道划分介质访问控制
电子游戏的核心原理
Event center parameter transfer, peer component value transfer method, brother component value transfer
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
BUUCTF---Reverse---easyre
[weekly pit] output triangle
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
随机推荐
Guangzhou's first data security summit will open in Baiyun District
Poj3617 best cow line
Cesium 两点之间的直线距离
数字三角形模型 AcWing 1018. 最低通行费
BeagleBoneBlack 上手记
Catch ball game 1
recyclerview gridlayout 平分中间空白区域
PHP and excel phpexcel
B-jiege's tree (pressed tree DP)
[weekly pit] information encryption + [answer] positive integer factorization prime factor
看过很多教程,却依然写不好一个程序,怎么破?
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
Number of schemes from the upper left corner to the lower right corner of the chessboard (2)
腾讯云数据库公有云市场稳居TOP 2!
8086 instruction code summary (table)
02 基础入门-数据包拓展
【每周一坑】输出三角形
Leetcode question 283 Move zero
js获取浏览器系统语言
01 basic introduction - concept nouns