python求100以内素数之和 from math import sqrt # 使用isPrime函数 def isPrime(n): if n <= 1: return False for i in range(2, int(sqrt(n)) + 1): if n % i == 0: return False return True count = 0 for i in range(101): if isPrime(i): count += i print(count) # 单行程序扫描素数
一.求算术平方根 a=0 x=int(raw_input('Enter a number:')) if x >= 0: while a*a < x: a = a + 1 if a*a != x: print x,'is not a perfect square' else: print a else: print x,'is a negative number' 二.求约数 方法一: divisor = [ ] x=int(raw_input('Enter a number:')) i=1 w
也称为求一个集合的所有的子集 采用二进制方法: def PowerSetsBinary(items): #generate all combination of N items N = len(items) #enumerate the 2**N possible combinations for i in range(2**N): combo = [] for j in range(N): #test jth bit of integer i if(i >> j ) % 2 == 1: co