Last time on FAIC I showed how you could produce the `m`

th permutation of `n`

elements, where `m`

is a number between zero and `n!-1`

that chooses from the `n!`

possible permutations. There’s a very interesting way to represent numbers that are always less than `n!`

for a given `n`

that I thought I might talk about today.

# Monthly Archives: April 2013

# Producing permutations, part four

Last time on FAIC I implemented a short algorithm that produces a sequence containing every permutation of a given size, such that the sequence is a Hamiltonian tour of the vertices of the n-dimensional permutohedron. That is, the shape you get when you make each vertex have the coordinates of a permutation, and an edge connects two permutations that differ only by a swap of adjacent items.

Since we have an algorithm that produces the same order every time, we can number each ordering; it is convenient to do so starting with zero and going to `n!-1`

. What I’d like is an algorithm which given `size`

, the number of items in the permutation, and `index`

, an integer between zero and `size!-1`

, gives back the `index`

th permutation of length `size`

, *without *having to calculate all of them.

# Producing permutations, part three

Last time on FAIC I described an algorithm for producing every permutation of size n such that any two successive permutations differ only by swapping two adjacent items. Today let’s actually write some code to implement this algorithm.

# Producing permutations, part two

Last time on FAIC I described a simple recursive algorithm for generating all permutations of the first n whole numbers: if you have all the permutations of the first n-1 whole numbers, you just insert the nth whole number at each of n positions in each permutation. So if we had all the permutations of `{0, 1, 2}`

and we wanted all the permutations of `{0, 1, 2, 3}`

, we just start listing permutations of `{0, 1, 2}`

with the `3`

stuck in each of 4 places:

{3, 0, 1, 2}, {0, 3, 1, 2}, {0, 1, 3, 2}, {0, 1, 2, 3}, That does it for {0, 1, 2}... {3, 0, 2, 1}, {0, 3, 2, 1}, {0, 2, 3, 1}, {0, 2, 1, 3}, That does it for {0, 2, 1}...

and continue to get the 24 permutations.

# Producing permutations, part one

One of my more popular articles from 2010 was my post on how to produce the Cartesian product of arbitrarily many sequences of the same type. That is, we have some sequences, say `{10, 20, 30}`

and `{200, 300, 400}`

, and we wish to produce the sequence of all possible combinations:

{ {10, 200}, {10, 300}, {10, 400}, {20, 200}, {20, 300}, {20, 400}, {30, 200}, {30, 300}, {30, 400} }

A related question that I did not answer was *“how do you produce all possible permutations of a sequence?”* That’s the question we’re going to explore for the next few episodes.

# That’s a big transistor

As promised last time, a fun-for-Friday rerun from the early days of FAIC. But before we get started, a quick physics refresher.

**Force** is the ability to change the **velocity** of an object by a certain amount in a given amount of time. One **newton** (N) of force is the ability to change the velocity of a one kilogram object by one meter per second, in one second. The earth applies a force of gravity of 9.8N on every 1kg mass near it.

**Work** is the application of a force to an object as it moves a certain distance. **Energy** is the ability to do work. One **joule** (J) of energy is the ability to apply a force of one newton to an object as it moves one meter. [1. Note that the force has to be applied *in the direction of the motion* for it to count as work; the earth’s gravitational force does no work on a sideways-moving object. This should jibe with your intuitive understanding of work; it is a lot harder to raise an object by 1 meter than it is to slide it 1 meter, where the work is done by the force overcoming friction.]

**Power** is the rate at which energy is consumed in time. One **watt** of power is the consumption of one joule of energy per second.

**Charge** is, like mass, a fundamental property of matter. The easiest way to manipulate charge is by manipulating electrons. Charge is measured in coulombs (C). Current is the movement of charge and is measured in amperes (A); one ampere is one coulomb of charge moving past a given point in one second.

Electric potential, better known as **voltage**, is to charge as gravity is to mass, and is measured in volts. Applying a potential of one volt to one coulomb of moving charge consumes one joule of energy. A better way to think about it though is to divide both sides of that equation by time and get that one volt times one amp is one watt.[2. volts times amps equals watts is one of the funamental equations you have to know in order to wire a house safely. If your house power is providing a potential of 120V and your light bulb is consuming 120W of power then the current on the wire supplying the bulb is 1A. Since lighting circuits typically use wires that are safe for up to 15A, this puts a limitation on how many bulbs you can have on one circuit.]

**Resistance** is the tendancy of an electric conductor to resist the movement of charge, and is measured in ohms (Ω). If there is a conductor where the difference in voltage between the two ends is one volt, and the resistance is one ohm, then there will be a one amp current in the conductor.

A few years back a bunch of my coworkers and I got to discussing the space program over lunch. Someone asked why it is that we continue to launch devices into orbit by strapping a big old tank full of liquid oxygen to the device and then set it on fire. Why haven’t we developed better technology using magnets or something?

# Building a tabletop coilgun

As you probably know, I’ve been re-running some of my fun non-computer posts from the last decade. This Friday I’m going to rerun my post on the impracticalities of large-scale coilguns, and I thought that as a precursor to that I might talk a bit about tabletop coilguns. So, no programming language design this week.

A couple years ago my friend Morgan expressed an interest in learning about electronics so I thought that a homemade coilgun would of course be a perfect gift for a ten year old. Yes, I am that awesome avuncular figure who gets kids pocket knives and drum sets and coilguns for their birthdays. Parents, you’re welcome!

This is a great project to teach kids about circuits because it has all of the basic parts except transistors, and each part has a clear purpose. I’ve deliberately left the voltages, capacitances, resistances and inductances I chose off the circuit diagram above. Better to work them out for yourselves on the basis of what kinds of voltages sources and what capacitors you’ve got available, and what your appetite for destruction is.