Guest User

Untitled

a guest
May 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # runs in 0.07 seconds
  4.  
  5. def unidig(n):
  6. used = [False]*10
  7. while n>0:
  8. l=n%10
  9. if used[l]:
  10. return False
  11. used[l]=True
  12. n/=10
  13. return True
  14.  
  15. m = [1,2,3,5,7,11,13,17]
  16.  
  17. # generate possible final endings, then skip 17 later (yuk)
  18. endings = [str(n) for n in xrange(100,1000) if n%17==0 and unidig(n)]
  19.  
  20. for i in xrange(len(m)-2,-1,-1):
  21. ne = []
  22. for pe in endings:
  23. avail = [True]*10
  24. for used in pe:
  25. l=int(used)
  26. avail[l]=False
  27.  
  28. for d in [n for n in xrange(10) if avail[n]]:
  29. t = str(d)+pe[0:2]
  30. if (int(t)%m[i])==0:
  31. a=t+pe[2:]
  32. ne.append(a)
  33.  
  34. endings = ne
  35.  
  36. print sum(int(n) for n in endings)
Add Comment
Please, Sign In to add comment