Skip to content

Instantly share code, notes, and snippets.

@nlowe
Created November 22, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlowe/63232c22bb2c6204e0f5 to your computer and use it in GitHub Desktop.
Save nlowe/63232c22bb2c6204e0f5 to your computer and use it in GitHub Desktop.
Pratt sequence generator in python
# Original script by Damian Yerrick
# http://stackoverflow.com/a/25964763
def pratt(max_size):
"""Generate a sorted list of products of powers of 2 and 3 below max_size"""
# for https://stackoverflow.com/q/25964453/2738262
products = []
pow3 = 1 # start with q = 0
while pow3 <= max_size:
# At this point, pow3 = 3**q, so set p = 0
pow2 = pow3
while pow2 <= max_size:
# At this point, pow2 = 2**p * 3**q
products.append(pow2)
pow2 = pow2 * 2 # this is like adding 1 to p
# now that p overflowed the maximum size, add 1 to q and start over
pow3 = pow3 * 3
# the Pratt sequence is the result of this process up to the given size
return sorted(products)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment