Numerical Examples - Raspberry Pi Pico¶
The following short Python programs will demonstrate essential operation of the
Raspberry Pi Pico board. These use only the onboard hardware and the serial
console. Each can be run by copying the program into code.py
on the
CIRCUITPY drive offered by the board. The text can be pasted directly from this
page, or each file can be downloaded from the CircuitPython sample code folder on this site.
Related Pages
Sieve of Eratosthenes¶
The Sieve of Eratosthenes is an ancient algorithm for finding prime numbers which was popular as a compiler benchmark in the 1980s.
Direct download: sieve.py.
1# pico_sieve.py
2
3# Raspberry Pi Pico - Sieve of Eratosthenes demo
4
5# Calculate all the prime numbers within a range of low integers. Note that
6# this is purely computational; nothing depends specifically on CircuitPython,
7# and this works fine in a normal desktop Python 3 as well.
8
9import time
10import math
11
12size = 40000
13
14# Capture an initial time stamp to compute runtime.
15start = time.monotonic()
16
17# Array of boolean flags, one per integer. A True represents a possible prime number,
18# a False a composite number.
19flags = [True]*size
20
21# Walk up through the array, identifying all prime numbers and removing their
22# multiples from the set of integers by setting the corresponding flag to False.
23# Note that the scan can stop once values are reached for which no unique
24# multiples are included in the array.
25for i in range(2, int(math.sqrt(size))):
26 if flags[i] is True:
27 # This value is a prime, now remove all the multiples from the set.
28 # Note that this marking may begin at i*i since all lower multiples will
29 # have already been removed.
30 multiple = i * i
31 while multiple < size:
32 flags[multiple] = False
33 multiple += i
34
35# Capture an final time stamp to compute runtime.
36end = time.monotonic()
37
38# Any remaining True values are prime
39print("Prime numbers:")
40
41for i in range(2, size):
42 if flags[i] is True:
43 print(i)
44
45print("Sieve running time:", end - start, "seconds.")
Sieve of Eratosthenes - ulab-optimized¶
This version uses the CircuitPython ulab module to speed up the calculation using an array of a native machine data type. CircuitPython is considerably slower than native machine code, so for numerical array calculations ulab can be used to execute iterative operations using fast native code.
Direct download: sieve.py.
1# pico_sieve_ulab.py
2
3# Raspberry Pi Pico - Sieve of Eratosthenes demo using ulab native types
4
5# Calculate all the prime numbers within a range of low integers. Note that
6# this is purely computational. This version uses the CircuitPython ulab module
7# to represent the flag array using native machine data types. This speeds up
8# operation but also uses less memory, allowing the array to be larger. The
9# ulab module implements a subset of numpy.
10
11import time
12import math
13
14# documentation: https://circuitpython.readthedocs.io/en/latest/shared-bindings/ulab/
15import ulab
16
17size = 40000
18
19# Capture an initial time stamp to compute runtime.
20start = time.monotonic()
21
22# Array of boolean flags, one per integer. A True represents a possible prime number,
23# a False a composite number.
24flags = ulab.ones((size), dtype=ulab.bool)
25
26# Walk up through the array, identifying all prime numbers and removing their
27# multiples from the set of integers by setting the corresponding flag to False.
28# Note that the scan can stop once values are reached for which no unique
29# multiples are included in the array.
30for i in range(2, int(math.sqrt(size))):
31 if flags[i]:
32 # This value is a prime, now remove all the multiples from the set.
33 # Note that this marking may begin at i*i since all lower multiples will
34 # have already been removed.
35 multiple = i * i
36
37 if multiple < size:
38 # Set a slice of the array to zeros in one step using fast iteration in native code.
39 flags[multiple::i] = 0
40
41# Capture an final time stamp to compute runtime.
42end = time.monotonic()
43
44# Any remaining True values are prime
45print("Prime numbers:")
46
47for i in range(2, size):
48 if flags[i]:
49 print(i)
50
51print("Sieve running time:", end - start, "seconds.")