def count_primes(x): count = 0 for e in x: if len(factors(e)) == 2: count += 1 return count
from ulam import ulam_spiral, factors colors = ximport('colors') # Create the Ulam spiral. grid_size = 31 grid = ulam_spiral(grid_size) # Fill the grid with the number of factors the integer in the (i,j) position has. for i in range(grid_size): for j in range(grid_size): grid[i][j] = len(factors(grid[i][j])) # Determine the maximum number of factors that one integer in the grid has. max_d = max(map(max, grid)) # Set up the canvas size. size(grid_size*max_d, grid_size*max_d) # Draw a small grid coloring the location of prime numbers. for i in range(grid_size): for j in range(grid_size): x = j*max_d y = i*max_d d = grid[i][j] oval(x+(max_d/2. - d/2.), y+(max_d/2. - d/2.), d, d, fill=color(0.0)) canvas.save('divisor_spiral.png')
# Create the Ulam spiral. grid_size = 7 grid = ulam_spiral(grid_size) # Set up the canvas size. square_size = 50 size(grid_size*square_size, grid_size*square_size) # Black lines. stroke(0) # Set the font properties. font("Helvetica", 20) align(align=CENTER) # Draw a small grid coloring the location of prime numbers. for i in range(grid_size): for j in range(grid_size): x = j*square_size y = i*square_size if len(factors(grid[i][j])) == 2: c = color(1.0, 0.0, 0.0, 0.5) else: c = color(1.0) rect(x, y, square_size, square_size, fill=c) text(str(grid[i][j]), x, y+square_size/2+8, width=square_size) canvas.save('small_spiral.png')