学习python之路---python小算法总结(二)

题目6输出101-201之间的素数

import math

result = []

for i in range(101,201):

   mark = True

   for j in range(2,int(math.sqrt(i))+1):

       if i % j == 0:

           mark = False

           break

   if mark == True:

       result.append(i)

print result

 

输出结果:[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163,167, 173, 179, 181, 191, 193, 197, 199]

 

题目7辗转相除法---输入两个正整数mn的最大公约数

m = int(raw_input('Please input the firstpositive integer: '))

n = int(raw_input('Please input the secondpositive integer: '))

if m < n:

   m,n = n,m

while n!=0: # 直到“余数”和“商”的商为0

    r= m % n # 得到m和n相除的余数

    m= n

    n= r

print m

 

 


题目8输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

s=raw_input('please enter a string:')

letters=0

space=0

digit=0

others=0

for c in s:

    ifc.isalpha():

       letters+=1

    elifc.isdigit():

        digit+=1

    elifc.isspace():

       space+=1

    else:

       others+=1

print "letters:%d,Digit:%d,Space:%d,Others:%d"%(letters,digit,space,others)

 

题目9递归运算

>>> def fact(j):

       sum=0

       ifj==0:

              sum=1

       else:

              sum=j*fact(j-1)

       returnsum

>>> fact(5)

120



题目10回文数判断

x=int(raw_input("input a number:"))

x=str(x)

for i in range(len(x)/2):

    if x[i]!=x[-i-1]:

        print 'this numberis not a huiwenshu'

        break

print 'this number is a huiwenshu'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值