gcd(b, a%b)这tm是欧几里得算法。即两个数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。
吓得我虎躯一震
吓得我虎躯一震
2018-03-11
def __str__(self):
for x in [2, 3, 5, 7]:
while self.p % x == 0 and self.q % x == 0:
self.p = self.p / x
self.q = self.q / x
return '%s/%s' % (self.p, self.q)
for x in [2, 3, 5, 7]:
while self.p % x == 0 and self.q % x == 0:
self.p = self.p / x
self.q = self.q / x
return '%s/%s' % (self.p, self.q)
2018-03-11
def __init__(self, num):
self.sum = []
for i in range(num):
if i <= 1:
self.sum.append(i)
else:
self.sum.append(self.sum[i-1] + self.sum[i-2])
def __str__(self):
return str(self.sum)
self.sum = []
for i in range(num):
if i <= 1:
self.sum.append(i)
else:
self.sum.append(self.sum[i-1] + self.sum[i-2])
def __str__(self):
return str(self.sum)
2018-03-10
import math
def is_sqr(x):
return int(math.sqrt(x))*int(math.sqrt(x))==x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return int(math.sqrt(x))*int(math.sqrt(x))==x
print filter(is_sqr, range(1, 101))
2018-03-10
class Fib(object):
def __call__(self,num):
list = [0,1]
for x in range(0,num-2):
list.append(list[-2]+list[-1])
return list
f = Fib()
print f(10)
def __call__(self,num):
list = [0,1]
for x in range(0,num-2):
list.append(list[-2]+list[-1])
return list
f = Fib()
print f(10)
2018-03-10
第一个score(self)是get方法,用@property装饰,第二个score(self, score)是set方法,用@score.setter装饰,@score.setter是前一个@property装饰后的副产品。
2018-03-10