Sign up

John D. Cook

Not verified No WebSub updates No webmention support Not yet validated

Applied Mathematics Consulting

Public lists
davewiner/hackerNewsStars
Fetched

John D. Cook
Text • John

Counting rooted trees

Combinatorial problems can be interesting for their own sake, but they are more interesting when there is a connection to a problem outside combinatorics, and the more unexpected the connection the better. Counting the number of unlabeled rooted trees [1] with n nodes is a pure mathematics problem. Designing numerical methods for solving differential equations […]

The post Counting rooted trees first appeared on John D. Cook.

John D. Cook
Text • John

Runge-Kutta order versus stages

The textbook version of the Runge-Kutta method for solving differential equations has 4 stages and has 4th order error. For lower order versions of RK the number of stages s also matches the order of the error p. But in order to achieve error on the order of p ≥ 5, you need more than p […]

The post Runge-Kutta order versus stages first appeared on John D. Cook.

John D. Cook
Text • John

Solving the RK4 design equations

I was digging into the Runge-Kutta method for solving differential equations and a line from [1] piqued my curiosity. These calculations, which are not reproduced in Kutta’s paper (they are however in Huen (1900)), are very tedious. The calculations are a set of eight constraints that the parameters of a fourth-order Runge-Kutta method must satisfy. […]

The post Solving the RK4 design equations first appeared on John D. Cook.

John D. Cook
Text • John

Inverse factorial improved

A couple years ago I wrote about how to compute the inverse of factorial. I used that code in writing the previous post because the post required solving the equation ⌊log2(n!)⌋ = b given b. That is, given a number of bits b, find the smallest value of n such that n! > 2b. What the […]

The post Inverse factorial improved first appeared on John D. Cook.

John D. Cook
Text • John

Cryptographic Keys and Decks of Cards

The previous post looked at the idea of storing a cryptographic key in the order of a deck of cards. A deck of 52 cards can store 225 bits of data because ⌊log2(52!)⌋ = 225. Here ⌊x⌋ is x rounded down to the nearest integer. If we want to store bigger keys, we’re going to need […]

The post Cryptographic Keys and Decks of Cards first appeared on John D. Cook.

John D. Cook
Text • John

Hiding data in permutations

The latest issue of Paged Out! has an article by Stephen Hewitt “An off-line backup of your cryptographic key using playing cards.” The idea is to use a deck of 52 to store a 128-bit cryptographic key. To erase the key, shuffle the deck. Hewitt gives his algorithm for embedding a key, one that can […]

The post Hiding data in permutations first appeared on John D. Cook.

John D. Cook
Text • John

Counting permutations with roots

My post from yesterday on permutation roots ends with a Mathematica code for finding the probability that a permutation of n elements has a kth root. This is done by finding the coefficient of xn in the generating function I wanted to say more about this, and look at implementing the same code in SymPy. […]

The post Counting permutations with roots first appeared on John D. Cook.

John D. Cook
Text • John

Printing floating point numbers in binary

It’s well known that you can convert the base 16 (hex) representation of an integer to the base 2 (binary) representation by simply converting each digit from hex to binary. For example, CAFEhex = 1100 1010 1111 1110two I imagine it’s less well known that you can do the same thing with floating point numbers. […]

The post Printing floating point numbers in binary first appeared on John D. Cook.

John D. Cook
Text • John

Permutation roots

Let σ be a permutation on n elements. If there is a permutation τ such that applying τ twice has the same effect on the list of elements as applying σ once, we say σ = τ² and τ is a square root of σ. If we let our n elements be the integers 0 […]

The post Permutation roots first appeared on John D. Cook.

John D. Cook
Text • John

exp_q

The function expq(x) is defined by taking the power series for exp(x) and keeping only the terms whose index is a multiple of q. For example, exp2(x) keeps only the even-numbered terms in the exponential power series and so equals cosh(x). In general, The first sum uses Iverson’s bracket notation: a Boolean expression in brackets […]

