https://www.johndcook.com/blog
130 posts · 8 Votes · 1 Followers
Science 52% · Tech 38% · Writing 5% · Culture 4% · Art 1% · Music 1%
Subscribe via RSS
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 x = 180π/(180 + π) = 3.08770208…. For that vale of x, sin(x) = sin(x°). In this article I’ll use the common convention of using radians by default and denoting degrees with ° as above. Note that x° =…
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 number could be 31, 41, or 59; the second could be either 26 or 53; the last could be 58, 97, 93, or 23. The following code enumerates all 3 × 2 × 4 = 24…
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 true or false? Claude’s response was The premise of this question isn’t quite right — no counterexample to the Jacobian conjecture has been…
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 that can be inscribed inside the regular solid. For example, if the edge of a cube is a, then r = a/2. The volume is 8r³, the area is 24r², and the ratio is r/3. The relationship between edge length and radius, and between radius and volume, is more complicated…
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 variation on the n-queens problem formulated by Martin Gardner. Can you place five white queens and three black queens on a 5 × 5 board so that no queen of one color attacks a queen of another color? Here’s the prompt I…
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 for diagnosis codes. grep -w -F -o -f icd10codes.txt notes.txt Now you might want to combine your list of words into a singular regular expression, for efficiency or possibly for some other reason. Apparently ripgrep…
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 “quadratic residues” but we can just say “squares” if the context is clear. So, for example, the numbers 1, 2, and 4 are squares mod 7, and the numbers 3, 5, and 6 are not. If k is a square mod p we will call is a low square if 0 ≤ k < p/2 and a high…
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 codes beginning with that letter. Note that they are not necessarily proportional to how often codes are used. The HCPCS map omits R and U because these are tiny relative to the…
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 code is simple: one letter and four digits. In regex terms, [A-Z]\d{4} Not all letters are used, so you can get more specific and say [A-CEGHJ-MP-V][0-9]\d{4} Some sources say no codes…
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 determines whether the advantages make the error rates tolerable. The post mentioned above gave the following regular expression for ICD-10 diagnosis codes.…
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 two chapters. Here’s a diagram I made to visualize the relationship between chapters and initial letters of codes. Notice there’s no letter U on the diagram. That’s because U is…
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 of the new data. The weights are precisions, which means something different for each model. For the beta-binomial model, variance may increase when seeing unexpected data (details here),…
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 conflict, which one should be given the benefit of the doubt? Bayesian data models provide a framework for making such decisions quantitative and objective. The choice of a data…
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 it would be 200 years before anyone could prove it. I find Erdős’s conjecture more interesting than Gilbreath’s conjecture. The conjecture is hard in a way…
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 following diagram. I guessed the value of C by eyeballing it, but in retrospect this would have been a convenient value for the creator of the original diagram to…
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, significant figures. e = 2.71828182… 2721/1001 = 2.71828171… The comparison is more impressive in binary. $ bc -l >>> obase=2 >>> 2721/1001 10.10110111111000010100… >>>…
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 what happens “in general” but does it necessarily happen every time you get new data? Conceivably if you get surprising data, data…
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 necessarily square. Dm,n is also the number of possible sequence alignments for a strand of DNA with m base pairs and a strand with n base pairs [1]. At each step in the…
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 ends as it does above, with the student confused. Here’s how I believe the conversation should continue. Professor: You’re absolutely right that f is a function…
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 so the golden ratio φ equals (1 + √5)/2. Silver rectangles A silver rectangle is one that if you stick two squares on its longer side you get another rectangle with the same aspect ratio. This tells us σ / 1 = (1 +…
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 unique solution is f(x) = φ(x / φ)φ where φ is the golden ratio. What an unexpected appearance of the golden ratio! The problem was proposed by H. L. Nelson and solved…
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 goes 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 takes two arguments. Is the first argument n or x? Grok said the function arguments are j(n,x). I thought I should run man bc just to make sure, and it said j(x, n) Returns the…
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 one-liner works. Bash brace…
The previous post looked at how many digits are in the reduced fraction for the nth 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 apply it to harmonic demoninators. from sympy import lcm, factorint, n_order def period(n): factors = factorint(n) exp2 = factors.get(2, 0) exp5 = factors.get(5, 0) r = max(exp2, exp5) d = n // (2**exp2 *…
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 base b to be 2. And we’ll look at the total number of bits in both the numerator and denominator, which we will use as the height of the fractions. First, let’s look at the actual and estimated heights, using the estimates from the previous post. Next let’s look at the…
The nth harmonic number is the sum of the reciprocals of the first n positive integers. Hn = 1 + 1/2 + 1/3 + 1/4 + … + 1/n The product of all the denominators is n!, so you could write Hn as a fraction Hn = p/q where p = n! Hn is an integer and q = n!. While p/q is a way to write Hn as a fraction, it’s not the most efficient because p and n! will have common factors. If we write Hn as a reduced fraction, the denominator will be the least common multiple of the integers 1 through n. That number…
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 three-sided figure. The “escribed” circles are analogous to the excircles in the previous post: you extend two sides and find a circle that is tangent to the triangle side and the extended…
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 the incircle and the radii of the three excircles are positive integers. Specifically, for a primitive triple the radius of the incircle is r = n(m − n), and the radii of the excircles opposite…
In this post we find all Pythagorean triples that contain consecutive numbers, all Pythagorean triples (a, b, c) such that a + 1 = b or b + 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 for primitive Pythagorean triples for which the legs are consecutive integers. He proved that the sequence shorter legs satisfies the recurrence relation with initial conditions u0 = 0 and u1…
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 be? It’s a theorem that goes back to Euclid that applies to an angle formed by connecting a point to two other points on a circle. The theorem says “The measure of an inscribed angle is half the measure of the arc it…