1. There are four numbers: 1, 2, 3, and 4. How many different three-digit numbers can be formed without repeated numbers? How many are each?
s=0for i inrange(1,5):#从1到四遍历for j inrange(1,5):for c inrange(1,5):if(i != j)and(i != c)and(c != j):print(i*100+j*10+ c *1)#i做为百位,j作为十位,c作为个位
s +=1print('共有%s种'% s)
2. Determine how many prime numbers there are between 101-200 and output all prime numbers.
count =0
leap =1from math import sqrt
for m inrange(101,201):#循环
k =int(sqrt(m +1))for i inrange(2, k +1):if m % i ==0:
leap =0breakif leap ==1:print('%5d'% m, end='')
count +=1if count %10==0:print(end='')
leap =1print('\n')print('101-200之间一共有%d个素数'% count)
3. Decompose a positive integer into prime factors. For example: input 90, print out 90=2 3 3*5
from sys import stdout
from pip._vendor.distlib.compat importraw_input
n =int(raw_input("请输入一个正整数:"))print("n = %d"% n)for i inrange(2, n +1):while n != i:if n % i ==0:
stdout.write(str(i))
stdout.write("*")
n = n / i
else:breakprint("%d"% n)
4. Enter a line of characters and count the number of English letters, spaces, numbers and other characters in it.
s=input('请输入字符:')
letter=0#英文字母
space=0#空格
digit=0#数字
other=0#其他for i in s:if i.isalpha():#判断是否是字母
letter+=1elif i.isspace():#判断是否是空格
space+=1elif i.isdigit():#判断是否是数字
digit+=1else:
other+=1print('字母个数为{}\n空格字数为{}\n数字字数为{}\n其他字符为{}\n'.format(letter,space,digit,other))
5. Sum of factorials from 1-25
a=1sum=0for i inrange(1,26):
a=a*i
sum=sum+a
print("1-20的阶乘:\n",sum)