Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Radcliffe / squareplus2graph.py
Created February 16, 2015 00:47
A "square-plus-two" digraph
# This Python script generates a directed graph with
# vertex set V = {0, 1, 2, ..., m-1} and edge set
# E = {(n, f(n)) : n in V} where f(n) = n*n + c (mod m).
#
# Requires the python-igraph package (http://igraph.org/python/)
#
# Author: David Radcliffe (14 February 2014)
# Based on an idea of James Tanton
# https://twitter.com/jamestanton/status/549558473868849152
#
@Radcliffe
Radcliffe / scrape_youtube_playlist.py
Last active August 29, 2015 14:23
Find the titles and running times of videos in a YouTube playlist by scraping the web page
# Python script to list the titles and running times of the videos
# in a YouTube playlist.
# Note: Only works for up to 100 videos. TODO: Fix this bug.
# The playlist id can be found in the playlist's URL.
playlist_id = "PLt5AfwLFPxWLNZRKWlcRmTABh_SExiiCj"
from lxml import html
import requests
@Radcliffe
Radcliffe / zips-tc-metro.txt
Created July 3, 2015 19:31
ZIP codes in the seven county twin cities metropolitan area
ZIP Code,County
55001,Washington County
55003,Washington County
55005,Anoka County
55010,Dakota County
55011,Anoka County
55014,Anoka County
55016,Washington County
55020,Scott County
55024,Dakota County
@Radcliffe
Radcliffe / invfactorial.py
Last active August 29, 2015 14:26
Inverse factorial
from math import log, sqrt, pi, e
from scipy.special import lambertw
fomm numpy import real
# http://mathforum.org/kb/message.jspa?messageID=342551&tstart=0
def invfactorial(n):
""" Approximate inverse factorial"""
L = log((x + .036534) / sqrt(2*pi))
return real(L/W(L/e) - .5)
"""
Problem:
Arrange the numbers from 1 to m*n in an m-by-n grid so that
no two consecutive numbers are placed in adjacent cells.
In how many ways can this be done? Two squares are considered
touching if they share a corner.
Example:
@Radcliffe
Radcliffe / rearrange_power_of_three.py
Last active December 16, 2015 03:27
Powers of 3 having the same digits
"""
James Tanton @jamestanton asked:
Is there a power of three whose digits can be rearranged to form
another power of three?
(https://twitter.com/jamestanton/status/676773021223272448)
The following Python script searches for examples. There is no power
of three, less than 3^200000, whose digits can be rearranged to form
another power of three.
"""
@Radcliffe
Radcliffe / Euler_four_square_identity.py
Created December 29, 2015 21:50
Proof of Euler's four square identity using Python and SymPy
"""
Euler's four square theorem states that
(x1^2 + x2^2 + x3^2 + x4^2) *
(y1^2 + y2^2 + y3^2 + y4^2) =
z1^2 + z2^2 + z3^2 + z4^2,
where
z1 = x1*y1 + x2*y2 + x3*y3 + x4*y4
z2 = x1*y2 - x2*y1 - x3*y4 + x4*y3
@Radcliffe
Radcliffe / reshape-expenditures.R
Last active January 5, 2016 05:07
Reshape expenditure data
library(dplyr)
d <- read.csv('expenditures.csv')
cats <- unique(d$ProgramCategory)
newnames <- gsub("[- ]", "", cats)
e <- d %>%
filter(ProgramCategory == cats[1]) %>%
select(Year:StudentsServed)
@Radcliffe
Radcliffe / walmart-closings.csv
Last active January 16, 2016 19:41
Walmart closings announced Jan 2016
Location DateClosedToPublic StoreNumber StoreType lon lat
14331 Count Rd. 99, Headland, AL 2016-01-28 2173 Walmart Express -85.3259952 31.3607946
18 Apple Way, Ashford, AL 2016-01-28 2011 Walmart Express -85.237821 31.1765287
952 E. Lawrence Harris Hwy, Slocomb, AL 2016-01-28 2165 Walmart Express -85.5907905 31.1076635
407 West Washington St., Abbeville, AL 2016-01-28 2186 Walmart Express -85.2594391 31.5737215
6361 Hwy 72 East Gurley, AL 2016-01-28 2235 Walmart Express -86.3700933 34.6947341
87395 US Hwy 278, Snead, AL 2016-01-28 2260 Walmart Express -86.3915109 34.1173155
3530 Cathedral Caverns Hwy, Grant, AL 2016-01-28 3769 Walmart Express -86.2611173 34.5014024
10188 Hwy 431 South, New Hope, AL 2016-01-28 3779 Walmart Express -86.4308169 34.5482599
720 N Hwy 71, Mansfield, AR 2016-01-28 2498 Walmart Express -94.2619279 35.077937
@Radcliffe
Radcliffe / crux-mathematicorum-M379.py
Created February 2, 2016 18:06
Solution to Crux Mathematicorum M379
"""
Problem M379, Crux Mathematicorum
Proposed by John Grant McLoughlin, University of New Brunswkci,
Fredericton, NB.
The integers 27+C, 555+C, and 1371+C are all perfect squares, the square
roots of which form an arithmetic sequence. Determine all possible values
of C.
"""