Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # runs in 0.07 seconds
- def unidig(n):
- used = [False]*10
- while n>0:
- l=n%10
- if used[l]:
- return False
- used[l]=True
- n/=10
- return True
- m = [1,2,3,5,7,11,13,17]
- # generate possible final endings, then skip 17 later (yuk)
- endings = [str(n) for n in xrange(100,1000) if n%17==0 and unidig(n)]
- for i in xrange(len(m)-2,-1,-1):
- ne = []
- for pe in endings:
- avail = [True]*10
- for used in pe:
- l=int(used)
- avail[l]=False
- for d in [n for n in xrange(10) if avail[n]]:
- t = str(d)+pe[0:2]
- if (int(t)%m[i])==0:
- a=t+pe[2:]
- ne.append(a)
- endings = ne
- print sum(int(n) for n in endings)
Add Comment
Please, Sign In to add comment