It doesn’t seem to be taught any more (neither of my daughters have ever heard of it), but casting out nines is a useful computational trick for verifying manual arithmetic calculations. Consider the sum below: 3074 6017 13814 1810 27 3611 ----- 28353 The sum of the digits of 3074 is 14, and the […]

It doesn’t seem to be taught any more (neither of my daughters have ever heard of it), but casting out nines is a useful computational trick for verifying manual arithmetic calculations. Consider the sum below:

3074 6017 13814 1810 27 3611 ----- 28353

The sum of the digits of 3074 is 14, and the sum of the digits of 14 is 5, so the result of casting out nines from 3074 is 5. The sum of the digits of 6017 is 14, and the sum of the digits of 14 is 5, so the result of casting out nines from 6107 is 5. The number 13814 includes digits 1 and 8, which sum to 9, so we can ignore them and sum the digits 3, 1 and 4, so the result of casting out nines from 13814 is 8. Likewise, the 1 and 8 of 1810 sum to 9, so we ignore them, and the result of casting nines out of 1810 is 1. The digits of the 27 sum to 9, which we cast out, so the result of casting nines out of 27 is 0. Finally, we can cast out the 3 and 6 from 3611, so the result of casting nines out of 3611 is 2. Now the nines results of the six numbers are 5, 5, 8, 1, 0 and 2, and again we cast out the 8 and 1, leaving 12. The sum of the digits of 12 is 3, which is the final result of casting out nines from the addends.

The sum of the digits of 28353 is 21, and the sum of the digits of 21 is 3, so the result of casting nines out of 28353 is 3. Since the addends and the sum have the same result when casting out nines, it is plausible that the sum is correct. If the process of casting out nines yielded different results from the addends and the sum, we could be sure that the sum was incorrect.

Although that formulation sounds complicated, in practice this goes faster than you can think. Consider 28353. The first two digits sum to 10, so cast out a nine and count 1. Then add 3, giving a running nine-sum of 4, and add 5, giving a running nine-sum of 9, which can be cast out. The only remaining digit is 3, which is the result of casting out nines from the sum.

Casting out nines from the addends is even easier, because there are more ways to make nine. From the first two addends, cast out the 3 and 6, then cast out the 7, 4, and 7, leaving 1. Adding the third number gives 0; cast out the 1 and 8 immediately, leaving the 1 carried from the first two numbers, plus 3, 1 and 4, which sums to 9, which is equivalent to 0. Cast out 1 and 8 from the fourth number, leaving 1, cast out the fifth number entirely, and cast out 3 and 6 from the sixth number, leaving the carry of 1 plus the two 1s at the end of the sixth number, for a total of 3.

With a little bit of practice, this becomes ridiculously fast. Your friends will be amazed when you quickly tell them their sum is wrong; they will also hate you.

Mathematically, casting out nines is the same as addition modulo 9. MathWorld gives a good explanation. The process of casting out nines works for multiplication and exponentiation as well as addition.

Your task is to write a program to cast out nines; avoid the temptation to simply take the remainder on division by 9 and do the actual work of summing the digits and casting out nines. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Today’s exercise is the kind of interview question that I don’t like. If you know the trick, you look like a genius, and if you don’t know the trick, you don’t get the job: In an unsorted array of integers, find the one integer that appears an odd number of times. You may use O(n) […]

Today’s exercise is the kind of interview question that I don’t like. If you know the trick, you look like a genius, and if you don’t know the trick, you don’t get the job:

In an unsorted array of integers, find the one integer that appears an odd number of times. You may use O(

n) time and O(1) space. For instance, in the array [4, 3, 6, 2, 6, 4, 2, 3, 4, 3, 3], the number 2 appears 2 times, the number 3 appears 4 times, the number 4 appears 3 times, and the number 6 appears 2 times, so the correct answer is 4.

Your task is to write a program to find the integer that appears an odd number of times. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Last week I was harshly schooled in the difference between theoretical physics and the reality of cell phone hardware. While it may be true that you can calculate distance from acceleration, the hardware on the LG G6 isn't precise enough to do so. But I remain undeterred!

