程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python find prime and composite numbers within a specified range - detailed explanation

編輯:Python

Catalog

1) brief introduction

2) Program implementation code -1- for loop

3) Program implementation code [2] List expression

List expression :

----------------------- Text ---------------


1) brief introduction

Prime number , Immediate Division 1 A number that has no other factor than itself , Also known as prime number .

Sum , Immediate Division 1 A number that has other factors besides itself .

--- Be careful :1 Neither prime nor composite !

2) Program implementation code -1- for loop

Package a function :

def primenum(start,end):

start As the starting point ,end For the end

Function main body

def primenum(start,end):
prime=[]
for i in range(start,end+1):
for j in range(2,i):# It doesn't contain 1, It itself
if i%j==0:
break
else:# Be careful else Add to for Back
prime.append(i)
return prime

The output effect is OK , But there are 0 and 1...

Add filtering effect

def primenum(start,end):
prime=[]
if start<2:start=2
for i in range(start,end+1):
for j in range(2,i):# It doesn't contain 1, It itself
if i%j==0:
break
else:# Be careful else Add to for Back
prime.append(i)
return prime
print(primenum(0,100))

result :

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

It's easy to add up numbers , Divide all by 0,1 It's good to count other numbers

def hs(start,end):
num=[]
if start<2:start=2
for i in range(start,end+1):
for j in range(2,i):
if i%j==0:
num.append(i)
break
return num

------------------------------------------ Beginner split line ---------------------------------------------------------------

Here comes the high-end game !||ヽ(* ̄▽ ̄*)ノミ|Ю The debut

3) Program implementation code [2] List expression

List expression :

The more important , Cross preposition .

a=[i for i in range(1,10)]
print(a)
>>>[1,2,3,4,5,6,7,8,9]

[ Use for Loop every time i The value of is nested into ]

a=[i for i in range(1,10) if i%2==0]
print(a)
>>>[2,4,6,8]

l - level - 了 !

if On the for Back , Every time i It will be more than if Review before copying , Otherwise it doesn't count

a=[i**2 for i in range(1,5)]
print(a)
>>>[1,4,9,16]

take i Did more

a=[j for j in range(i) for i in range(10)]
print(a)

Report errors , The first loop precedes

a=[j for i in range(10) for j in range(i)]
print(a)
>>>[0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8]

Nested loop

----------------------- Text ---------------

The method of finding composite numbers :

list

a=[]

Add the expression number one ( We use 3~100)

a=[i for i in range(3,100+1)]

Nesting , Factor

a=[j for i in range(3,100+1) for j in range(2,i)]

Judge whether to divide

a=[j for i in range(3,100+1) for j in range(2,i) if i%j==0]

Be careful :i Is the real value !

a=[i for i in range(3,100+1) for j in range(2,i) if i%j==0]

Next, filter out duplicate values :

a=list(set([i for i in range(3,100+1) for j in range(2,i) if i%j==0]))

Okay OK

Next comes prime numbers —— But judgment is much simpler , I go straight to the code

value = [i for i in range(1 if 1 >= 2 else 2, 100 + 1) if i not in a]

Some people want to find prime numbers in a row instead of composite numbers in a row , Find prime numbers in a row :

one=[i for i in range(1 if 1 >= 2 else 2, 100 + 1) if i not in [i for i in range(1,100+1) for j in range(2, i) if i % j == 0]]

---------------- End -----------------


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved