File size: 1,135 Bytes
c0419ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27d0603
c0419ac
 
 
27d0603
c0419ac
27d0603
c0419ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27d0603
c0419ac
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import matplotlib.pyplot as plt
import gradio as gr


def isprime(x):
    if x < 2:
        return False
    for i in range(2, x):
        if x % i == 0:
            return False
    return True


def ulamspiral(n):
    x = 0
    y = 0
    estado = 0 
    numsteps = 1
    cambio_dir = 0
    last_x = 0
    last_y = 0
    plt.figure(facecolor=(0, 0, 0), figsize=(20,20))
    plt.axis('off')
    for step in range(1, n+1):
        if isprime(step):
            plt.plot(x, y, "wo", markersize=8)
        if step != 1:
            plt.plot([last_x, x], [last_y, y], "w-", markersize=2)
        last_x = x
        last_y = y
        if estado == 4:
            estado = 0
        if estado == 0:
            x += 1
        elif estado == 1:
            y += 1
        elif estado == 2:
            x -= 1
        elif estado == 3:
            y -= 1
        
        if step % numsteps == 0:
            estado = (estado + 1) % 4
            cambio_dir += 1
            if cambio_dir % 2 == 0:
                numsteps += 1

    return plt.gcf()
    
iface = gr.Interface(ulamspiral, gr.inputs.Slider(1, 10000, 1), "plot")

iface.launch()