Advertisement
Python253

Ulam_spiral_(for_primes)_Test

Feb 26th, 2019
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.62 KB | None | 0 0
  1. 33 Lines of Code:
  2. Start = 41
  3.  
  4. # coding=UTF-8
  5. from __future__ import print_function, division
  6. from math import sqrt
  7.  
  8. def cell(n, x, y, start=41):
  9.     d, y, x = 0, y - n//2, x - (n - 1)//2
  10.     l = 2*max(abs(x), abs(y))
  11.     d = (l*3 + x + y) if y >= x else (l - x - y)
  12.     return (l - 1)**2 + d + start - 1
  13.  
  14. def show_spiral(n, symbol='# ', start=1, space=None):
  15.     top = start + n*n + 1
  16.     is_prime = [False,False,True] + [True,False]*(top//2)
  17.     for x in range(3, 1 + int(sqrt(top))):
  18.         if not is_prime[x]: continue
  19.         for i in range(x*x, top, x*2):
  20.             is_prime[i] = False
  21.  
  22.     cell_str = lambda x: f(x) if is_prime[x] else space
  23.     f = lambda _: symbol # how to show prime cells
  24.  
  25.     if space == None: space = ' '*len(symbol)
  26.  
  27.     if not len(symbol): # print numbers instead
  28.         max_str = len(str(n*n + start - 1))
  29.         if space == None: space = '.'*max_str + ' '
  30.         f = lambda x: ('%' + str(max_str) + 'd ')%x
  31.  
  32.     for y in range(n):
  33.         print(''.join(cell_str(v) for v in [cell(n, x, y, start) for x in range(n)]))
  34.     print()
  35.  
  36. show_spiral(50, symbol=u'♞', space=u'♘') # white are the primes
  37.  
  38.  
  39.  
  40.  
  41.  
  42. ...
  43. RESULT:
  44. ...
  45.  
  46.  
  47. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘
  48. ♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘
  49. ♘♘♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘
  50. ♘♘♘♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘
  51. ♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♞
  52. ♞♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘
  53. ♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘
  54. ♞♘♘♘♘♘♞♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♞♘
  55. ♘♘♘♘♘♞♘♘♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘
  56. ♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘
  57. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞
  58. ♘♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘
  59. ♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘
  60. ♘♘♘♘♞♘♞♘♘♘♞♘♞♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘
  61. ♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♞♘♘♘♘♘♞
  62. ♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘
  63. ♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘
  64. ♘♘♞♘♞♘♞♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♞♘♞♘♘♘
  65. ♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘
  66. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♞♘
  67. ♘♘♘♘♘♞♘♘♘♞♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♞♘♘
  68. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘
  69. ♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♞♘♞♘♞♘♘♘♘♘♞♘♞♘♞♘♘♘♘♘♘♘♘
  70. ♘♘♘♘♞♘♘♘♞♘♞♘♞♘♞♘♞♘♞♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘
  71. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘
  72. ♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♞♞♘♞♘♞♘♞♘♘♘♞♘♞♘♞♘♘♘♘♘♞♘♘♘
  73. ♘♞♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘
  74. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘
  75. ♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♞
  76. ♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘
  77. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘
  78. ♘♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♘♘
  79. ♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘
  80. ♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘
  81. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♞
  82. ♞♘♘♘♘♘♘♘♞♘♞♘♞♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♞♘♘♘♞♘♞♘♘♘
  83. ♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘
  84. ♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘
  85. ♘♞♘♘♘♘♘♞♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘
  86. ♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘
  87. ♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞
  88. ♘♘♘♘♞♘♘♘♘♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘
  89. ♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘
  90. ♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘
  91. ♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘
  92. ♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘
  93. ♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘
  94. ♞♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♞♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘
  95. ♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♞♘♘♘♘
  96. ♘♘♘♘♞♘♘♘♘♘♘♘♘♘♘♘♘♘♞♘♘♘♘♘♞♘♘♘♞♘♞♘♘♘♘♘♞♘♘♘♞♘♘♘♘♘♞♘♘♘
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement