当前位置:网站首页>[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
边栏推荐
- mod_ WSGI + pymssql path SQL server seat
- Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
- Unity writes a timer tool to start timing from the whole point. The format is: 00:00:00
- 看过很多教程,却依然写不好一个程序,怎么破?
- Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
- What programming do children learn?
- How to handle the timeout of golang
- Notes on beagleboneblack
- Enumeration gets values based on parameters
- Digital triangle model acwing 1018 Minimum toll
猜你喜欢
rt-thread i2c 使用教程
Tencent T4 architect, Android interview Foundation
02 基础入门-数据包拓展
夏志刚介绍
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
Event center parameter transfer, peer component value transfer method, brother component value transfer
【每周一坑】正整数分解质因数 +【解答】计算100以内质数之和
【GET-4】
小孩子學什麼編程?
02 basic introduction - data package expansion
随机推荐
Guangzhou's first data security summit will open in Baiyun District
SSH connection denied
腾讯T4架构师,android面试基础
JVM_ Common [interview questions]
系统与应用监控的思路和方法
Rhcsa Road
Color is converted to tristimulus value (r/g/b) (dry stock)
Case ① | host security construction: best practice of 3 levels and 11 capabilities
Tencent T2 Daniel explained in person and doubled his job hopping salary
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
New generation garbage collector ZGC
Redisson bug analysis
小孩子學什麼編程?
Problems encountered in using RT thread component fish
What programming do children learn?
Period compression filter
RT thread I2C tutorial
22-07-05 七牛云存储图片、用户头像上传
Cesium Click to draw a circle (dynamically draw a circle)
String length limit?