Marc Stevens gave an example of two alphanumeric strings that differ in only one byte that have the same MD5 hash value. It may seem like beating a dead horse to demonstrate weaknesses in MD5, bu...
https://www.johndcook.com/blog/2024/03/20/md5-hash-collision/
The key to solving a lot of elementary what-number-comes-next puzzles is to take first or second differences. For example, if asked what the next item in the series 14, 29, 50, 77, 110, … the a...
Fermat’s little theorem says that if p is a prime number, then for any positive integer b < p we hve bp-1 = 1 (mod p). This theorem gives a necessary but not sufficient condition for a number t...
https://www.johndcook.com/blog/2024/02/10/factoring-pseudoprimes/
I needed the inverse factorial function for my previous post. I was sure I’d written a post on computing the inverse factorial, and intended to reuse the code from that earlier post. But when I...
https://www.johndcook.com/blog/2024/01/01/inverse-factorial/
A Kepler triangle is a right triangle whose sides are in geometric progression. That is, if the sides have length a < b < c, then b/a = c/b = k. All Kepler triangles are similar because the propo...
The previous post describes the hoops I jumped through to enter Unicode characters on a Mac. Here’s a script to run from the command line that will copy Unicode characters to the system clipboa...
https://www.johndcook.com/blog/2023/09/28/cross-platform-unicode/
I was flipping through Gravitation this weekend and was curious about an illustration on page 309. This post reproduces that graph. The graph is centered at Cairo, Egypt and includes triangles w...
https://www.johndcook.com/blog/2023/08/27/curvature-at-cairo/
Given the equations for two circles, how can you tell whether they intersect? And if they do intersect, how do you find the point(s) of intersection? MathWorld gives a derivation, but I’d like ...
https://www.johndcook.com/blog/2023/08/27/intersect-circles/
Yesterday I wrote about how you could use the spaCy Python library to find proper nouns in a document. Now suppose you want to refine this and find proper nouns that are the subjects of sentences...
https://www.johndcook.com/blog/2023/07/18/dependency-labels/
Suppose you take the arithmetic mean and the geometric mean of the first n integers. The ratio of these two means converges to e/2 as n grows . In symbols, Now suppose we wanted to visualize the ...
Let φ be the golden ratio. The fractional parts of nφ bounce around in the unit interval in a sort of random way. Technically, the sequence is quasi-random. Quasi-random sequences are like rand...
https://www.johndcook.com/blog/2023/04/29/golden-integration/
I used Mathematica to create the graphics for my previous two posts because SciPy didn’t have the functions I needed. In particular, elliptic integrals and elliptic functions in SciPy only take...
https://www.johndcook.com/blog/2022/11/29/elliptic-functions-python/
Thanks to the quadratic equation, it’s easy to tell whether a quadratic equation has a double root. The equation has a double root if and only if the discriminant is zero. The discriminant of a...
https://www.johndcook.com/blog/2022/11/16/cubic-quartic-double-root/
This post is for someone unfamiliar with SciPy who wants to use Python to do basic statistical calculations. More specifically, this post will look at working with common families of random varia...
https://www.johndcook.com/blog/2022/10/15/python-as-stat-calculator/
There are 10 digits in ASCII, and I bet you can guess what they are. In ASCII, a digit is a decimal is a number. Things are much wilder in Unicode. There are hundreds of decimals, digits, and num...
ICD codes are diagnostic codes created by the WHO. (Three TLAs in just the opening paragraph!) The latest version, ICD-11, went into effect in January of this year. A few countries are using ICD-...
I had some errors in a recent blog post that might have been eliminated if I had programmatically generated the content of the post rather than writing it by hand. I rewrote the example in this p...
https://www.johndcook.com/blog/2022/09/05/literate-programming/
I’ve had to work a little with VIN numbers lately, and so I looked back at a post I wrote on the subject three years ago. That post goes into the details of Vehicle Identification Numbers and t...
https://www.johndcook.com/blog/2022/09/04/computing-vin-checksums/
I got a data file from a client recently in “pickle” format. I happen to know that pickle is a binary format for serializing Python objects, but trying to open a pickle file could be a puzzle...
With org-mode you can keep data, code, and documentation in one file. Suppose you have an org-mode file containing the following table. #+NAME: mydata | Drug | Patients | |------+----------| | X ...
The previous post discussed how to use org-mode as a notebook. You can have blocks of code and blocks of results, analogous to cells in a Jupyter notebook. The code and the results export as obvi...
You can think of org-mode as simply a kind of markdown, a plain text file that can be exported to fancier formats such as HTML or PDF. It’s a lot more than that, but that’s a reasonable place...
https://www.johndcook.com/blog/2022/08/02/org-babel-vs-jupyter/
The arithmetic-geometric mean (AGM) of two non-negative real numbers a and b is defined as the limit of the iteration starting with a0 = a and b0 = b and an+1 = ½ (an + bn) bn+1 = √(an bn) for...
Unicode provides a way to distinguish symbols that look alike but have different meanings. We can illustrate this with the following Python code. import unicodedata as u for pair in : for c in pa...
https://www.johndcook.com/blog/2022/06/16/letter-like-unicode-symbols/
Start with a list of ones: 1, 1, 1, 1, 1, … Taking the partial sums of this sequence gives consecutive numbers. That is, the nth number of the new series is the sum of the first n terms of the ...
https://www.johndcook.com/blog/2022/06/04/general-tetrahedral-numbers/
Suppose you have a standard deck of 52 cards. You pull out a card, put it back in the deck, shuffle, and pull out another card. How long would you expect to do this until you’ve seen every card...
https://www.johndcook.com/blog/2022/05/14/exhaustive-sampling/
I’ve seen an image similar to the following many times, but I don’t recall any source going into detail regarding how the spiral is constructed. This post will do just that. The previous post...
https://www.johndcook.com/blog/2022/05/09/logarithmic-spiral/
There are five ways to parenthesize a product of four things: ((ab)c)d (ab)(cd) (a(b(cd)) (a(bc))d (a((bc)d) In a context where multiplication is not associative, the five products above are not ...
Suppose you’re asked to write a function that takes a number and returns a planet. We’ll number the planets in order from the sun, starting at 1, and for our purposes Pluto is the 9th planet....
https://www.johndcook.com/blog/2021/09/18/planetary-code-golf/
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 ...
https://www.johndcook.com/blog/2021/04/12/spaceship-operator/