fork download
  1. # https://stackoverflow.com/questions/65017966/given-n-find-how-many-n-digit-numbers-are-there-such-that-the-number-has-prime/65020994?noredirect=1#comment114995029_65020994
  2.  
  3. n = 2
  4. m = 4
  5. k = 0
  6. primes = [2,3,5,7]
  7. non_primes = [1,4,6,8,9]
  8.  
  9. row = [2, 2, 1, 0]
  10.  
  11. # (1) Transform the current row of remainders,
  12. # each storing a count, multiplying by 10:
  13.  
  14. new_row = [0] * m
  15.  
  16. for r in range(m):
  17. new_r = (10 % m * r) % m
  18. new_row[new_r] += row[r]
  19.  
  20. row = new_row
  21.  
  22. print(row)
  23.  
  24. # (2) Create new counts by using the new
  25. # possible digits:
  26.  
  27. i = 2
  28. new_row = [0] * m
  29. for d in (primes if i in primes else non_primes):
  30. for r in range(m):
  31. new_r = (r + d) % m
  32. new_row[new_r] += row[r]
  33.  
  34. row = new_row
  35.  
  36. print(row)
Success #stdin #stdout 0.02s 9208KB
stdin
Standard input is empty
stdout
[3, 0, 2, 0]
[2, 7, 3, 8]