Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Radcliffe / download_patent_grants.py
Created December 30, 2014 05:56
Download all USPTO patent grants 1976 - present
# Download patent grants 1976 - 2014
import os
import urllib
import urllib2
import lxml.html
if not os.path.exists('data'):
os.mkdir('data')
@Radcliffe
Radcliffe / standardized_address.py
Last active August 29, 2015 14:07
Standardized addresses using SmartyStreets
# Address Standardization with SmartyStreets
import urllib2
import urllib
import json
# Note: Replace these lines with valid credentials
AUTH_ID = "########-####-####-####-############"
AUTH_TOKEN = "####################"
@Radcliffe
Radcliffe / harshad.py
Created October 6, 2014 00:22
A144261: a(n) = smallest k such that k*n is a Niven (or Harshad) number.
# A Harshad number (or Niven number) is a number that is divisible by its sum of digits.
def sum_of_digits(n):
s = 0
while n > 0:
s += (n % 10)
n /= 10
return s
# Alternative one-liner:
# sum_of_digits = lambda n: sum(map(int, str(n)))
@Radcliffe
Radcliffe / state-code-graph.py
Created June 19, 2014 00:56
Hamiltonian path that visits all 59 US states and possessions by two-letter code, changing one letter each time.
#!/usr/bin/env python
# Construct a graph whose nodes are labeled with the two-letter abbreviations
# for all 59 states and possessions of the United States. Two nodes are joined
# by an edge if and only if they differ by a single letter (i.e. they share
# the same first letter or the same second letter). For example, MN is
# adjacent to MI and TN, but not to NM.
# The output file can be read by GraphViz to produce a graphic file in a
# variety of formats. For example, the following command will produce a
@Radcliffe
Radcliffe / birthday.py
Last active August 29, 2015 14:01
Nit-picking the birthday paradox
#!/usr/bin/python
# The birthday paradox predicts that in a group of 23 people, the probability
# is about 51% that two people share the same birthday.
# p = 1 - (1 - 1/365) * (1 - 2/365) * ... * (1 - 22/365) = 0.507297
# This assumes that birthdays are distributed uniformly and independently
# among the 365 days of the year, excluding leap years.
@Radcliffe
Radcliffe / pythagorean_anagrams.py
Last active August 29, 2015 14:00
Find integral right triangles whose side lengths are digital permutations of each other.
# James Tanton asked: A 756-567-657 triangle has three side lengths with digits
# permutations of each other. Is there a right triangle with this property?
# https://twitter.com/jamestanton/status/459633602159202304
#
# This Python code generates all solutions with sides less than 10^9.
# The output is listed after the code.
from time import time
start_time = time()
@Radcliffe
Radcliffe / shufflesquare.py
Created April 21, 2014 13:43
Numbers whose squares can be rearranged to form exactly two other squares
#!/usr/bin/env python
#
# List the numbers n such that the digits of the square of n
# can be rearranged to form exactly two other squares.
# For example, 13 belongs to list because 13*13 = 169, and
# the digits of 169 can be rearranged to form the squares 196
# (= 14*14) and 961 (= 31*31); but no other squares can be formed
# by rearranging the digits of 169.
#
# Inspired by a question of James Tanton (@jamestanton on Twitter).
#!/usr/bin/env python
#
# This Python 2.7 script solves the following probability problem:
# If 20 distinct positive integers are chosen from the range 1 - 80
# (without replacement) what is the probability that their sum is 810?
# Source: Kim Zafra, http://lnkd.in/bmg4AR5
# Remarks:
@Radcliffe
Radcliffe / _.md
Created February 15, 2014 20:31
Tributary inlet
@Radcliffe
Radcliffe / _.md
Created December 21, 2013 16:04
Covering a grid with circles