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

John D. Cook
• John

Distinguishing variables from parameters

Imagine the following dialog. Professor: f is a function of a real variable x that takes a real parameter k. Student: What’s a parameter? Professor: It’s a constant that can vary. Student: Then if it can vary, isn’t it a variable? Professor: Sorta, but no not really. This conversation plays out over and over, and unfortunately it often […]

The post Distinguishing variables from parameters first appeared on John D. Cook.

John D. Cook
• John

Silver Rectangles and the Ways of Kings

Golden rectangles The defining property of golden rectangle is that if you stick a square on its longer side, you get another golden rectangle. The smaller vertical rectangle is similar to the larger horizontal rectangle. This means φ / 1 = (1 + φ) / φ which tells us φ² = 1 + φ and […]

The post Silver Rectangles and the Ways of Kings first appeared on John D. Cook.

John D. Cook
• John

Derivative equals inverse

Here’s kind of a strange problem with an interesting solution: find a function f such that the derivative of f equals the inverse of f for all positive x. f ′(x) = f−1(x) This is a differential equation, but a very unusual one, one that cannot be solved using any of the techniques taught in a class on differential equations. […]

The post Derivative equals inverse first appeared on John D. Cook.

John D. Cook
• John

Who you gonna believe: Grok or the docs?

The calculator utility bc has a minimal math library. For example, there’s no tangent function because you’re expected take the ratio of sine and cosine. (The Gnu version of bc does have a function for tangent, but the POSIX version does not.) And yet bc includes support for Bessel functions J(x). The bc function j […]

The post Who you gonna believe: Grok or the docs? first appeared on John D. Cook.

John D. Cook
• John

Brace expansion tree

Here’s a crazy bash one-liner I found via an article by Peter Krumins: echo {w,t,}h{e{n{,ce{,forth}},re{,in,fore,with{,al}}},ither,at} This prints 30 English words: when, whence, whenceforth, where, wherein, wherefore, wherewith, wherewithal, whither, what, then, thence, thenceforth, there, therein, therefore, therewith, therewithal, thither, that, hen, hence, henceforth, here, herein, herefore, herewith, herewithal, hither, hat This post will explain how […]

The post Brace expansion tree first appeared on John D. Cook.