File:Ulam spiral 10x.png

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Original file(2,010 × 2,010 pixels, file size: 8 KB, MIME type: image/png)

Summary

Description
English: Plot of the Ulam spiral on a 201x201 grid. The image has been upscaled 10x so that it will not be too tiny on modern displays.
Date
Source Own work
Author Morn

Python source code

# Create image of Ulam spiral

SIZE = 201    # image size (must be odd)

P = [0] * (SIZE * SIZE + 1)

for C in range(2, len(P)):
    if P[C] == 0:
        #print(C)
        for x in range(2, len(P)):
            z = x * C
            if z >= len(P):
                break
            P[z] = 1

cdir = 0
dirs = [(1,0), (0,-1), (-1,0), (0,1)]
out = [["0"] * SIZE for y in range(SIZE)]
px, py = SIZE//2, SIZE//2
nstep = []
cstep = 1
for x in range(SIZE * SIZE):
    nstep.append(cstep)
    nstep.append(cstep)
    cstep += 1

cstep = nstep.pop(0)
for p in range(SIZE * SIZE):
    if p > 0 and P[p+1] == 0:
        out[py][px] = "1"
    px += dirs[cdir % 4][0]
    py += dirs[cdir % 4][1]
    cstep -= 1
    if cstep == 0:    
        cdir += 1
        cstep = nstep.pop(0)

# Save image in NetPBM bitmap format
f = open("ulam.pbm", "w")
f.write(f"P1\n{SIZE} {SIZE}\n")
for y in range(SIZE):
    f.write(''.join(out[y]) + "\n")
f.close()

# Convert PBM to PNG with ImageMagick (and increase resolution 10x):
#  mogrify -format png -interpolate Nearest -filter point -resize 1000% ulam.pbm

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

17 April 2023

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current10:38, 17 April 2023Thumbnail for version as of 10:38, 17 April 20232,010 × 2,010 (8 KB)MornUploaded own work with UploadWizard
The following pages on the English Wikipedia use this file (pages on other projects are not listed):

Global file usage

The following other wikis use this file:

Metadata