The post exp_q first appeared on John D. Cook.

John D. Cook
Text • John

Excel column numbering

I was working with a wide spreadsheet from a client the other day and I had to convert between Excel column labels and column numbers. I had never paid attention to how Excel labels columns and implicitly thought it was base 26 using letters rather than digits. But then I realized that’s not right. Excel […]

The post Excel column numbering first appeared on John D. Cook.

John D. Cook
Text • John

An almost periodic function

This post takes a more abstract view of the previous post. That post looked at the concrete question of whether a number ever has the same sine in radians as in degrees. The relation between radians and degrees is irrelevant except that π/180 is an irrational number. Suppose α and β are two positive numbers […]

The post An almost periodic function first appeared on John D. Cook.

John D. Cook
Text • John

When sine of x degrees equals sine of x radians

Ordinarily the sine of x radians and the sine of x degrees are very different numbers. Having your calculator in radian mode when it should be in degree mode, or vice versa, results in a major error. But sometimes it doesn’t matter. A trivial example is when x = 0. A more interesting example is […]

The post When sine of x degrees equals sine of x radians first appeared on John D. Cook.

John D. Cook
Text • John

Forensic accounting in Python

I recently had a project in which I had to reverse engineer a data analysis. There was some ambiguity regarding which of several possibilities someone chose for several of the variables, something analogous to the following example. Suppose you have three numbers with uncertain values with a known, or at least purported, sum. The first […]

The post Forensic accounting in Python first appeared on John D. Cook.

John D. Cook
Text • John

Locally everywhere does not imply everywhere

A couple days ago, Levent Alpöge, a mathematician working at Anthropic, discovered a counterexample to the Jacobian conjecture using Claude Fable 5. I was curious whether most mathematicians were trying to prove or disprove the conjecture, so I asked Claude. Before a counterexample to the Jacobian conjecture was found, did most mathematicians believe it was […]

The post Locally everywhere does not imply everywhere first appeared on John D. Cook.

John D. Cook
Text • John

Volume to Area ratio for Regular Solids

The volume of a sphere of radius r is V = 4πr³ / 3 and the surface area is A = 4πr² and so the ratio of volume to area is V / A = r / 3. Surprisingly, the same ratio holds for all regular solids if r is the radius of the largest sphere […]

The post Volume to Area ratio for Regular Solids first appeared on John D. Cook.

John D. Cook
Text • John

Solving a chess puzzle with Grok 4.5

I’ve written several posts about using Claude or ChatGPT to generate Prolog or Lean code to solve a chess puzzle. I didn’t think Grok would be up to the task, though I didn’t try it. I’ve heard good things about Grok 4.5, so I gave it a shot. It did great. Here’s the problem, a […]

The post Solving a chess puzzle with Grok 4.5 first appeared on John D. Cook.

John D. Cook
Text • John

Fitting a regular expression to a list of words

Suppose you want to search for a list of words. If you’re using grep, you can add the -f flag provide a file of regular expressions, and you can add the -F to tell it that the regular expressions are in fact just words. I did something like this a couple days ago when searching […]

The post Fitting a regular expression to a list of words first appeared on John D. Cook.

John D. Cook
Text • John

Sum of low squares

Squares, high and low Let p be an odd prime number. Then half the numbers from 1 through p − 1 are squares and half are not. That is, for half of numbers 1 ≤ k < p, the equation x² = k mod p has a solution. The traditional name for these numbers is […]

The post Sum of low squares first appeared on John D. Cook.

John D. Cook
Text • John

Visualizing Medical Code Hierarchy

Quick follow up to the previous two posts on ICD-10 codes and HCPCS codes. This post uses Python’s squarify library to create treemaps visualizing how many codes begin with each letter. Here’s the treemap for HCPCS codes. And here’s the treemap for ICD-10 codes. The sizes of the squares are proportional to the number of […]

The post Visualizing Medical Code Hierarchy first appeared on John D. Cook.

John D. Cook
Text • John

Regular expressions for HCPCS codes

