Sign up

John D. Cook

Not verified No WebSub updates No webmention support Valid

Applied Mathematics Consulting

Public lists
davewiner/hackerNewsStars
Fetched

John D. Cook
Text • John

cos(200!)

In a footnote to the previous post, I said that Python’s math library can calculate the logarithm of extremely large numbers but not the cosine. This post will expand on that comment. In this post I’ll use n = 200! as my example rather than 1000! nbecause this value of N is larger than the […]

The post cos(200!) first appeared on John D. Cook.

John D. Cook
Text • John

Calculating log(1000!)

The previous post pointed out that the following code such as the following unexpectedly works. >>> from math import log, factorial >>> log(factorial(1000)) 5912.128178488163 If you don’t find this unexpected, note that if you replace math.log with numpy.log the code will fail [1]. Functions like natural logarithm operate on real numbers. Real numbers are represented […]

The post Calculating log(1000!) first appeared on John D. Cook.

John D. Cook
Text • John

The code that didn’t break

Last week I wrote a post on hiding cryptographic keys in decks of cards. I wrote some code for that post that shouldn’t work, but before fixing I noticed that it in fact did work. The code computes logarithms for integers larger than the largest representable float. For example, the largest float is on the […]

The post The code that didn’t break first appeared on John D. Cook.

John D. Cook
Text • John

Enumerating trees and circles

A few days ago I wrote a post on counting rooted trees. That post looked at the sequence c(n) which counts the number of rooted trees with n nodes. Here one node is distinguished as the root, but the nodes below the root are not distinguished from each other; all that matters is how the […]

The post Enumerating trees and circles first appeared on John D. Cook.

John D. Cook
Text • John

Metallic alchemy

After writing the previous post about metallic ratios, I thought about the analogy to alchemy and the attempt to make precious metals out of base metals. When can you make one metallic ratio out of another? Can you make the golden ratio out of the lead ratio? Before we can make gold out of lead, […]

The post Metallic alchemy first appeared on John D. Cook.

John D. Cook
Text • John

Ratio of metallic ratios

The golden ratio is the first and best known of the metallic ratios. I’ve written about the silver ratio a few times, most recently here. And I’ve mentioned the bronze ratio a couple times. The metallic ratios after bronze don’t have standard names. The nth metallic ratio M(n) is the number whose continued fraction representation […]

The post Ratio of metallic ratios first appeared on John D. Cook.

John D. Cook Valid
Text • John

Holonomic functions

Yesterday I wrote that a lot of the special functions that pop up in mathematical physics are solutions to second order linear differential equations with polynomial coefficients. More generally, holonomic functions are defined to be those functions that are the solutions to linear differential equations, of any order, with polynomial coefficients. Most special functions are […]

The post Holonomic functions first appeared on John D. Cook.

John D. Cook Valid
Text • John

Estimating a cumulative sum

In this post I mentioned two series which I denoted t(n) and c(n). The former is the number of unlabeled rooted trees with n nodes. The latter is the cumulative sum of the former, i.e. The sequence c(n) is also the number of constrains on an n-step Runge-Kutta method; that’s how I became interested in it. Now the t(n) sequence has […]

The post Estimating a cumulative sum first appeared on John D. Cook.

John D. Cook Valid
Text • John

Why polynomial coefficients?

Second order linear differential equations with polynomial coefficients form their own area of study. This seems like a narrow class of equations, but it’s very important in applications. This class of equations seems like a mathematically natural topic, but why is it so important in applications? I did a PhD in differential equations without ever […]

The post Why polynomial coefficients? first appeared on John D. Cook.

John D. Cook Valid
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 Valid
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 Valid
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 Valid
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 Valid
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 Valid
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 Valid
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 Valid
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 Valid
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 Valid
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.