Skip to content

Instantly share code, notes, and snippets.

@willf
Last active November 10, 2017 15:07
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 willf/80aa5d31cffe7374c6cfa10dd79bc2d3 to your computer and use it in GitHub Desktop.
Save willf/80aa5d31cffe7374c6cfa10dd79bc2d3 to your computer and use it in GitHub Desktop.
# I was sitting on a bus whose number's prime
# factorization was 37 x 113. The prime factorization of the
# number of the bus in front of me was 11 x 373.
# I wonder how many other four-digit bus numbers
# have a prime factorization with exactly those digits?
# pip install sympy
from itertools import chain
from sympy.ntheory import factorint
def strp(n,p):
if p == 1:
return str(n)
return str(n) + "^" + str(p)
def strf(f):
return "*".join([strp(k, f[k]) for k in sorted(f.keys())])
def f(x):
fs = factorint(x)
if sorted(list(chain(*[list(str(f)) for f in fs.keys()]))) == ['1', '1', '3', '3', '7']:
if len(fs.keys()) == 2:
print(x, strf(fs))
for x in range(1000,10000):
f(x)
# 1781 13*137
# 2249 13*173
# 3707 11*337
# 4103 11*373
# 4121 13*317
# 4181 37*113
# 4247 31*137
# 4847 37*131
# 5321 17*313
# 5363 31*173
# 5627 17*331
# 8063 11*733
# 8249 73*113
# 9563 73*131
# 9827 31*317
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment