正剧开始:
星历2016年02月13日 16:04:49, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起学习分数。
不过在开始修炼之前,小伟偷眼瞄了一眼,竟然发现了这样的一个问题:
碰上这种问题,小伟是不会错过的,这不,很快就有了解答:
<span style="font-size:18px;">>>> 当棱长为2时,8块3面涂色,0块2面涂色,0块1面涂色,0块没有涂色。 当棱长为3时,8块3面涂色,12块2面涂色,6块1面涂色,1块没有涂色。 当棱长为4时,8块3面涂色,24块2面涂色,24块1面涂色,8块没有涂色。 当棱长为5时,8块3面涂色,36块2面涂色,54块1面涂色,27块没有涂色。 当棱长为6时,8块3面涂色,48块2面涂色,96块1面涂色,64块没有涂色。 当棱长为7时,8块3面涂色,60块2面涂色,150块1面涂色,125块没有涂色。 当棱长为8时,8块3面涂色,72块2面涂色,216块1面涂色,216块没有涂色。 当棱长为9时,8块3面涂色,84块2面涂色,294块1面涂色,343块没有涂色。 def tmp(): paintNone = 0; paintOne = 0; paintTwo = 0; paintThree = 0; for edgeLength in range(2, 10): total = pow(edgeLength, 3); #涂三面的永远只是8个角 paintThree = 8; #涂两面的是 paintTwo = round(6*(4*(edgeLength-2))/2); #涂一面的是 paintOne = 6*(pow(edgeLength-2, 2)); #不涂 paintNone = total-paintThree-paintTwo-paintOne; print('当棱长为{0}时,{1}块3面涂色,{2}块2面涂色,{3}块1面涂色,{4}块没有涂色。'.\ format(edgeLength, paintThree, paintTwo, paintOne, paintNone)); return;</span>
怎样分解质因数呢?
>>> [0] 0 = 0 [1] 1 = 1 [2] 2 = 2 [3] 3 = 3 [2, 2] 4 = 2 * 2 [5] 5 = 5 [2, 3] 6 = 2 * 3 [7] 7 = 7 [2, 2, 2] 8 = 2 * 2 * 2 [3, 3] 9 = 3 * 3 [2, 5] 10 = 2 * 5 [11] 11 = 11 [2, 2, 3] 12 = 2 * 2 * 3 [13] 13 = 13 [2, 7] 14 = 2 * 7 [3, 5] 15 = 3 * 5 [2, 2, 2, 2] 16 = 2 * 2 * 2 * 2 [17] 17 = 17 [2, 3, 3] 18 = 2 * 3 * 3 [19] 19 = 19 [2, 2, 5] 20 = 2 * 2 * 5 [3, 7] 21 = 3 * 7 [2, 11] 22 = 2 * 11 [23] 23 = 23 [2, 2, 2, 3] 24 = 2 * 2 * 2 * 3 [5, 5] 25 = 5 * 5 [2, 13] 26 = 2 * 13 [3, 3, 3] 27 = 3 * 3 * 3 [2, 2, 7] 28 = 2 * 2 * 7 [29] 29 = 29 [2, 3, 5] 30 = 2 * 3 * 5 [31] 31 = 31 [2, 2, 2, 2, 2] 32 = 2 * 2 * 2 * 2 * 2 [3, 11] 33 = 3 * 11 [2, 17] 34 = 2 * 17 [5, 7] 35 = 5 * 7 [2, 2, 3, 3] 36 = 2 * 2 * 3 * 3 [37] 37 = 37 [2, 19] 38 = 2 * 19 [3, 13] 39 = 3 * 13 #判断一个数是否质数 def prime(num): if (num < 2): return False; sqr = int(math.sqrt(num))+1; for i in range(2, sqr): if (num%i==0): return False; return True; #分解质因数 def primeFactor(num, lists): if (num < 2): lists.append(num); return lists; elif (prime(num) == True): lists.append(num); return lists; else: sqr = int(math.sqrt(num))+1; i = 2; while i <= sqr: if (num % i == 0 and prime(i) == True): lists.append(i); num = num//i; break; i+=1; return primeFactor(num, lists); if __name__ == '__main__': for i in range(40): lists = primeFactor(i, []) print(lists); s = '{0} = '.format(i); for j in range(len(lists)-1): s += '{0} * '.format(lists[j]); s += '{0}'.format(lists[-1]); print(s); >>> [3, 3, 37, 333667] 111111111 = 3 * 3 * 37 * 333667 >>> [2, 3, 3, 5, 3607, 3803] 1234567890 = 2 * 3 * 3 * 5 * 3607 * 3803 >>> [3, 3, 11, 2153, 57920960187043] 12345678900987654321 = 3 * 3 * 11 * 2153 * 57920960187043 //约计算3秒 >>> [3, 3, 11, 41, 101, 271, 3541, 9091, 27961] 99999999999999999999 = 3 * 3 * 11 * 41 * 101 * 271 * 3541 * 9091 * 27961 >>> 3 * 3 * 11 * 41 * 101 * 271 * 3541 * 9091 * 27961 99999999999999999999 if __name__ == '__main__': bound = 99999999999999999999; for i in range(bound, bound+1): lists = primeFactor(i, []) print(lists); s = '{0} = '.format(i); for j in range(len(lists)-1): s += '{0} * '.format(lists[j]); s += '{0}'.format(lists[-1]); print(s);
<span style="font-size:18px;">import fractions; gcd(12, 20);</span>
<span style="font-size:18px;">def lcm(a, b): return a*b//gcd(a, b); if __name__ == '__main__': print(lcm(12, 30));</span>
<span style="font-size:18px;">def lcm(a, b): return a*b//gcd(a, b); if __name__ == '__main__': print(lcm(60, 42));</span>
本节到此结束,欲知后事如何,请看下回分解。