I return instead, to my original and simpler problem: can I use my phone's sensors to determine many jumps have I done in a jump rope session? The accelerometer is clearly one way to approximate this. Just check out the graph below. It shows three bursts of me jumping, with each set of jumps followed by a pause:

The simplest strategy here is to count the peaks of the graph. Swapping out the code to calculate distance with code to do this counting was simple enough. All the code can be found here, but the relevant lines as follows:

;; Counting the peaks in a given accelerometer stream. This should ;; give us a rough approximation of jumps (define (count-peaks accum data) (let ((lower 0) (upper 10) (verbose? #t) (a-now (+ (data 1) (data 2) (data 3))) (rising? (accum 0)) (num-peaks (accum 1))) (cond ((and rising? (>= a-now upper)) (if verbose? (show "peak discovered: " a-now)) (list #f (+ 1 num-peaks))) ((and (not rising?) (<= a-now lower)) (list #t num-peaks)) (else (list rising? num-peaks)))))

For now, a peak is defined as having total acceleration over 10m/s^{2}. I arrived at this number the way all important mathematical constants are: I stood in my living room and jumped up and down. Set the peak height too low, and simple movements are marked as jump. Set it too high, and not all the jumps are counted.

A smarter approach would be to make this number adaptive, making it somehow dependent on the data set itself. But for now, a constant of 10 seems to work OK.

With this code change, I was able to record a 10 jump data file and run it through tinyscheme:

While this was functional, I was curious if I could streamline running this script. Switching to Termux, then entering the right filename in emacs, and then evaluating the code in the `*scheme*` buffer was all a bit much.

Fortunately, Termux has the answer: `Termux:Task`. This is an add on for Tasker that allows you to kick off shell scripts under Termux. The first order of business was to wrap up the scheme code as a shell script. That wasn't hard:

#!/data/data/com.termux/files/usr/bin/sh SRC_DIR=$HOME/dt/i2x/code/android-accelerometer-playtime/ MAIN_SCM=$SRC_DIR/main.scm usage () { echo "Usage: `basename $0` /path/to/data.csv" exit } if [ -f "$1" ] ; then exec tinyscheme -c "(define *src-dir* \"$SRC_DIR\") (load \"$MAIN_SCM\") (show (count-jumps \"$1\"))" else usage fi

`tinyscheme` can be invoked with `-c` which allows for running arbitrary scheme expressions. I'm using this to initialize and run parameterized code.

From there, I soft linked this script under `$HOME/.termux/tasker`. This allowed the script to be visible inside of Tasker:

Note the use of `%asfile1`. This variable is set by AutoShare, a Tasker plugin that lets you kick off actions based on the system sharing menu.

With the Tasker code in place, I can record data in the Physics Toolbox Sensor Suite app, then share that data with the `Count Jumps` AutoShare command. This kicks off the `count_jumps` shell script, which invokes `tinyscheme` and runs my code above. Simple, right? It may be a virtual Rube Goldberg, but it does work, and successfully demonstrates how you can get from an Android App to scheme code with very little effort.

As a final test, here's me knocking out 30 test jumps:

And there you have it, 31 jumps. As approximations go, I'll take it.

We looked at binary search in the previous exercise. Today we look at ternary search. Instead of one mid at the middle of the array, ternary search has two mids a third of the way from each end; two-thirds of the array is discarded at each recursive step. Your task is to write a program […]

We looked at binary search in the previous exercise. Today we look at ternary search. Instead of one `mid`

at the middle of the array, ternary search has two `mid`

s a third of the way from each end; two-thirds of the array is discarded at each recursive step.

Your task is to write a program that performs ternary search. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Most implementations of binary search assume that the target array has no duplicates. But sometimes there are duplicates, and in that case you want to find the first occurrence of an item, not just any one of several occurrences. For instance, in the array [1,2,2,3,4,4,4,4,6,6,6,6,6,6,7] the first occurrence of 4 is at element 4 (counting […]

Most implementations of binary search assume that the target array has no duplicates. But sometimes there are duplicates, and in that case you want to find the *first* occurrence of an item, not just any one of several occurrences. For instance, in the array [1,2,2,3,4,4,4,4,6,6,6,6,6,6,7] the first occurrence of 4 is at element 4 (counting from 0), the first occurrence of 6 is at element 8, and 5 does not appear in the array.

Your task is to write a binary search that finds the first occurrance of a set of duplicate items in a sorted array. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Regular readers of this blog know that Euclid’s algorithm, dating to 300 B.C., is the standard algorithm for computing the greatest common divisor of two numbers. But middle school teachers have their students calculate the greatest common divisor by factoring the two numbers and taking the product of the prime factors they have in common, […]

Regular readers of this blog know that Euclid’s algorithm, dating to 300 B.C., is the standard algorithm for computing the greatest common divisor of two numbers. But middle school teachers have their students calculate the greatest common divisor by factoring the two numbers and taking the product of the prime factors they have in common, being careful to count multiplicities correctly.

Your task is to write a program that calculates greatest common divisors by factoring. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

With the yearly Reproducible Build Summit starting today, now’s a good time for an update on what has happened in Guix land in that area!

Isolated build
environments
are very helpful to achieve reproducible
builds, but they are
not sufficient: timestamps and non-determinism can still make a package
build non-reproducible. Developers can rely on `guix build --check`

and `guix challenge`

to identify non-reproducible builds.

This article provides an overview of the progress made to fix non-reproducibility issues in packages over the year, and then goes on to show a very concrete way for Guix to take advantage of reproducible builds.

Tools that produce build artifacts occupy a key role: if their output is non-reproducible, then lots of packages that use them will be non-reproducible as a result. Among those packages, we fixed:

- GNU R (timestamps in
`.rds`

files and man pages; random temporary file names recorded in generated files); - GNU Guile (order-sensitive symbol generation during macro expansion);
- Ghostscript (timestamps and UUIDs in generated PDF files);
- GNU groff (timestamps in generated files);
- gdk-pixbuf (unsorted directory entries ending up in generated cache files);
- Perl build
system
(
`perllocal.pod`

files were produced in a non-deterministic way).

Sometimes we think that an issue is rare and we embark on a trip to fix
individual packages that are affected… until we realize that it’s common
enough to deserve a global, once-and-for-all fix. This is what happened
with timestamps in gzip headers: after blissfully assuming that “almost
everyone” uses the `-n`

flag of
gzip,
we finally
introduced
a build phase to automatically strip timestamps for gzip headers—this is
a subset of what Debian’s
`strip-nondeterminism`

achieves, but hey, Scheme integration matters to us!

There’s a number of well-identified issues left to be addressed: Python bytecode, GTK+ icon them caches, TeX Live, and more. Often, the issue database initiated by Debian is a great resource to find about issues and fixes.

We recently gained a new build farm called
`berlin.guixsd.org`

,
which is slated to replace our existing build farm at
`mirror.hydra.gnu.org`

. Having set it up as an independent build
farm—`berlin`

does not download binaries from `hydra`

—we can *challenge*
build reproducibility by comparing the binaries produced on each of
these build farms. Comparing the results of two independent build
farms, with different hardware and kernel versions, maximizes the
chances to catch all sorts of non-reproducibility issues. The result
with today’s master is… *drum rolls*

```
$ guix challenge $(guix package -A | cut -f1) \
--substitute-urls="https://mirror.hydra.gnu.org https://berlin.guixsd.org"
…
6,501 store items were analyzed:
- 5,048 (77.6%) were identical
- 533 (8.2%) differed
- 920 (14.2%) were inconclusive
```

We’re somewhere between 78% and 91%—not as good as Debian
yet, but we know what to do next!
The inconclusive comparisons here can be due to a package that failed to
build on one machine, for instance because its test suite fails in a
non-deterministic way, or simply because one of the build farms is
lagging behind. `guix challenge`

lists all the problematic packages,
which makes it easy to retrieve the faulty binaries and investigate.

There’s a very practical advantage to reproducible builds: anyone who
publishes
binaries is in essence a *mirror* of our build farm.

Until now, Guix’s public key infrastructure (PKI) was used pretty
rigidly: you could download binaries from a server *if and only if* you
had previously authorized its public
key.
So to download binaries from the person next to you, you would first
need to retrieve their public key and authorize it. In addition to
being inconvenient, it has the drawback of being an all-or-nothing
decision: you would now accept *any* binary coming from that person.
Can’t we do better?

We realized there’s an easy way to exploit the mirroring property
mentioned above: assuming I trust binaries from `mirror.hydra.gnu.org`

,
then I can download from anyone *who publishes the exact same binaries*.
Put this way, it seems obvious, but it required some adjustments to the
substitute code.

To understand what’s going on, let’s look at the metadata `guix publish`

produces, in a format inherited from Hydra:

```
$ wget -q -O - https://berlin.guixsd.org/8kib1cirdv0qbmn9hdkjzjfx3n5nw1yw.narinfo
StorePath: /gnu/store/8kib1cirdv0qbmn9hdkjzjfx3n5nw1yw-sed-4.4
URL: nar/gzip/8kib1cirdv0qbmn9hdkjzjfx3n5nw1yw-sed-4.4
Compression: gzip
NarHash: sha256:18v7dgny1xna7f53mbkj8bk4y2f00l5rjk2k6hj166kjv964lz7r
NarSize: 637360
References: 3x53yv4v144c9xp02rs64z7j597kkqax-gcc-5.4.0-lib 8kib1cirdv0qbmn9hdkjzjfx3n5nw1yw-sed-4.4 n6nvxlk2j8ysffjh3jphn1k5silnakh6-glibc-2.25
FileSize: 218663
System: x86_64-linux
Deriver: pi8686q63rwr4md90vm3qxwhk2g2fvqa-sed-4.4.drv
Signature: 1;berlin.guixsd.org;KHNpZ25hdHVyZSAKIChkYXRhIAogIChmbGFncyByZmM2OTc5KQogIChoYXNoIHNoYTI1NiAjQTRDRjUyMTVGNzlBOEUxRkFBNjIyOEQwQjk0QjMyMTZCRkY1RjA1NkQxMzZENUEzNTFGM0I2OTYzQzc1MzQzMiMpCiAgKQogKHNpZy12YWwgCiAgKGVjZHNhIAogICAociAjMDFDM0NGMEIzRUMwNkIwRUNGMTJEMTU4MkNCMzA2RjkzMEU2Njc1NDNFOEQ2NkZCRjhDRUY4QkQwMkMzOTg1NCMpCiAgIChzICMwRTg2MUEyRjI3MDg2MjVBRDkzMDg5RjFFRjE4NzUwQjIzQjM0RTA5MkFFRkQ3RTlFNkZCMjlCMkMwMURFNjI5IykKICAgKQogICkKIChwdWJsaWMta2V5IAogIChlY2MgCiAgIChjdXJ2ZSBFZDI1NTE5KQogICAocSAjOEQxNTZGMjk1RDI0QjBEOUE4NkZBNTc0MUE4NDBGRjJEMjRGNjBGN0I2QzQxMzQ4MTRBRDU1NjI1OTcxQjM5NCMpCiAgICkKICApCiApCg==
```

This “narinfo” gives us, among other things, the hash of the sed binary
that `berlin.guixsd.org`

obtained, the URL where it can be downloaded,
and a signature on this metadata (a base64-encoded canonical
s-expression).

Guix has supported the ability to specify several *substitute servers*
for a while, with `--substitute-urls`

, but it would filter out narinfos
signed by an unauthorized key. The main change was thus to keep
narinfos *with a hash identical to that advertised by one of the
authorized narinfos*. Thus, if I run:

```
$ guix build sed \
--substitute-urls="https://somebody.example.org https://mirror.hydra.gnu.org"
```

Guix will fetch a narinfo from both URLs. If `somebody`

’s narinfo
claims the same hash as `hydra`

, then Guix will download the actual
binary from `somebody`

—which, hopefully, may be faster than downloading
from `hydra`

. Of course, when the download completes, `guix-daemon`

verifies that the hash is really as advertised in the narinfo, such that
`somebody.example.org`

cannot tweak me into downloading a different
binary.

This feature landed in September, and will be in the forthcoming Guix release.

Among the ideas floating around, one is to
have `guix publish`

advertise itself on the local network *via* Avahi.
Guix could, optionally, discover neighboring `guix publish`

instances
and add them to its list of substitute servers. Binaries could
sometimes be downloaded from the local network, which should be faster.

More generally, the role of our build farm shifts from providing
binaries to providing *meta-data about binaries*. We can entirely
decouple the choice of a meta-data server from the choice of a binary
provider.

Longer-term, binaries could very well be downloaded from a content-addressed store such as IPFS or GNUnet without having to forego our existing infrastructure. Peer-to-peer distribution of binaries has been on our mind for a while, but we hadn’t quite realized this decoupling and how it would allow us to support a smooth transition.

These are all exciting perspectives, and a nice practical consequence of reproducible builds!

In an array A, the term inversions refers to the number of items that are out of order; each i < j for which A[j] < A[i] counts as a single inversion. For instance, the array [1 4 3 2 5] has three inversions: (4,3), (4,2) and (3,2). Inversions are a measure of the disorder […]

In an array *A*, the term *inversions* refers to the number of items that are out of order; each *i* < *j* for which *A*[*j*] < *A*[*i*] counts as a single inversion. For instance, the array [1 4 3 2 5] has three inversions: (4,3), (4,2) and (3,2). Inversions are a measure of the disorder of an array: an ordered array has no inversions and a reversed array has the maximum *n*(*n*−1)/2 number of inversions.

Your task is to write programs that count the number of inversions in an array and make an enumeration of them. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

The prime ant (A293689) is an obstinate animal that navigates the integers and divides them until there are only primes left, according to the following procedure: An infinite array A is initialized to contain all the integers greater than 1: [2, 3, 4, 5, 6, …]. Let p be the position of the ant on […]

The *prime ant* (A293689) is an obstinate animal that navigates the integers and divides them until there are only primes left, according to the following procedure:

An infinite array *A* is initialized to contain all the integers greater than 1: [2, 3, 4, 5, 6, …].

Let *p* be the position of the ant on the array. Initially *p* = 0, so the ant is at *A*[0] = 2.

At each turn, the ant moves as follows:

- If
*A*[*p*] is prime, move the ant one position forward, so*p*←*p*+ 1. - Otherwise (if
*A*[*p*] is composite), let*q*be its smallest divisor greater than 1. Replace*A*[*p*] with*A*[*p*] ÷*q*. Replace*A*[*p*−1] with*A*[*p*−1] +*q*. Move the ant one position backward, so*p*←*p*− 1.

Your task is to write a program that computes the position of the prime ant after *n* turns; for instance, after 47 turns the prime ant will be at *p* = 9. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

I continue today working through my backlog of homework problems: Given an array of at least three distinct positive integers sorted in ascending order, find all triples of array elements that form an arithmetic progression, so that the difference between the first and second elements is the same as the difference between the second and […]

I continue today working through my backlog of homework problems:

Given an array of at least three distinct positive integers sorted in ascending order, find all triples of array elements that form an arithmetic progression, so that the difference between the first and second elements is the same as the difference between the second and third elements. For instance, in the array (1 2 3 4 6 7 9) there are five triplets in arithmetic progressions: (1 2 3), (2 3 4), (2 4 6), (1 4 7), and 3 6 9).

Your task is to write a program that finds all the arithmetic progressions in an array; for extra credit, do the same thing for geometric progressions, where the ratios of the first to second element and the second to third elements are the same. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

We have another homework problem today: Replace each element of an array with the product of every other element of the array, without using the division operator. For instance, given array (5 3 4 2 6 8), the desired output is (1152 1920 1440 2880 960 720). Your task is to write a program to […]

We have another homework problem today:

Replace each element of an array with the product of every other element of the array, without using the division operator. For instance, given array (5 3 4 2 6 8), the desired output is (1152 1920 1440 2880 960 720).

Your task is to write a program to replace each element of an array with the product of every other element of the array, without performing division. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Guix will be present on a few venues in the coming weeks:

- On October 23rd, I (Ludovic Courtès) will be at
GPCE,
an academic conference co-located with SPLASH in Vancouver, Canada.
I will present the paper
*Code Staging in GNU Guix*, which discusses the motivation for and genesis of G-expressions, as well as recent improvements. It’s an honor to be presenting before an audience of experts in the field! - Christopher Baines will be at freenode #live in Bristol, UK, among well-known free software activists from a variety of organizations and projects. Christopher will give a talk on October 29th to give an overview of Guix and GuixSD.
- On October 31st, Ricardo Wurmus, Jan Nieuwenhuizen, and possibly more Guix hackers will join a dozen free software projects at the third Reproducible Build Summit in Berlin, Germany. As in previous years, we expect it to be a good time to share tips & tricks as well as a longer-term vision with our fellow hackers!

If you’re around in Vancouver, Bristol, or Berlin, let’s get in touch! :-)

GNU Guix is a transactional package manager for the GNU system. The Guix System Distribution or GuixSD is an advanced distribution of the GNU system that relies on GNU Guix and respects the user&aposs freedom.

In addition to standard package management features, Guix supports transactional upgrades and roll-backs, unprivileged package management, per-user profiles, and garbage collection. Guix uses low-level mechanisms from the Nix package manager, except that packages are defined as native Guile modules, using extensions to the Scheme language. GuixSD offers a declarative approach to operating system configuration management, and is highly customizable and hackable.

GuixSD can be used on an i686 or x86_64 machine. It is also possible to use Guix on top of an already installed GNU/Linux system, including on mips64el, armv7, and aarch64.

Zeros And Ones This is somebody’s homework problem: Given an array containing only zeros and ones, find the index of the zero that, if converted to one, will make the longest sequence of ones. For instance, given the array [1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1], replacing the zero at index 10 (counting from 0) forms a sequence of 9 ones. […]

Zeros And Ones

This is somebody’s homework problem:

Given an array containing only zeros and ones, find the index of the zero that, if converted to one, will make the longest sequence of ones. For instance, given the array [1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,0,0,1,1], replacing the zero at index 10 (counting from 0) forms a sequence of 9 ones.

Your task is to write a program that determines where to replace a zero with a one to make the maximum length subsequence. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

This interesting little question comes from Career Cup: Given an array that contains only the elements -1 and 1, find the number of sub-arrays with a sum of zero. For instance, given the array [-1, 1, -1, 1], there are four sub-arrays that sum to zero: [-1, 1], [1, -1], [-1, 1] and [-1, 1, […]

This interesting little question comes from Career Cup:

Given an array that contains only the elements -1 and 1, find the number of sub-arrays with a sum of zero. For instance, given the array [-1, 1, -1, 1], there are four sub-arrays that sum to zero: [-1, 1], [1, -1], [-1, 1] and [-1, 1, -1, 1].

Your task is to count the sub-arrays of a -1/1 array that sum to zero. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pat Ballew is a retired math teacher who writes a blog On This Day In Math that gives a day-by-day history of mathematics. The blog is odd, quirky, and unquestionably fun. On October 7th, Ballew wrote: The 280th day of the year…. The sum of the first 280 consecutive primes, mod 280, is prime. Since […]

Pat Ballew is a retired math teacher who writes a blog *On This Day In Math* that gives a day-by-day history of mathematics. The blog is odd, quirky, and unquestionably fun. On October 7th, Ballew wrote:

The 280th day of the year…. The sum of the first 280 consecutive primes, mod 280, is prime.

Since I like to play with prime numbers, that got my attention, and I quickly went to determine how many such days there are in a year.

Your task is to determine how many days in a year share the characteristic that on the *n*th day the sum of the first *n* primes, mod *n*, is prime. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.