Let σ(n) be the sum of the positive divisors of n and let gcd(a, b) be the greatest common divisor of a and b. Form an n by n matrix M whose (i, j) entry is σ(gcd(i, j)). Then the…

Let σ(n) be the sum of the positive divisors of n and let gcd(a, b) be the greatest common divisor of a and b. Form an n by n matrix M whose (i, j) entry is σ(gcd(i, j)). Then the…

Mills’ constant is a number θ such that the integer part of θ raised to a power of 3 is always a prime. We’ll see if we can verify this computationally with SymPy. from sympy import floor, isprime from sympy.mpmath…

The Airy functions Ai(x) and Bi(x) are independent solutions to the differential equation For negative x they act something like sin(x) and cos(x). For positive x they act something like exp(x) and exp(-x). This isn’t surprising if you look at…

There’s a new book on SymPy, a Python library for symbolic math. The book is Instant SymPy Starter by Ronan Lamy. As far as I know, this is the only book just on SymPy. It’s only about 50 pages, which…

You may have seen the joke “Enter any 12-digit prime number to continue.” I’ve seen it floating around as the punchline in several contexts. So what do you do if you need a 12-digit prime? Here’s how to find the…

Suppose you want to know when your great-grandmother was born. You can’t find the year recorded anywhere. But you did discover an undated letter from her father that mentions her birth and one curious detail: the 13-year and 17-year cicadas…

A handful of dice can make a decent normal random number generator, good enough for classroom demonstrations. I wrote about this a while ago. My original post included Mathematica code for calculating how close to normal the distribution of the sum…

I needed to compute the higher moments of a mixture distribution for a project I’m working on. I’m writing up the code here in case anyone else finds this useful. (And in case I’ll find it useful in the future.)…

The Python stack for scientific computing is more modular than say R or Mathematica. Python is a general-purpose programming language that has libraries for scientific computing. R and Mathematica are statistical and mathematical programming languages that have general-purpose features. The…

There’s a useful tradition of using a typewriter font, or more generally some monospaced font, for bits of code sprinkled in prose. The practice is analogous to using italic to mark, for example, a French mot dropped into an English…

Suppose you want to create a background image that tiles well. You’d like it to be periodic horizontally and vertically so that there are no obvious jumps when the image repeats. Functions like sine and cosine are period along the…

Pick a number x between 0 and 1. Then repeatedly replace x with 4x(1-x). For almost all starting values of x, the result exhibits chaos. Two people could play this game with starting values very close together, and eventually their…

Stéfan van der Walt wrote some Python code to animate the system described in yesterday’s post on mechanical vibrations. Stéfan posted his code on github. It currently illustrates undamped free vibrations, but could be modified to work with damped or…

My previous post looked at continued fractions and rational approximations for e and gave a little Python code. I found out later there’s a more direct way to do this in Python using Sage. At its simplest, the function continued_fraction…

This morning Dave Richeson posted a humorous fake proof that depends on the famous approximation 22/7 for pi. It occurred to me that nearly everyone knows a decent rational approximation to pi. Some people may know more. But hardly anyone,…