Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168733
Image: ubuntu2004
# -*- coding: cp936 -*- class queens(object): def __init__(self,Num): self.Num = Num a,self.ans = list(range(Num)),[] import itertools for B in itertools.permutations(a): Flag,N = True,list(enumerate(B)) for (x,y) in itertools.combinations(N,2): if abs(x[0]-y[0]) == abs(x[1]-y[1]): Flag = False break if Flag: self.ans.append( N ) def __str__(self): return str(self.ans) def fact(x): if x<=1:return x else: return x*fact(x-1) import time START = time.time() Num = 11 print fact(Num) A = queens(Num) END=time.time() print len(A.ans),END-START
362880 352 2.92183494568
import time for i in range(4,17): START = time.time() A = queens(i) print i,'\t',time.time()-START,'\t',len(A.ans)
4 0.00103783607483 2 5 0.00075101852417 10 6 0.00458002090454 4 7 0.0350069999695 40 8 0.303432941437 92 9 2.94067978859 352 10 31.2748670578 724 11 367.872130156 2680