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
Contents
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 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 | # pico_sieve.py
# Raspberry Pi Pico - Sieve of Eratosthenes demo
# Calculate all the prime numbers within a range of low integers. Note that
# this is purely computational; nothing depends specifically on CircuitPython,
# and this works fine in a normal desktop Python 3 as well.
import time
import math
size = 40000
# Capture an initial time stamp to compute runtime.
start = time.monotonic()
# Array of boolean flags, one per integer. A True represents a possible prime number,
# a False a composite number.
flags = [True]*size
# Walk up through the array, identifying all prime numbers and removing their
# multiples from the set of integers by setting the corresponding flag to False.
# Note that the scan can stop once values are reached for which no unique
# multiples are included in the array.
for i in range(2, int(math.sqrt(size))):
if flags[i] is True:
# This value is a prime, now remove all the multiples from the set.
# Note that this marking may begin at i*i since all lower multiples will
# have already been removed.
multiple = i * i
while multiple < size:
flags[multiple] = False
multiple += i
# Capture an final time stamp to compute runtime.
end = time.monotonic()
# Any remaining True values are prime
print("Prime numbers:")
for i in range(2, size):
if flags[i] is True:
print(i)
print("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 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 | # pico_sieve_ulab.py
# Raspberry Pi Pico - Sieve of Eratosthenes demo using ulab native types
# Calculate all the prime numbers within a range of low integers. Note that
# this is purely computational. This version uses the CircuitPython ulab module
# to represent the flag array using native machine data types. This speeds up
# operation but also uses less memory, allowing the array to be larger. The
# ulab module implements a subset of numpy.
import time
import math
# documentation: https://circuitpython.readthedocs.io/en/latest/shared-bindings/ulab/
import ulab
size = 40000
# Capture an initial time stamp to compute runtime.
start = time.monotonic()
# Array of boolean flags, one per integer. A True represents a possible prime number,
# a False a composite number.
flags = ulab.ones((size), dtype=ulab.bool)
# Walk up through the array, identifying all prime numbers and removing their
# multiples from the set of integers by setting the corresponding flag to False.
# Note that the scan can stop once values are reached for which no unique
# multiples are included in the array.
for i in range(2, int(math.sqrt(size))):
if flags[i]:
# This value is a prime, now remove all the multiples from the set.
# Note that this marking may begin at i*i since all lower multiples will
# have already been removed.
multiple = i * i
if multiple < size:
# Set a slice of the array to zeros in one step using fast iteration in native code.
flags[multiple::i] = 0
# Capture an final time stamp to compute runtime.
end = time.monotonic()
# Any remaining True values are prime
print("Prime numbers:")
for i in range(2, size):
if flags[i]:
print(i)
print("Sieve running time:", end - start, "seconds.")
|