atlas news
    
John D.Cook : Python
21  mars     01h07
MD5 hash collision example
John    Marc Stevens gave an example of two alphanumeric strings that differ in only one byte that have the same MD hash value. It may seem like beating a dead horse to demonstrate weaknesses in MD , but it’s instructive to study the flaws of broken methods. And despite the fact that MD has been broken...
04  mars     18h55
Finite differences and Pascal’s triangle
John    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 , , , , , ... the answer or at lest the answer the person posing the question is most likely looking for is . You...
10  février     15h45
Factoring pseudoprimes
John    Fermat’s little theorem says that if p is a prime number, then for any positive integer b p we hve bp mod p . This theorem gives a necessary but not sufficient condition for a number to be prime. Fermat’s primality test The converse of Fermat’s little theorem is not always true, but ...
01  janvier     21h12
Computing inverse factorial
John    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 searched I couldn’t find anything, so I’m posting the code here for my future reference and for anyone...
20  novembre     11h55
Kepler triangle
John    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 proportionality constant k can only take on one value. To see this, we first pick our units The post...
28  septembre     23h24
Cross-platform way to enter Unicode characters
John    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 clipboard. It runs anywhere the Python module pyperclip runs. usr bin env python import sys import pyperclip...
27  août     21h47
Curvature at Cairo
John    I was flipping through Gravitation this weekend and was curious about an illustration on page . This post reproduces that graph. The graph is centered at Cairo, Egypt and includes triangles whose side lengths are the distances between cities. The triangles are calculated using only distances...
    20h02
Calculating the intersection of two circles
John    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 to add the derivation there in two ways. First, I’d like to be more explicit about the number of solutions...
18  juillet     19h04
Filtering on how words are being used
John    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 or proper nouns that are direct objects. This post was motivated by a project in which I needed to ...
23  mai     13h44
AM over GM
John    Suppose you take the arithmetic mean and the geometric mean of the first n integers. The ratio of these two means converges to e as n grows . In symbols, Now suppose we wanted to visualize the convergence by plotting the expression on the left side for a sequence of ns. First let’s let n ...
29  avril     14h56
Golden integration
John    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 random sequences but better in the sense that they explore a space more efficiently than random sequences....
30  novembre     01h15
Elliptic functions of a complex argument in Python
John    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 real valued arguments, but I needed to use complex arguments. Also, I needed theta functions, which are...
16  novembre     13h35
When a cubic or quartic has a double root
John    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 cubic is much less known, and the analogs for higher order polynomials are unheard of. There is a...
15  octobre     14h40
Using Python as a statistical calculator
John    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 variables normal, exponential, gamma, etc. and how to calculate probabilities with these. All the statistical...
07  octobre     12h33
Unicode numbers
John    There are 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 numeric characters, and they’re different sets. The following Python code loops through all possible...
06  octobre     21h39
Regex to match ICD-11 code
John    ICD codes are diagnostic codes created by the WHO. Three TLAs in just the opening paragraph The latest version, ICD , went into effect in January of this year. A few countries are using ICD now; it’s expected to be at least a couple years before the US moves from ICD to ICD . I still...
06  septembre     00h17
Literate programming to reduce errors
John    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 post in using org mode. My org file looked like this: begin src python :session :exports none lax lat ...
04  septembre     11h00
Computing VIN checksums
John    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 the quirky algorithm used to compute the check sum. This post captures the algorithm in Python code. See ...
16  août     18h40
Dump a pickle file to a readable text file
John    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 if you didn’t know this. Be careful There are a couple problems with using pickle files for data The...
15  août     13h24
Keeping data and code together with org-mode
John    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 Y Z Note that there cannot be a blank line between the The post Keeping...
03  août     13h05
Inline computed content in org-mode
John    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 obvious blocks when you export the org file to another format, such as LaTeX or HTML. And that’s fine for ...
02  août     14h49
Org-mode as a lightweight notebook
John    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 to start. Org mode also integrates with source code. You can embed code in your file and have The post...
30  juillet     13h44
Complex AGM
John    The arithmetic geometric mean AGM of two non negative real numbers a and b is defined as the limit of the iteration starting with a a and b b and an an bn bn s an bn for n . This sequence converges very quickly and is useful in numerical algorithms. The post Complex AGM...
16  juin     20h16
Letter-like Unicode symbols
John    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 ’K’, ’â ’ , ’Î ’, ’â ’ , ’â ’, ’ ’ : for c in pair: print format ord c , ’ X’ , u.bidirectional c , u.name c...
04  juin     18h55
General tetrahedral numbers
John    Start with a list of ones:,,,,, ... 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 previous series.,,,,, ... If we take partial sums again, The post General tetrahedral...
14  mai     16h23
Sampling with replacement until you’ve seen everything
John    Suppose you have a standard deck of 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 Here’s a variation on the same problem. Suppose you’re a park ranger keeping data on tagged The post...
09  mai     16h42
Logarithmic spiral
John    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 constructed iterated golden rectangles. We start with a golden rectangle and imagine chopping of first the...
01  février     13h31
Non-associative multiplication
John    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 necessarily the same. Maybe all five are different. This post will give two examples where the products...
18  septembre     11h02
Planetary code golf
John    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, and for our purposes Pluto is the th planet. Here’s an obvious solution: def planet n : planets Mercury , Venus , Earth , Mars , Jupiter , ...
12  avril     15h36
Spaceship operator in Python
John    Some programming languages, such as Perl, have an infix operator that returns a three state comparison. The expression a b evaluates to ,, or depending on whether a b, a b, or a b. You could think of as a concatenation of,, and . The operator The post Spaceship...