Blog Archives

Number theory determinant and SymPy

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

Tagged with: , ,
Posted in Math, Python

A prime-generating formula and SymPy

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

Tagged with: , ,
Posted in Python

Relating Airy and Bessel functions

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

Tagged with: , , ,
Posted in Math, Python

SymPy book

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

Tagged with: , ,
Posted in Python

Need a 12-digit prime?

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

Tagged with: ,
Posted in Python

Synchronizing cicadas with Python

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

Tagged with: ,
Posted in Math, Python

Recognizing numbers

I was playing around with SymPy, a symbolic math package for Python, and ran across nsimplify. It takes a floating point number and tries to simplify it: as a fraction with a small denominator, square root of a small integer,

Tagged with: ,
Posted in Math, Python

Rolling dice for normal samples: Python version

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

Tagged with: , , ,
Posted in Math, Python

Moments of mixtures

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.)

Tagged with: ,
Posted in Python, Statistics

New introduction to SciPy

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

Tagged with: ,
Posted in Python

Suffix primes

MathUpdate tweeted this afternoon that Any number made by removing the first n digits of 646216567629137 is still prime. and links to sequence A012885 in the Online Encyclopedia of Integer Sequences (OEIS). The OEIS heading for the sequence is Suffixes

Tagged with:
Posted in Python

Wallpaper and phase portraits

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

Tagged with: ,
Posted in Math, Python

Exact chaos

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

Tagged with: ,
Posted in Python

Python animation for mechanical vibrations

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

Tagged with:
Posted in Python

Continued fractions with Sage

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

Tagged with:
Posted in Python