fork download
  1. # 0J/QsNGA0YXQvtC80LXQvdC60L4=
  2. def is_prime(n):
  3. if n < 2:
  4. return False
  5. for i in range(2, int(n**0.5) + 1):
  6. if n % i == 0:
  7. return False
  8. return True
  9.  
  10. def spiral_matrix(rows, cols):
  11. matrix = [[0] * cols for _ in range(rows)]
  12. num = 1
  13. left, right, top, bottom = 0, cols - 1, 0, rows - 1
  14.  
  15. while left <= right and top <= bottom:
  16.  
  17. for j in range(left, right + 1):
  18. matrix[top][j] = num
  19. num += 1
  20. top += 1
  21.  
  22.  
  23. for i in range(top, bottom + 1):
  24. matrix[i][right] = num
  25. num += 1
  26. right -= 1
  27.  
  28.  
  29. if top <= bottom:
  30. for j in range(right, left - 1, -1):
  31. matrix[bottom][j] = num
  32. num += 1
  33. bottom -= 1
  34.  
  35.  
  36. if left <= right:
  37. for i in range(bottom, top - 1, -1):
  38. matrix[i][left] = num
  39. num += 1
  40. left += 1
  41.  
  42. return matrix
  43.  
  44.  
  45.  
  46. rows = 6
  47. cols = 6
  48.  
  49.  
  50. matrix = spiral_matrix(rows, cols)
  51.  
  52.  
  53. new_matrix = [[1 if is_prime(num) else 0 for num in row] for row in matrix]
  54.  
  55.  
  56.  
  57. print("\nПеретворена матриця (1 - просте, 0 - ні):")
  58. for row in new_matrix:
  59. print(*row)
  60.  
Success #stdin #stdout 0.07s 14148KB
stdin
Standard input is empty
stdout
Перетворена матриця (1 - просте, 0 - ні):
0 1 1 0 1 0
0 0 0 1 0 1
1 0 0 0 0 0
0 1 0 0 0 0
1 0 1 0 0 0
0 0 0 1 0 1