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