Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 100pointwords.txt
Created January 6, 2015 23:33
English words with letter sum of 100, where A=1, B=2, C=3, etc.
abatements abettors abrogative absconders acclimation
accounter accumulate acknowledge acolytes acquitted
acromegaly activates addressing adiabatically adulthood
advantaging adverting aerofoils aerometer affectation
afghanistan aggresses agrology airdrops alertest
alienation alkalinize allottable alpinist alternated
ambulating amiableness amortise amphiboles amputees
amusedly analysis anchoritic aneurism anginous
anglophobia animaters annually answerable anterior
anvilling aphrodisia apoplectic apostacy appeasers
@Radcliffe
Radcliffe / mplswards.json
Created January 8, 2015 21:05
Minneapolis Wards - GeoJSON
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Radcliffe
Radcliffe / index.html
Created January 11, 2015 22:58
myPlot
<!doctype HTML>
<meta charset = 'utf-8'>
<html>
<head>
<link rel='stylesheet' href='//cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.15-beta/nv.d3.min.css'>
<script src='//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js' type='text/javascript'></script>
<script src='//d3js.org/d3.v3.min.js' type='text/javascript'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/nvd3/1.1.15-beta/nv.d3.min.js' type='text/javascript'></script>
<script src='//nvd3.org/assets/lib/fisheye.js' type='text/javascript'></script>
@Radcliffe
Radcliffe / splitdigits.py
Last active August 29, 2015 14:14
Arrange consecutive digits to form two numbers whose product is as large as possible.
# Problem: Arrange the consecutive digits {x,x+1,..,y} to form two numbers A and B
# so that the product A*B is as large as possible.
#
# The generator `splitdigits` returns all candidate triples (P, A, B) where P = A * B.
def splitdigits(x=1, y=9, base=10):
if x == y:
yield (0, y, 0)
else:
for P, A, B in splitdigits(x+1, y, base):