(21).找數字的因數
def print_f(x):
print(x,"的因數有:")
for i in range(1,x+1):
if x%i==0:
print(i,end=' ')
num=240
print_f(num)
240 的因數有:
1 2 3 4 5 6 8 10 12 15 16 20 24 30 40 48 60 80 120 240
(22).找出間隔內質數
low=50
up=100
print(low,"至",up," 間隔內所有質數:")
for n in range(low,up+1):
if n>1:
for i in range(2,n):
if(n%i)==0:
break
else:
print(n,end=' ')
50 至 100 間隔內所有質數:
53 59 61 67 71 73 79 83 89 97
~~~~~~~傳統寫法
low=50
up=100
print(low,"至",up," 間隔內所有質數:")
for n in range(low,up+1):
f=0;i=2
while(f==0 and i<((n-1)/2)):
i=i+1
if(n%i)==0:
f=1
if(f==0):
print(n,"is prime")
50 至 100 間隔內所有質數:
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
(23).最大公因數
~~~~~~~輾轉相除
x=12;y=18
if (x>y):
x,y=y,x
m=x;x=y
while(m>0):
y=x;x=m;m=y%x
print('GCD=',x)
GCD= 6
(24).函數算GCD - LCM
def gcd(m,n):
return m if (n==0) else gcd(n,m%n)
def lcm(m,n):
return m*n//gcd(m,n)
m=56
n=24
print(m,"&",n," 的 GCD= ",gcd(m,n))
print(m,"&",n," 的 LCM= ",lcm(m,n))
56 & 24 的 GCD= 8
56 & 24 的 LCM= 168
(25).印出50階層
fact=1
for n in range(1,51):
fact=fact*n
print('%2d!=%d' %(n,fact))
1!=1
2!=2
3!=6
4!=24
5!=120
6!=720
7!=5040
8!=40320
9!=362880
......略
(26).費氏數列
n1=0;n2=1
print(n1,end=' ')
for i in range(1,17):
print(n2,end=' ')
n=n2
n2=n1+n2
n1=n
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
(27).找出某區間中阿姆斯壯數
#1~1000之間的阿姆斯壯數
sum=0
for num in range(1,1000):
temp=num
while temp>0:
digit=temp%10
sum=sum+digit**3
temp=temp//10
if num==sum:
print(num,end=' ')
sum=0
#1~1000之間的阿姆斯壯數
sum=0
for num in range(1,1000):
temp=num
while temp>0:
digit=temp%10
sum=sum+digit**3
temp=temp//10
if num==sum:
print(num,end=' ')
sum=0
1 153 370 371 407
#暴力法
import time;
t0=time.time()
for a in range(0,10):
for b in range(0,10):
for c in range(0,10):
res=a**3+b**3+c**3
dignum=int(str(a)+str(b)+str(c))
if(res==dignum and res>10**2):
print(res)
t1=time.time()
print("費時:",t1-t0);print("\n\n")
#避開相同組合的數字
t0=time.time()
n=0
for a in range(0,10):
for b in range(a,10):
for c in range(b,10):
print(a,b,c);n=n+1
res=a**3+b**3+c**3
dignum=str(a)+str(b)+str(c)
if ''.join(sorted(list(str(res))))==dignum:
print(''.join(sorted(list(str(res)))),dignum,res)
t1=time.time()
print("費時:",t1-t0);print("\n\n")
print(n)
153
370
371
407
費時: 0.02200007438659668
0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 0 5
0 0 6
0 0 7
0 0 8
0 0 9
0 1 1
0 1 2
0 1 3
0 1 4
0 1 5
0 1 6
0 1 7
0 1 8
0 1 9
0 2 2
0 2 3
0 2 4
0 2 5
0 2 6
0 2 7
0 2 8
0 2 9
0 3 3
0 3 4
0 3 5
0 3 6
0 3 7
037 037 370
0 3 8
0 3 9
0 4 4
0 4 5
0 4 6
0 4 7
047 047 407
0 4 8
0 4 9
0 5 5
0 5 6
0 5 7
0 5 8
0 5 9
0 6 6
0 6 7
0 6 8
0 6 9
0 7 7
0 7 8
0 7 9
0 8 8
0 8 9
0 9 9
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 2 2
1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 2 8
1 2 9
1 3 3
1 3 4
1 3 5
135 135 153
1 3 6
1 3 7
137 137 371
1 3 8
1 3 9
1 4 4
1 4 5
1 4 6
1 4 7
1 4 8
1 4 9
1 5 5
1 5 6
1 5 7
1 5 8
1 5 9
1 6 6
1 6 7
1 6 8
1 6 9
1 7 7
1 7 8
1 7 9
1 8 8
1 8 9
1 9 9
2 2 2
2 2 3
2 2 4
2 2 5
2 2 6
2 2 7
2 2 8
2 2 9
2 3 3
2 3 4
2 3 5
2 3 6
2 3 7
2 3 8
2 3 9
2 4 4
2 4 5
2 4 6
2 4 7
2 4 8
2 4 9
2 5 5
2 5 6
2 5 7
2 5 8
2 5 9
2 6 6
2 6 7
2 6 8
2 6 9
2 7 7
2 7 8
2 7 9
2 8 8
2 8 9
2 9 9
3 3 3
3 3 4
3 3 5
3 3 6
3 3 7
3 3 8
3 3 9
3 4 4
3 4 5
3 4 6
3 4 7
3 4 8
3 4 9
3 5 5
3 5 6
3 5 7
3 5 8
3 5 9
3 6 6
3 6 7
3 6 8
3 6 9
3 7 7
3 7 8
3 7 9
3 8 8
3 8 9
3 9 9
4 4 4
4 4 5
4 4 6
4 4 7
4 4 8
4 4 9
4 5 5
4 5 6
4 5 7
4 5 8
4 5 9
4 6 6
4 6 7
4 6 8
4 6 9
4 7 7
4 7 8
4 7 9
4 8 8
4 8 9
4 9 9
5 5 5
5 5 6
5 5 7
5 5 8
5 5 9
5 6 6
5 6 7
5 6 8
5 6 9
5 7 7
5 7 8
5 7 9
5 8 8
5 8 9
5 9 9
6 6 6
6 6 7
6 6 8
6 6 9
6 7 7
6 7 8
6 7 9
6 8 8
6 8 9
6 9 9
7 7 7
7 7 8
7 7 9
7 8 8
7 8 9
7 9 9
8 8 8
8 8 9
8 9 9
9 9 9
費時: 1.4991998672485352
220
list() 字串拆分成串列
>>> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
split() 字串拆分成字元
>>> name="my name is Wen"
>>> name.split()
['my', 'name', 'is', 'Wen']
join() 字串加入串列
>>> namelist=['My','name','is','Wen']
>>> ' '.join(namelist)
'My name is Wen'
map() 字串轉成串列
>>> listA=['1','2','3']
>>> list(map(int,listA))
[1, 2, 3]
>>> ipline='1 2 3 4 5'
>>> list01=list(map(int,ipline.split()))
>>> print(list01)
[1, 2, 3, 4, 5]
(28).遞迴算總和
n=100
def sum(n):
if(n<1):
return 0
return sum(n-1)+n
print(sum(n))
def sum(n):
if(n<1):
return 0
return sum(n-1)+n
print(sum(n))
5050
(29).函數印費式數列
def fib(n):
a,b=0,1
while a<n:
print(a,end=' ')
a,b=b,a+b
print()
fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
(30).遞迴算階層 factorial
def recu_f(n):
if n==1:
return n
else:
return n*recu_f(n-1)
num=6
print(num,"階層=",recu_f(num))
6 階層= 720
(31).遞迴算二進位
def dtb(n):
if n>1:
dtb(n//2)
print(n%2,end=' ')
dec=18
dtb(dec)
def fib(n):
a,b=0,1
while a<n:
print(a,end=' ')
a,b=b,a+b
print()
fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
(30).遞迴算階層 factorial
def recu_f(n):
if n==1:
return n
else:
return n*recu_f(n-1)
num=6
print(num,"階層=",recu_f(num))
6 階層= 720
(31).遞迴算二進位
def dtb(n):
if n>1:
dtb(n//2)
print(n%2,end=' ')
dec=18
dtb(dec)
1 0 0 1 0
沒有留言:
張貼留言