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 2024-11-22 13:31
Factors of orders of sporadic groups
I was curious about the orders of the sporadic groups, specifically their prime factors, so I made a bar graph to show how often each factor appears. To back up a little bit, simple groups are to groups roughly what prime numbers are to integers. Finite simple groups have been fully classified, and they fall […]The post Factors of orders of sporadic groups first appeared on John D. Cook.
Excessive, deficient, and perfect numbers
I learned recently that weekly excess deaths in the US have dipped into negative territory [1], and wondered whether we should start speaking of deficient deaths by analogy with excessive and deficient numbers. The ancient Greeks defined a number n to be excessive, deficient, or perfect according to whether the sum of the number’s proper […]The post Excessive, deficient, and perfect numbers first appeared on John D. Cook.
Is fast grep faster?
The grep utility searches text files for regular expressions, but it can search for ordinary strings since these strings are a special case of regular expressions. However, if your regular expressions are in fact simply text strings, fgrep may be much faster than grep. Or so I’ve heard. I did some benchmarks to see. Strictly […]The post Is fast grep faster? first appeared on John D. Cook.
Tower of powers and convergence
This post will look at the “tower of powers” and ask what it means, when it converges, and how to compute it. Along the way I’ll tie in two recent posts, including one that should come as a surprise. First of all, the expression is right-associative. That is, it is the limit of x^(x^(x^…)) and […]The post Tower of powers and convergence first appeared on John D. Cook.
Lambert W strikes again
I was studying a statistics paper the other day in which the author said to solve t log( 1 + n/t ) = k for t as part of an algorithm. Assume 0 < k < n. Is this well posed? First of all, can this equation be solved for t? Second, if there is […]The post Lambert W strikes again first appeared on John D. Cook.
Comparing files with very long lines
Suppose you need to compare two files with very long lines. Maybe each file is one line. The diff utility will show you which lines differ. But if the files are each one line, this tells you almost nothing. It confirms that there is a difference, but it doesn’t show you where the difference is. […]The post Comparing files with very long lines first appeared on John D. Cook.
New ICD-10 diagnosis code weirdness
In October 2020 there were 619 new codes added to the list of ICD-10 diagnosis codes. Some of these make sense, such as four codes added for COVID: U07.1 COVID-19 Z11.52 Encounter for screening for COVID-19 Z20.822 Contact with and (suspected) exposure to COVID-19 Z86.16 Personal history of COVID-19 There are also 24 new codes […]The post New ICD-10 diagnosis code weirdness first appeared on John D. Cook.
Magic square of squares
Allen William Johnson [1] discovered the following magic square whose entries are all squares. The following Python code verifies that this is a magic square. import numpy as np M = np.array( [[ 30**2, 246**2, 172**2, 45**2], [ 93**2, 116**2, 66**2, 258**2], [126**2, 138**2, 237**2, 44**2], [260**2, 3**2, 54**2, 150**2] ]) def verify(M): m, n […]The post Magic square of squares first appeared on John D. Cook.
Martian gravity
There is a lot of talk about Mars right now, and understandably so. The flight of Ingenuity today was awesome. As Daniel Oberhaus pointed out on Twitter, … the atmosphere on the surface of Mars is so thin that it’s the equivalent of flying at ~100k feet on Earth. No rotorcraft, piloted or uncrewed, has […]The post Martian gravity first appeared on John D. Cook.
Hashing phone numbers
A cryptographic hash is also known as a one-way function because given an input x, one can quickly compute the hash h(x), but it is extremely time-consuming to try to recover x if you only know h(x). Even if the hashing algorithm is considered “broken,” it may take an enormous effort to break it. Google […]The post Hashing phone numbers first appeared on John D. Cook.
Duodecimal vs Hexadecimal
I ran across the word duodecimal recently, the Latin-derived term for base 12, and realized it’s been years, maybe decades, since I’d heard that term. I’ve used hexadecimal explicitly in 30 different blog posts, and implicitly in more, but this is the first time I’ve used duodecimal. Hexadecimal comes up constantly in application. I suppose […]The post Duodecimal vs Hexadecimal first appeared on John D. Cook.
Floor, ceiling, bracket
Mathematics notation changes slowly over time, generally for the better. I can’t think of an instance that I think was a step backward. Gauss introduced the notation [x] for the greatest integer less than or equal to x in 1808. The notation was standard until relatively recently, though some authors used the same notation to […]The post Floor, ceiling, bracket first appeared on John D. Cook.
Box in ball in box in high dimension
Start with a unit circle and draw the largest square you can inside the circle and the smallest square you can outside the circle. In geometry lingo these are the inscribed and circumscribed squares. The circle fits inside the square better than the square fits inside the circle. That is, the ratio of the area […]The post Box in ball in box in high dimension first appeared on John D. Cook.
More on why simple approximations work
A few weeks ago I wrote several blog posts about very simple approximations that are surprisingly accurate. These approximations are valid over a limited range, but with range reduction they can be used over the full range of the functions. In this post I want to look again at and Padé approximation It turns out […]The post More on why simple approximations work first appeared on John D. Cook.
Mathematical stability vs numerical stability
Is 0 a stable fixed point of f(x) = 4x (1-x)? If you set x = 0 and compute f(x) you will get exactly 0. Apply f a thousand times and you’ll never get anything but zero. But this does not mean 0 is a stable attractor, and in fact it is not stable. It’s […]The post Mathematical stability vs numerical stability first appeared on John D. Cook.
Spaceship operator in Python
Some programming languages, such as Perl, have an infix operator <=> that returns a three-state comparison. The expression a <=> b evaluates to -1, 0, or 1 depending on whether a < b, a = b, or a > b. You could think of <=> as a concatenation of <, =, and >. The <=> operator […]The post Spaceship operator in Python first appeared on John D. Cook.
Real radical roots
The previous post Does chaos imply period 3? ended with looking at a couple cubic polynomials whose roots have period 3 under the mapping f(x) = 4x(1-x). These are 64 x³ – 112 x² + 56 x – 7 and 64 x³ – 96 x² + 36 x – 3. I ended the post by […]The post Real radical roots first appeared on John D. Cook.
Does chaos imply period 3?
Sharkovskii’s theorem says that if a continuous map f from an interval I to itself has a point with period 3, then it has a point with period 5. And if it has a point with period 5, then it has points with order 7, etc. The theorem has a chain of implications that all […]The post Does chaos imply period 3? first appeared on John D. Cook.
Sarkovsky’s theorem
The previous post explained what is meant by period three implies chaos. This post is a follow-on that looks at Sarkovsky’s theorem, which is mostly a generalization of that theorem, but not entirely [1]. First of all, Mr. Sarkovsky is variously known Sharkovsky, Sharkovskii, etc. As with many Slavic names, his name can be anglicized […]The post Sarkovsky’s theorem first appeared on John D. Cook.
Period three implies chaos
One of the most famous theorems in chaos theory, maybe the most famous, is that “period three implies chaos.” This compact statement comes from the title of a paper [1] by the same name. But what does it mean? This post will look at what the statement means, and the next post will look at […]The post Period three implies chaos first appeared on John D. Cook.
Better approximation for ln, still doable by hand
A while back I presented a very simple algorithm for computing natural logs: log(x) ≈ (2x – 2)(x + 1) for x between exp(-0.5) and exp(0.5). It’s accurate enough for quick mental estimates. I recently found an approximation by Ronald Doerfler that is a little more complicated but much more accurate: log(x) ≈ 6(x – […]The post Better approximation for ln, still doable by hand first appeared on John D. Cook.
Beta distribution with given mean and variance
It occurred to me recently that a problem I solved numerically years ago could be solved analytically, the problem of determining beta distribution parameters so that the distribution has a specified mean and variance. The calculation turns out to be fairly simple. Maybe someone has done it before. Problem statement The beta distribution has two […]The post Beta distribution with given mean and variance first appeared on John D. Cook.
Close but no cigar
The following equation is almost true. And by almost true, I mean correct to well over 200 decimal places. This sum comes from [1]. Here I will show why the two sides are very nearly equal and why they’re not exactly equal. Let’s explore the numerator of the sum with a little code. >>> from […]The post Close but no cigar first appeared on John D. Cook.
Arithmetic-geometric mean
The previous post made use of both the arithmetic and geometric means. It also showed how both of these means correspond to different points along a continuum of means. This post combines those ideas. Let a and b be two positive numbers. Then the arithmetic and geometric means are defined by A(a, b) = (a […]The post Arithmetic-geometric mean first appeared on John D. Cook.
Higher roots and r-means
The previous post looked at a simple method of finding square roots that amounts to a special case of Newton’s method, though it is much older than Newton’s method. We can extend Newton’s method to find cube roots and nth roots in general. And when we do, we begin to see a connection to r-means. […]The post Higher roots and r-means first appeared on John D. Cook.
Calculating square roots
Here’s a simple way to estimate the square root of a number x. Take a guess g at the root and compute the average of g and x/g. If you want to compute square roots mentally or with pencil and paper, how accurate can you get with this method? Could you, for example, get within […]The post Calculating square roots first appeared on John D. Cook.
Efficiently solving Kepler’s equation
A couple years ago I wrote a blog post on Kepler’s equation M + e sin E = E. Given mean anomaly M and eccentricity e, you want to solve for eccentric anomaly E. There is a simple way to solve this equation. Define f(E) = M + e sin E and take an initial guess at the solution and […]The post Efficiently solving Kepler’s equation first appeared on John D. Cook.
Unusually round exponential sum
The exponential sum page on this site draws lines between the consecutive partial sums of where m is the month, d is the day, and y is the last two digits of the year. The sum for today is unusually round: By contrast, the sum from yesterday is nowhere near round: Out of curiosity, I […]The post Unusually round exponential sum first appeared on John D. Cook.
Hypotenuse approximation
Ashley Kanter left a comment on Tuesday’s post Within one percent with an approximation I’d never seen. One that I find handy is the hypotenuse of a right-triangle with other sides a and b (where a<b) can be approximated to within 1% by 5(a+b)/7 when 1.04 ≤ b/a ≤1.50. That sounds crazy, but it’s right. […]The post Hypotenuse approximation first appeared on John D. Cook.
Coulomb’s constant
Richard Feynman said nearly everything is really interesting if you go into it deeply enough. In that spirit I’m going to dig into the units on Coulomb’s constant. This turns out to be an interesting rabbit trail. Coulomb’s law says that the force between two charged particles is proportional to the product of their charges […]The post Coulomb’s constant first appeared on John D. Cook.
Within one percent
This post looks at some common approximations and determines the range over which they have an error of less than 1 percent. So everywhere in this post “≈” means “with relative error less than 1%.” Whether 1% relative error is good enough completely depends on context. Constants The familiar approximations for π and e are […]The post Within one percent first appeared on John D. Cook.
Recursive grep
The regular expression search utility grep has a recursive switch -R, but it may not work like you’d expect. Suppose want to find the names of all .org files in your current directory and below that contain the text “cheese.” You have four files, two in the working directory and two below, that all contain […]The post Recursive grep first appeared on John D. Cook.
Calculating dogleg severity
Dogleg severity (DLS) is essentially what oilfield engineers call curvature. Oil wells are not simply vertical holes in the ground. The boreholes curve around underground, even if the intent is to drill a perfectly straight hole. With horizontal drilling the curvature can be substantial. Dogleg severity is calculated by measuring inclination and azimuth every 100 […]The post Calculating dogleg severity first appeared on John D. Cook.
Solving for Möbius transformation coefficients
Möbius transformations are functions of the form where ad – bc ≠ 0. A Möbius transformation is uniquely determined by its values at three points. Last year I wrote a post that mentioned how to determine the coefficients of a Möbius transformation. There I said The unique bilinear transform sending z1, z2, and z3 to […]The post Solving for Möbius transformation coefficients first appeared on John D. Cook.
Smallest denominator for given accuracy
The following table gives the best rational approximations to π, e, and φ (golden ratio) for a given accuracy goal. Here “best” means the fraction with the smallest denominator that meets the accuracy requirement. I found these fractions using Mathematica’s Convergents function. For any irrational number, the “convergents” of its continued fraction representation give a […]The post Smallest denominator for given accuracy first appeared on John D. Cook.
Simple approximation for Gamma function
I find simple approximations more interesting than highly accurate approximations. Highly accurate approximations can be interesting too, in a different way. Somebody has to write the code to compute special functions to many decimal places, and sometimes that person has been me. But somewhat ironically, these complicated approximations are better known than simple approximations. One […]The post Simple approximation for Gamma function first appeared on John D. Cook.
Mentally computing e^x
A few days ago I wrote about how to estimate 10x. This is an analogous post for exp(x) = ex. We will assume -0.5 ≤ x ≤ 0.5. You can bootstrap your way from there to other values of x. For example, exp(1.3) = exp(1 + 0.3) = e exp(0.3) and exp(0.8) = exp(1 – […]The post Mentally computing e^x first appeared on John D. Cook.
What makes the log10 trick work?
In my post on mentally calculating logarithms, I showed that log10 x ≈ (x – 1)/(x + 1) for 1/√10 ≤ x ≤ √10. You could convert this into an approximation for logs in any base by multiplying by the right scaling factor, but why does it work out so simply for base 10? Define […]The post What makes the log10 trick work? first appeared on John D. Cook.
Simple approximation for surface area of an ellipsoid
After writing the previous post, I wondered where else you might be able to use r-means to create accurate approximations. I thought maybe this would apply to the surface area of an ellipsoid, and a little searching around showed that Knud Thomsen thought of this in 2004. The general equation for the surface of an […]The post Simple approximation for surface area of an ellipsoid first appeared on John D. Cook.
Simple approximation for perimeter of an ellipse
The perimeter of an ellipse cannot be computed in closed form. That is, no finite combination of elementary functions will give you the exact value. But we will present a simple approximation that is remarkably accurate. So this post has two parts: exact calculation, and simple approximation. Exact perimeter The perimeter can be computed exactly […]The post Simple approximation for perimeter of an ellipse first appeared on John D. Cook.
Books and revealed preferences
Revealed preferences are the preferences we demonstrate by our actions. These may be different from our stated preferences. Even if we’re being candid, we may not be self-aware. One of the secrets to the success of Google’s PageRank algorithm is that it ranks based on revealed preferences: If someone links to a site, they’re implicitly […]The post Books and revealed preferences first appeared on John D. Cook.
Mentally calculating 10^x
This is the last in a sequence of three posts on mental calculation. The first looked at computing sine, cosine, and tangent in degrees. The second looked at computing logarithms, initially in base 10 but bootstrapping from there to other bases as well. In the previous post, we showed that log10 x ≈ (x-1)/(x+1) for […]The post Mentally calculating 10^x first appeared on John D. Cook.
Mentally calculating logarithms
The previous post looked at approximations for trig functions that are simple enough to compute without a calculator. I wondered whether I could come up with something similar for logarithms. I start with log base 10. Later in the post I show how to get to find logs in other bases from logs base 10. […]The post Mentally calculating logarithms first appeared on John D. Cook.
Simple trig function approximations
Anthony Robin gives three simple approximations for trig functions in degrees in [1]. The following plots show that these approximations are pretty good. It’s hard to distinguish the approximate and exact curves. The accuracy of the approximations is easier to see when we subtract off the exact values. The only problem is that the tangent […]The post Simple trig function approximations first appeared on John D. Cook.
Computing Fourier series coefficients with the FFT
The Discrete Fourier Transform (DFT) is a mathematical function, and the Fast Fourier Transform (FFT) is an algorithm for computing that function. Since the DFT is almost always computed via the FFT, the distinction between the two is sometimes lost. It is often not necessary to distinguish between the two. In my previous post, I […]The post Computing Fourier series coefficients with the FFT first appeared on John D. Cook.
Support of a signal and its FFT
The previous post looked at the Fourier uncertainty principle. This post looks at an analogous result for the discrete Fourier transform. The uncertainty principle for the (continuous) Fourier transform says a signal cannot be localized in both the time domain and the frequency domain. The more bunched up a function is, the more spread out […]The post Support of a signal and its FFT first appeared on John D. Cook.
Fourier uncertainty principle
Heisenberg’s uncertainty principle says there is a limit to how well you can know both the position and momentum of anything at the same time. The product of the uncertainties in the two quantities has a lower bound. There is a closely related principle in Fourier analysis that says a function and its Fourier transform […]The post Fourier uncertainty principle first appeared on John D. Cook.
Fourier transforms in Mathematica
Unfortunately there are many slightly different ways to define the Fourier transform. So the first two questions when using Mathematica (or any other software) to compute Fourier transforms are what definition of Fourier transform does it use, and what to do if you want to use a different definition. The answer to the first question […]The post Fourier transforms in Mathematica first appeared on John D. Cook.
Bessel determinants
The Bessel functions J and Y are analogous to sine and cosine. Bessel functions come up in polar coordinates the way sines and cosines come up in rectangular coordinates. There are J and Y functions of various orders, conventionally written with a subscript ν. I recently ran across a curious set of relations between these […]The post Bessel determinants first appeared on John D. Cook.
Computing normal probabilities with a simple calculator
If you need to calculate Φ(x), the CDF of a standard normal random variable, but don’t have Φ on your calculator, you can use the approximation [1] Φ(x) ≈ 0.5 + 0.5*tanh(0.8 x). If you don’t have a tanh function on your calculator, you can use tanh(0.8x) = (exp(1.6x) – 1) / (exp(1.6x) + 1). […]The post Computing normal probabilities with a simple calculator first appeared on John D. Cook.
...21222324252627282930...