Feed john-d-cook John D. Cook

Favorite IconJohn D. Cook

Link https://www.johndcook.com/blog
Feed http://feeds.feedburner.com/TheEndeavour?format=xml
Updated 2026-07-24 09:33
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.
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.
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.
Locally everywhere does not imply everywhere
A couple days ago, Levent Alpoge, 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.
Volume to Area ratio for Regular Solids
The volume of a sphere of radius r is V = 4r^3 / 3 and the surface area is A = 4r^2 and so the ratio of volume to area is V / A = r / 3. Surprisingly, the same ratio holds for all regular solids ifr is the radius of the largest sphere [...]The post Volume to Area ratio for Regular Solids first appeared on John D. Cook.
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.
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.
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^2 = 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.
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.
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.
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.
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.
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.
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.
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 Erds speculated that Gilbreath's conjecture is true but [...]The post Progress on Gilbreath's conjecture first appeared on John D. Cook.
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 segmentAB is a diameter and the lineCD is perpendicular to the diameter. Assume the outer circle is a unit circle. I guessedC = (cos(1), sin(1)) and made the [...]The post Reproducing a geometry theorem diagram first appeared on John D. Cook.
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.
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.
DNA Sequence Alignment and Kings
This morning I wrote a post that included the central Delannoy numbers. Thenth central Delannoy numberDn 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.
Distinguishing variables from parameters
Imagine the following dialog. Professor:f is a function of a real variablex that takes a real parameterk. 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.
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 ^2 = 1 + and [...]The post Silver Rectangles and the Ways of Kings first appeared on John D. Cook.
Derivative equals inverse
Here's kind of a strange problem with an interesting solution: find a functionf such that the derivative off equals the inverse off 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.
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.
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.
When will the decimals in a/b repeat?
The previous post looked at how many digits are in the reduced fraction for thenth harmonic number. I was curious about how long the cycle of digits in a harmonic number might be. I wrote about the period length for the digits of fractions almost a decade ago. This post includes code so I can [...]The post When will the decimals in a/b repeat? first appeared on John D. Cook.
Height of harmonic numbers
The previous post looked at writing the harmonic numbers as reduced fractions and estimating the number of digits in the numerator and denominator based on asymptotics. This is a follow up post with plots. We'll choose our baseb to be 2. And we'll look at the total number of bits in both the numerator and [...]The post Height of harmonic numbers first appeared on John D. Cook.
Writing down harmonic numbers
Thenth harmonic number is the sum of the reciprocals of the firstn positive integers. Hn = 1 + 1/2 + 1/3 + 1/4 + ... + 1/n The product of all the denominators isn!, so you could write Hn as a fraction Hn = p/q wherep =n! Hn is an integer and q =n!. While [...]The post Writing down harmonic numbers first appeared on John D. Cook.
Hart’s theorem
Hart's theorem says If a triangle be formed by the arcs of three circles, the inscribed and the three escribed circles are all tangent to a new circle or line. Here triangle" means a three-sided figure whose sides are portions of a circle. The inscribed circle is the largest circle that can fit inside the [...]The post Hart's theorem first appeared on John D. Cook.
Incircles and Excircles of Pythagorean triangles
This post will reveal the connection between my two previous posts: one on the Star Trek lemma and one on Pythagorean triples. In the process of writing the latter, I looked at the Wikipedia article on Pythagorean triples and noticed this curious paragraph. In every Pythagorean triangle, the radius of theincircleand the radii of the [...]The post Incircles and Excircles of Pythagorean triangles first appeared on John D. Cook.
Consecutive Pythagorean triangle sides
In this post we find all Pythagorean triples that contain consecutive numbers, all Pythagorean triples (a,b,c) such thata + 1 =b orb + 1 = c. a + 1 =b George Osborne wrote a paper [1] addressing the question of when the squares of two consecutive numbers is also a square. Geometrically this is asking [...]The post Consecutive Pythagorean triangle sides first appeared on John D. Cook.
The Star Trek lemma
I was reading an article this evening and saw a footnote to a book by Arthur Baragar [1]. This caught my eye because he was my officemate at UT for a year. I found his book on Archive.org and was surprised to see The Star Trek Lemma" in the table of contents. What could this [...]The post The Star Trek lemma first appeared on John D. Cook.
Regular expressions that work “everywhere”
The most frustrating aspect of regular expressions is that implementations vary. Features supported in one tool may not be supported at all in another tool, or they may be supported with slightly different syntax. I learned regular expressions in the context Perl, a maximalist regex environment. This led to frustration when features I expect to [...]The post Regular expressions that work everywhere" first appeared on John D. Cook.
Lobachevsky’s integral formula
Let f be an even function with period . Then the following remarkable theorem by Lobachevsky holds. This theorem is useful in Fourier analysis and signal processing. It's useful to know even in the special case f(x) = 1. For a jinc" analog, see this paper. *** Every time I see the name Lobachevsky I [...]The post Lobachevsky's integral formula first appeared on John D. Cook.
Queens on a prime order board
Then queens problem is to place on ann *n chessboardn queens so that none attacks any other. This means there is only one queen on every horizontal, vertical, and diagonal line. Whenn is a prime number >= 5, it is sufficient to place the queens on a line that has slope 2, 3, 4, ..., [...]The post Queens on a prime order board first appeared on John D. Cook.
All pieces on a 6 by 5 board
I've written a couple posts lately on getting an LLM to generate code to solve chess problems. The first used Claude to generate Prolog and the second used ChatGPT to generate Prolog. This post will use Claude to generate Z3/Python code. The puzzle is one I've written about before: Place all the pieces-king, queen, two [...]The post All pieces on a 6 by 5 board first appeared on John D. Cook.
Formalizing a ring theorem with Lean 4 and Claude
I've been testing Claude's ability to generate Lean 4 code to prove theorems. I've written about a couple experiments that verified calculations. I did not write about my failed attempt to get Claude to formalize a proof of the pqr theorem for seminorms. This time I asked Claude to formally prove the theorem from the [...]The post Formalizing a ring theorem with Lean 4 and Claude first appeared on John D. Cook.
Partial fraction decomposition
Nearly everyone who has seen partial fraction decomposition was introduced to it as a way to compute integrals. IfP(x) andQ(x) are polynomials, then you can break their ratio P(x)/Q(x) into a sum of terms that can each be integrated in closed form. As with most topics in a calculus class, partial fractions go by in [...]The post Partial fraction decomposition first appeared on John D. Cook.
Three examples suffice
You can't prove a theorem by just checking a few examples. Except sometimes you can. A few weeks ago I wrote Pentagonal numbers are truncated triangular numbers. In a nutshell, if the pentagonal numbers are defined by Pn= (3n^2 -n)/2 and the triangular numbers by Tn= (n^2 +n)/2 then Pn=T2n- 1-Tn- 1. Here's a visualization [...]The post Three examples suffice first appeared on John D. Cook.
Testing pentagonal numbers
Thenth pentagonal number Pn is the number of dots in diagrams like those below with n concentric pentagons. We have the formula Pn = (3n^2 - n)/2 wheren is a positive integer. Ifn is an integer but not positive, the equation above defines ageneralized pentagonal number. If you're given ann, you can easily compute Pn. [...]The post Testing pentagonal numbers first appeared on John D. Cook.
Quaternion Rotations, Claude, and Lean
I got an email message this afternoon reporting a typo in a blog post from about a year ago on converting between quaternions and rotation matrices [1]. The email said exactly where the typo was, but I decided to see whether Claude would find it. Specifically, I prompted Sonnet 4.6 Medium with the following. Write [...]The post Quaternion Rotations, Claude, and Lean first appeared on John D. Cook.
Writing Prolog with ChatGPT
A few days ago I wrote about using Claude to solve a chess puzzle by writing Prolog code. This morning I tried a similar chess puzzle with ChatGPT. The task is to place a queen, king, rook, bishop, and knight on a 4 by 4 chessboard so no piece attacks another. Of course there's not [...]The post Writing Prolog with ChatGPT first appeared on John D. Cook.
RSA munitions T-shirt
Back when the US government classified strong encryption as munitions," RSA public key cryptography was illegal to export. In 1995, Adam Back protested this by creating a terse, obfuscated implementation of RSA in Perl code and used it as an email signature. The code was also printed on T-shirts. The shirt was classified as munitions [...]The post RSA munitions T-shirt first appeared on John D. Cook.
Solving a chess puzzle with Claude and Prolog
Prolog is the original logic programming language. The name comes from programming in logic. More specifically, the name comes from programmation en logique because the inventor of the language, Philippe Roussel, is French. Prolog has its advantages and disadvantages. One of the advantages is that the language represents logical problems directly. One of the disadvantages [...]The post Solving a chess puzzle with Claude and Prolog first appeared on John D. Cook.
Formally proving a calculation with Claude and Lean
I ran an experiment today to see whether Claude [1] could generate Lean code to prove a calculation at the bottom of this post, six lines of calculus. I started with this prompt This page contains a mathematical proof that a Fourier coefficient, a_n, is given in terms of a Bessel function. The LaTeX source [...]The post Formally proving a calculation with Claude and Lean first appeared on John D. Cook.
Pulling on a thread
Often there's a thread running through a sequence of my posts. Sometimes I make this explicit and sometimes I don't. The latest thread started with this post commenting on a tweet that observed that exp(-x^2) (1 + cos(sin(x) + x))/2. Some people said online that that the approximation is simply due to the first [...]The post Pulling on a thread first appeared on John D. Cook.
Aitken acceleration before Aitken
Kepler solved his eponymous equation M =E -e sin(E) by finding a fixed point of E = M + e sin(E). So guess a value ofE and stick it into the right hand side. Then plug that value into the right hand side again. Kepler said a couple iterations should be enough. And a couple [...]The post Aitken acceleration before Aitken first appeared on John D. Cook.
The Laplace limit
An earlier post discussed how to solve Kepler's equation M =E -e sin(E) using a sine series. You could also solve Kepler's equation using a power series, which Lagrange did in 1771. Both approaches expressE as a function ofe andM, but from different perspectives. Bessel thought of his solution as a sum of sines in [...]The post The Laplace limit first appeared on John D. Cook.
A crank formula for π
I ran across a cranky formula for based on physical constants here and decided to play around with it. The source describes as wavelength (chosen in the microwave region)" and I thought perhaps you could chose a value of to make the equation work. But as a comment pointed out, the bracketed [...]The post A crank formula for first appeared on John D. Cook.
From Kepler to Bessel
The previous post very briefly said that the integral representation for Bessel functions was motived by solving Kepler's equation. This post will go into more detail. Kepler's equation There are multiple ways to describe the position of a planet in an elliptical orbit around a star. For historical reasons, these descriptions have arcane names such [...]The post From Kepler to Bessel first appeared on John D. Cook.
Mr. Bessel’s eponymous functions
Yesterday I wrote a post showing that the trapezoid rule evaluates the integral very efficiently. But how do we know what the exact integral is for comparison? If you ask Mathematica, it will tell you the integral equals -2 J1(1) where J1 is a Bessel function. This may seem like rabbit out of a hat, [...]The post Mr. Bessel's eponymous functions first appeared on John D. Cook.
12345678910...