Since I revisited my old post on ICD code matching, I thought I’d revisit by post on HCPCS codes too. HCPCS stands for Healthcare Common Procedure Coding System, and is pronounced “hick picks.” When most people say HCPCS, they technically mean HCPCS Level II, and that’s what I mean here. The format of a HCPCS […]

The post Regular expressions for HCPCS codes first appeared on John D. Cook.

John D. Cook
Text • John

Regular expression speed and error rates

Seven years ago I wrote a post about regular expressions to match diagnosis codes. I wanted to revisit that post looking at speed and error rates. Regular expressions usually do not exactly match what you’re looking for and nothing else. They have error false positives and false negatives. But they also have advantages, and context […]

The post Regular expression speed and error rates first appeared on John D. Cook.

John D. Cook
Text • John

ICD-10 chapters and code letters

I’ve been thinking about ICD-10 codes; they come up a lot in my work. The ICD-10-CM standard is divided into 21 chapters, which generally correspond to the first letter of a code. However, a chapter may contain blocks beginning with more than one letter, and codes starting with a single letter, namely D, can span […]

The post ICD-10 chapters and code letters first appeared on John D. Cook.

John D. Cook
Text • John

Posterior variance

A few days ago I wrote a post entitled Does additional data always reduce posterior variance?. In a nutshell, the answer is no, not always. That led the previous post which looked at posterior means for three Bayesian models, showing how the posterior mean is a weighted average of the prior mean and the mean […]

The post Posterior variance first appeared on John D. Cook.

John D. Cook
Text • John

Posterior mean

Common sense says that what you believe after seeing new data should be some sort of compromise between what you believed before and what the new data says. You don’t want to ignore previous information or new information. How much should new data change your prior beliefs? When prior judgment and new information are in […]

The post Posterior mean first appeared on John D. Cook.

John D. Cook
Text • John

Progress on Gilbreath’s conjecture

Years ago I wrote about Gilbreath’s conjecture. It’s a simple conjecture; you could explain it to anyone who understands what prime numbers are. See the linked post for a description of the problem. Gilbreath’s conjecture is simple, but it’s also kinda weird. As I wrote before, Paul Erdős speculated that Gilbreath’s conjecture is true but […]

The post Progress on Gilbreath’s conjecture first appeared on John D. Cook.

John D. Cook
Text • John

Reproducing a geometry theorem diagram

I ran across a geometry theorem with the following diagram. The theorem corresponding to the diagram is interesting, but I found reproducing the diagram more interesting. The segment AB is a diameter and the line CD is perpendicular to the diameter. Assume the outer circle is a unit circle. I guessed C = (cos(1), sin(1)) and made the […]

The post Reproducing a geometry theorem diagram first appeared on John D. Cook.

John D. Cook
Text • John

e approximation

I ran across the approximation e ≈ 2721/1001 recently. What makes this remarkable is its accuracy relative to the size of the denominator. You can create a trivial approximation just by truncating a decimal expansion e ≈ 2718/1000 but this is only good to four significant figures, but 2721/1001 is good to seven, almost eight, […]

The post e approximation first appeared on John D. Cook.

John D. Cook
Text • John

Does additional data always reduce posterior variance?

A discussion over lunch today brought up the fact that additional data does not always decrease the size of a confidence interval. This post will look at this from a Bayesian perspective. In general, new information reduces your uncertainty regarding whatever you’re estimating. The posterior distribution becomes more concentrated as more data are collected. That’s […]

The post Does additional data always reduce posterior variance? first appeared on John D. Cook.

John D. Cook
Text • John

DNA Sequence Alignment and Kings

This morning I wrote a post that included the central Delannoy numbers. The nth central Delannoy number Dn counts the number of ways a king can move from one corner of a chessboard to the diagonally opposite corner without backtracking. The more general Delannoy numbers Dm,n are the analogy for an m × n rectangular board, not […]

The post DNA Sequence Alignment and Kings first appeared on John D. Cook.