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

边栏推荐
- Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
- 【云原生与5G】微服务加持5G核心网
- 案例 ①|主机安全建设:3个层级,11大能力的最佳实践
- Digital triangle model acwing 1018 Minimum toll
- Quel genre de programmation les enfants apprennent - ils?
- [weekly pit] positive integer factorization prime factor + [solution] calculate the sum of prime numbers within 100
- 5. 無線體內納米網:十大“可行嗎?”問題
- Guangzhou's first data security summit will open in Baiyun District
- 5. Wireless in vivo nano network: top ten "feasible?" problem
- 使用ssh连接被拒
猜你喜欢

Boder radius has four values, and boder radius exceeds four values

02 basic introduction - data package expansion

I've seen many tutorials, but I still can't write a program well. How can I break it?

Oceanbase Community Edition OBD mode deployment mode stand-alone installation

rt-thread i2c 使用教程

SQL injection 2
![[network planning] Chapter 3 data link layer (3) channel division medium access control](/img/df/dd84508dfa2449c31d72c808c50dc0.png)
[network planning] Chapter 3 data link layer (3) channel division medium access control

01 basic introduction - concept nouns
![[weekly pit] information encryption + [answer] positive integer factorization prime factor](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[weekly pit] information encryption + [answer] positive integer factorization prime factor

【GET-4】
随机推荐
看过很多教程,却依然写不好一个程序,怎么破?
Node.js: express + MySQL实现注册登录,身份认证
Leetcode question 448 Find all missing numbers in the array
腾讯安卓开发面试,android开发的基础知识
Tencent T2 Daniel explained in person and doubled his job hopping salary
报错分析~csdn反弹shell报错
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
22-07-05 七牛云存储图片、用户头像上传
Extraction rules and test objectives of performance test points
Node. Js: express + MySQL realizes registration, login and identity authentication
5. 无线体内纳米网:十大“可行吗?”问题
Boder radius has four values, and boder radius exceeds four values
String length limit?
Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
Wechat applet common collection
【每周一坑】计算100以内质数之和 +【解答】输出三角形
JVM_ Common [interview questions]
Case ① | host security construction: best practice of 3 levels and 11 capabilities
Groovy基础语法整理
Quel genre de programmation les enfants apprennent - ils?