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 2025-06-07 13:01
Martin’s doileys
An iteration due to artist Barry Martin produces intricate doiley-like patterns by iterating a simple mathematical function. I ran across this via [1]. The images produced are sensitive to small changes in the starting parameters x and y, as well as to the parameters a, b, and c. Here are three examples: And here’s the […]The post Martin’s doileys first appeared on John D. Cook.
Divisibility by any prime
Here is a general approach to determining whether a number is divisible by a prime. I’ll start with a couple examples before I state the general rule. This method is documented in [1]. First example: Is 2759 divisible by 31? Yes, because and 0 is divisible by 31. Is 75273 divisible by 61? No, because […]The post Divisibility by any prime first appeared on John D. Cook.
Finding pi in pi with Perl
Here’s a frivolous problem whose solution illustrates three features of Perl: Arbitrary precision floating point Lazy quantifiers in regular expressions Returning the positions of matched groups. Our problem is to look for the digits 3, 1, 4, and 1 in the decimal part of π. First, we get the first 100 digits of π after […]The post Finding pi in pi with Perl first appeared on John D. Cook.
Solving for neck length
A few days ago I wrote about my experiment with a wine bottle and a beer bottle. I blew across the empty bottles and measured the resulting pitch, then compared the result to the pitch you would get in theory if the bottle were a Helmholtz resonator. See the previous post for details. Tonight I […]The post Solving for neck length first appeared on John D. Cook.
Trig functions across programming languages
Programming languages are inconsistent in their support for trig functions, and inconsistent in the names they use for the functions they support. Several times I’ve been irritated by this and said that I should make a comparison chart someday, and today I finally did it. Here’s the chart. The C column also stands for languages […]The post Trig functions across programming languages first appeared on John D. Cook.
Da Vinci on wave propagation
From Leonardo da Vinci: The impetus is much quicker than the water, for it often happens that the wave flees the place of its creation, while the water does not; like the waves made in a field of grain by the wind, where we see the waves running across the field while the grain remains […]The post Da Vinci on wave propagation first appeared on John D. Cook.
Pitch of a big wine bottle
Yesterday my daughter came by and dropped off a huge blue wine bottle (empty). She had started removing the label, but as you can see she didn’t get very far yet. It’s an Incanto Chardonnay Pinot Grigio from Trader Joe’s. I blew across the top of the bottle to hear what sound it makes, and […]The post Pitch of a big wine bottle first appeared on John D. Cook.
More images from an oddball coordinate system
Three months ago I posted some images created by graphing equations in circular coordinates. Not polar coordinates as you might expect, but a strange coordinate system based on circles in another way. The most interesting thing about this coordinate system is that familiar functions product unfamiliar graphs. This morning I played around with the code […]The post More images from an oddball coordinate system first appeared on John D. Cook.
Fibonacci numbers and hyperbolic sine
Richard Askey came up with the following formula for Fibonacci numbers: Here φ is the golden ratio, (1 + √5)/2. We’ll use this formula as the jumping off point to discuss the implications of how equations are written, complex logarithms, and floating point computing in Python. Reading forward and backward Of course every equation of […]The post Fibonacci numbers and hyperbolic sine first appeared on John D. Cook.
Herd immunity on the back of an envelope
This post presents a back-of-the-envelope calculation regarding COVID herd immunity in the US. Every input to the calculation is only roughly known, and I’m going to make simplifying assumptions left and right. So take this all with a grain of salt. According to a recent article, about 26 million Americans have been vaccinated against COVID, […]The post Herd immunity on the back of an envelope first appeared on John D. Cook.
Pythagorean chaos
If a and b are two distinct positive integers, then |a²-b²|, 2ab, a²+b² is a Pythagorean triple. The result still gives the sides of a right triangle if the starting points aren’t integers In [1], Nick Lord looks at what happens if you iterate this procedure, using the output of one step as the input […]The post Pythagorean chaos first appeared on John D. Cook.
Python triple quote strings and regular expressions
There are several ways to quote strings in Python. Triple quotes let strings span multiple lines. Line breaks in your source file become line break characters in your string. A triple-quoted string in Python acts something like “here doc” in other languages. However, Python’s indentation rules complicate matters because the indentation becomes part of the […]The post Python triple quote strings and regular expressions first appeared on John D. Cook.
Mentally computing 3rd and 5th roots
A couple years ago I wrote about a trick for mentally computing the fifth root of an integer if you know that the number you’re starting with is the fifth power of a two-digit number. This morning I wrote up a similar (and simpler) trick for cube roots as a thread on @AlgebraFact. You can […]The post Mentally computing 3rd and 5th roots first appeared on John D. Cook.
How it started, how it’s going
This morning I created several mathematical versions of the popular “How it started, how it’s going” meme and posted them on Twitter. First, the Koch snowflake on @AlgebraFact: Then the logistic bifurcation on @AnalysisFact: Then cellular automaton “Rule 90” on @CompSciFact: And finally, the Lorenz system on @Diff_eq:The post How it started, how it’s going first appeared on John D. Cook.
Newton’s method spirals
In [1] the authors look at applying Newton’s root-finding method to the function f(z) = zp where p = a + bi. They show that if you start Newton’s method at z = 1, the kth iterate will be (1 – 1/p)k. This converges to 0 when a > 1/2, runs around in circles when […]The post Newton’s method spirals first appeared on John D. Cook.
Sums of consecutive reciprocals
The sum of the reciprocals of consecutive integers is never an integer. That is, for all positive integers m and n with n > m, the sum is never an integer. This was proved by József Kürschák in 1908. This means that the harmonic numbers defined by are never integers for n > 1. The […]The post Sums of consecutive reciprocals first appeared on John D. Cook.
Peaks of Sinc
Yesterday I wrote two posts about finding the peaks of the sinc function. Both focused on numerical methods, the first using a contraction mapping and the second using Newton’s method. This post will focus on the locations of the peaks rather than ways of computing them. The first post included this discussion of the peak […]The post Peaks of Sinc first appeared on John D. Cook.
Gell-Mann amnesia and its opposite
Michael Crichton coined the term Gell-Mann Amnesia effect to describe forgetting how unreliable a source is in one area when you trust it in another area. In Crichton’s words: Briefly stated, the Gell-Mann Amnesia effect is as follows. You open the newspaper to an article on some subject you know well. In Murray [Gell-Mann]’s case, […]The post Gell-Mann amnesia and its opposite first appeared on John D. Cook.
Rate of convergence for Newton’s method
In the previous post I looked at the problem of finding the peaks of the sinc function. In this post we use this problem to illustrate how two formulations of the same problem can behave very differently with Newton’s method. The previous post mentioned finding the peaks by solving either x cos x – sin […]The post Rate of convergence for Newton’s method first appeared on John D. Cook.
Reverse iteration root-finding
The sinc function is defined by sinc(x) = sin(x)/x. This function comes up constantly in signal processing. Here’s a plot. We would like to find the location of the function’s peaks. Let’s focus first on the first positive peak, the one that’s somewhere between 5 and 10. Once we can find that one, the rest […]The post Reverse iteration root-finding first appeared on John D. Cook.
Low-tech transparency
I recently received two large data files from a client, with names like foo.xlsx and foo.csv. Presumably these are redundant; the latter is probably an export of the former. I did a spot check and that seems to be the case. Then I had a bright idea: use pandas to make sure the two files […]The post Low-tech transparency first appeared on John D. Cook.
Zeros of trigonometric polynomials
A periodic function has at least as many real zeros as its lowest frequency Fourier component. In more detail, the Sturm-Hurwitz theorem says that has at least 2n zeros in the interval [0, 2π) if an and bn are not both zero. You could take N to be infinity if you’d like. Note that the […]The post Zeros of trigonometric polynomials first appeared on John D. Cook.
Just-in-case revisited
Just-in-time learning means learning something just when you need it. The alternative is just-in-case, learning something in case you need it. I discussed this in an earlier post, and today I’d like to add a little to that discussion. There are some things you need to know (or at least be familiar with) before you […]The post Just-in-case revisited first appeared on John D. Cook.
Data pathology
This post is an expansion of something I wrote on Twitter: Data scientists often complain that the bulk of their work is data cleaning. But if you see data cleaning as the work, not just an obstacle to the work, it can be interesting. You could think of it as data pathology, a kind of […]The post Data pathology first appeared on John D. Cook.
More stability, less stress
It’s been eight years since I started my consulting business. Two of the things I love about having my own business are the stability and the reduced stress. This may sound like a joke, but I’m completely serious. Having a business is ostensibly less stable and more stressful than having a salaried job, but at […]The post More stability, less stress first appeared on John D. Cook.
Quaternion square roots
If y is a quaternion, how many solutions does x² = y have? That is, does every quaternion have a square root, and if so, how many square roots does it have? A quaternion can have more than two roots. There is a example right in the definition of quaternions: i² = j² = k² […]The post Quaternion square roots first appeared on John D. Cook.
Bootstrapping a minimal math library
Sometimes you don’t have all the math functions available that you would like. For example, maybe you have a way to calculate natural logs but you would like to calculate a log base 10. The Unix utility bc is a prime example of this. It only includes six common math functions: sine cosine arctangent natural […]The post Bootstrapping a minimal math library first appeared on John D. Cook.
Average sum of digits
The smaller the base you write numbers in, the smaller their digits sums will be on average. This need not be true of any given number, only for numbers on average. For example, let n = 2021. In base 10, the sum of the digits is 5, but in base 2 the sum of the […]The post Average sum of digits first appeared on John D. Cook.
Random polynomials revisited
A few days ago I wrote about the expected number of roots in a random polynomial where each coefficient is drawn from a standard normal, i.e. a Gaussian distribution with mean 0 and variance 1. Another class of random polynomials, one that comes up in applications to physics, draws each coefficient from a different distribution. […]The post Random polynomials revisited first appeared on John D. Cook.
One infinity or two?
If you want to add ∞ to the real numbers, should you add one infinity or two? The answer depends on context. This post gives several examples each of when its appropriate to add one or two infinities. Two infinities: relativistic addition A couple days ago I wrote about relativistic addition, where the sum of […]The post One infinity or two? first appeared on John D. Cook.
Radio Frequency Bands
The radio spectrum is conventionally [1] divided into frequency bands that seem arbitrary at first glance. For example, VHF runs from 30 to 300 MHz. All the frequency band boundaries are 3 times a power of 10. Why all the 3’s? Two reasons: 3 is roughly the square root of 10, and the speed of […]The post Radio Frequency Bands first appeared on John D. Cook.
Most popular posts of 2020
These have been the most popular posts here this year. Pretending OOP never happened Worst tool for the job Banned math book Divisibility by 13 Memento ComplexitatisThe post Most popular posts of 2020 first appeared on John D. Cook.
Relativistic addition
Let c be a positive constant and define a new addition operation on numbers in the interval (-c, c) by This addition has several interesting properties. If x and y are small relative to c, then x ⊕ y is approximately x + y. But the closer x or y get to c the more […]The post Relativistic addition first appeared on John D. Cook.
Galileo’s polygon theorem
William J. Milne [1] attributes the following theorem to Galileo: The area of a circle is a mean proportional between the areas of any two similar polygons, one of which is circumscribed about the circle and the other is isoparametric with the circle. So imagine a polygon P and let S be an inscribed circle, that […]The post Galileo’s polygon theorem first appeared on John D. Cook.
Visualizing real roots of a high degree polynomial
The previous post looked at finding the expected number of real zeros of high degree polynomials. If you wanted to see how many real roots a particular high degree polynomial has, you run into several difficulties. If you use something like Descartes’ rule of signs, you’re likely to greatly over-estimate the number of roots. It […]The post Visualizing real roots of a high degree polynomial first appeared on John D. Cook.
Expected number of roots
Suppose you create a 100th degree polynomial by picking coefficients at random from a standard normal. How many real roots would you expect? There are 100 complex roots by the fundamental theorem of algebra, but how many would you expect to be real? A lot fewer than I would have expected. There’s a theorem due […]The post Expected number of roots first appeared on John D. Cook.
Large matrices rarely have saddlepoints
A matrix is said to have a saddlepoint if an element is the smallest element in its row and the largest element in its column. For example, 0.546 is a saddlepoint in the matrix below because it is the smallest element in the third row and the largest element in the first column. In [1], […]The post Large matrices rarely have saddlepoints first appeared on John D. Cook.
A generalization of sine and cosine
David Shelupsky [1] suggested a generalization of sine and cosine based on solutions to the system of differential equations with initial conditions αs(0) = 0 and βs(0) = 1. If s = 2, then α(t) = sin(t) and β(t) = cos(t). The differential equations above reduce to the familiar fact that the derivative of sine […]The post A generalization of sine and cosine first appeared on John D. Cook.
A connected topology for the integers
You can define a topology on the positive integers by choosing as an open basis sets the series of the form an + b where a and b are relatively prime positive integers. Solomon Golumb defines this topology in [1] and proves that it is connected. But that paper doesn’t prove that proposed basis really […]The post A connected topology for the integers first appeared on John D. Cook.
Accessing characters by name
You can sometimes make code more readable by using names for characters rather than the characters themselves or their code points. Python and Perl both, for example, let you refer to a character by using its standard Unicode name inside \N{}. For instance, \N{SNOWMAN} refers to Unicode character U+2603, shown at the top of the […]The post Accessing characters by name first appeared on John D. Cook.
Pythagorean dates
The numbers that make up today’s date—12, 16, and 20—form a Pythagorean triple. That is, 12² + 16² = 20². There won’t be any such dates next year. You could confirm this by brute force since there are only 365 days in 2021, but I hope to do something a little more interesting here. The […]The post Pythagorean dates first appeared on John D. Cook.
Shells, quoting, and one-liners
The goal this post is to show how to call Perl and Python one-liners from a shell. We want to be able to use bash on Linux, and cmd or PowerShell on Windows, ideally with the same code in all three shells. Bash Bash interprets text inside double quotes but not in single quotes. So […]The post Shells, quoting, and one-liners first appeared on John D. Cook.
Factors of consecutive products
Pick a positive integer k and take the product of k consecutive integers greater than k. Then the result is divisible by a prime number greater than k. This theorem was first proved 128 years ago [1]. For example, suppose we pick k = 5. If we take the product 20*21*22*23*24 then it’s obviously divisible […]The post Factors of consecutive products first appeared on John D. Cook.
Four meanings of ^
Logic vs C I recently made a mistake because I interpreted the symbol ^ in the wrong context. I saw an expression of the form a ^ b and thought of the ^ as AND, as in logic, but the author’s intent was XOR, as in C. How else might someone misinterpret the ^ operator? […]The post Four meanings of ^ first appeared on John D. Cook.
The Crown and The Planets
I first heard the hymn “I Vow to Thee, My Country” while watching the first season of The Crown [1]. I assume the hymn is familiar in the UK, but it is not in America as far as I know. When I say I first heard the hymn, I mean that I first heard it […]The post \The Crown and The Planets\ first appeared on \John D\. Cook\.
Predictive probability for large populations
Suppose you want to estimate the number of patients who respond to some treatment in a large population of size N and what you have is data on a small sample of size n. The usual way of going about this calculates the proportion of responses in the small sample, then pretends that this is […]The post Predictive probability for large populations first appeared on John D. Cook.
Asymptotic series for gamma function ratios
Applications of statistics often require working with for large x and fixed α and β. The simplest approximation to this ratio of gamma functions is You can do a little better [1] with where c = (α + β – 1)/2. To get more accuracy we’ll need more terms. Tricomi and Erdélyi worked out the […]The post Asymptotic series for gamma function ratios first appeared on John D. Cook.
Redundant Residue Number Systems
You can uniquely represent a large number by its remainders when divided by smaller numbers, provided the smaller numbers have no factors in common, and carry out arithmetic in this representation. Such a representation is called a Residue Number System (RNS). In the 1960’s people realized RNSs could be useful in computer arithmetic. The original […]The post Redundant Residue Number Systems first appeared on John D. Cook.
Ruling out gaps in the list of Mersenne primes
The largest known primes are Mersenne primes, in part because the Lucas-Lehmer algorithm makes it more efficient to test Mersenne numbers for primality than to test arbitrary numbers. These numbers have the form 2n – 1. The Great Internet Mersenne Prime Search (GIMPS) announced today that they have tested whether 2n – 1 is prime […]The post Ruling out gaps in the list of Mersenne primes first appeared on John D. Cook.
When are formal proofs worth the effort?
Formal verification of theorems takes a lot of work. And it takes more work where it is least needed. But the good news is that it takes less effort in contexts where it is needed most. Years of effort have gone into formally verifying the proofs of theorems that no one doubted were correct. For […]The post When are formal proofs worth the effort? first appeared on John D. Cook.
...26272829303132333435...