## FSM Generator

### July 11, 2014

A few weeks ago we had an exercise that solved the problem of removing singleton occurrences of a given character from a string while leaving multiple occurrences of the given character intact. Our solution used a finite state machine that iterated over the input string writing output as it went.

The finite state machine used in that solution was hand-crafted to solve the given problem. But it is possible to write a program that takes a definition of a finite state machine and creates a function that takes an input string and applies the finite state machine to it. Such a program isn’t hard to create and provides a useful utility for small parsing problems.

Your task is to write a program that generates a finite state machine from a simple description; the format and capabilities of the machine are up to you, but it should be at least powerful enough to solve the “remove singleton” problem. 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.

## SQUFOF, Continued Fraction Version

### July 8, 2014

Square Forms Factorization, abbreviated SQUFOF, is a special-purpose method for factoring integers. It is commonly used to split small semi-primes in the double-large-prime variants of the quadratic sieve and number field sieve factoring algorithms. We studied the “binary quadratic forms” variant of SQUFOF in a previous exercise; today we study the “continued fraction” variant of SQUFOF.

Although the mathematical basis of SQUFOF is the quadratic forms of Gauss, SQUFOF was discovered by Daniel Shanks while he was investigating the failures of the continued fraction factoring algorithm; you should read his paper, which is as good a demonstration of the process of mathematical discovery as anything I have ever read. Shanks’ algorithm expands the continued fraction of the square root of *n*, the number being factored, searching among the convergents for a quadratic form that is a perfect square; in this, it differs from the CFRAC algorithm, which combines multiple convergents to assemble a perfect square. We copy the algorithm from the paper by Jason Gower and Sam Wagstaff that provides the standard description of SQUFOF:

[ Note: In the description that follows, be careful to distinguish the symbols

Q̂andQ; the first one has a circumflex over theQ, the second one doesn't. Some browsers render the "combining circumflex accent" readably, but some don't. On the browsers available to me, only one out of four rendered the symbol readably, sorta kinda, and only when I expanded the font to be quite large. If in doubt, look at the program on the next page, where the first symbol is named`qhat`

and the second symbol is named`q`

. ]The variable

Nis the integer to factor,Sremembersq_{0},qholds the currentq,_{i}PandP’hold two consecutive values ofP,_{i}Q̂andQhold two consecutive values ofQ, and_{i}tis a temporary variable used in updatingQ. Finally,Bis an upper bound on the number of forms tested for being square forms before the algorithm gives up.

1. Initialize:Read the odd positive integerNto be factored. IfNis the square of an integer, output the square root and stop. IfN≡ 1 mod 4, then setD← 2N; otherwise, setD←N. In any case, setS← ⌊√D⌋,Q̂ ← 1,P←S,Q←D−P·P,L← ⌊2√2√D⌋,B← 2 ·L, andi← 0.

2. Cycle forward to find a proper square form:Steps 2a through 2e are repeated fori= 1, 2, 3, ….

2a:Setq← ⌊(S+P) /Q⌋ andP’←q·Q−P.

2b:IfQ≤l, then:

IfQis even, put the pair (Q/ 2,Pmod (Q/ 2) onto the QUEUE; otherwise, ifQ≤L/ 2, then put the pair (Q,PmodQ) onto the QUEUE.

2c:Sett←Q̂ +q· (P−P’),Q̂ ←t, andP←P’.

2d:Ifiis odd, go to Step 2e. IfQis not the square of an integer, go to Step 2e. Otherwise, setr← √Q, a positive integer. If there is no pair (r,t) in the QUEUE for whichrdividesP−t, then go to Step 3. Ifr> 1 and there is a pair (r,t) in the QUEUE for whichrdividesP−t, then remove all pairs from the beginning of the QUEUE up to and including this pair and go to Step 2e. Ifr= 1 and there is a pair (1,t) in the QUEUE, then the algorithm fails because we have traversed the entire principal period of quadratic forms of discriminant 4Nwithout finding a proper square form.

2e:Leti←i+ 1. Ifi>B, give up. Otherwise, go to Step 2a.

3. Compute an inverse square root of the square form:SetQ̂ ←r,P←P+r· ⌊(S−P) /r⌋, andQ← (D−P·P) /Q̂.

4. Cycle in the reverse direction to find a factor ofN:

4a:Setq← ⌊(S+P) /Q⌋ andP’←q·Q−P.

4b:IfP=P’, then go to Step 5.

4c:Sett←Q̂ +q· (P−P’,Q̂ ←Q,Q←t, andP←P’and go to Step 4a.

5. Print the factor ofIfN:Qis even, setQ←Q/ 2. Output the factorQofN.

Later, in Section 5.2 of their paper, Gowers and Wagstaff give instructions for using a multiplier *m* to help find a factor of *N*, changing the algorithm given above as follows:

Change Step 1 to this: Read the odd positive integer

Nto be factored. IfNis the square of an integer, output the square root and stop. IfmN≡ 1 mod 4, then setD← 2mN; otherwise, setD←mN. In any case, setS← ⌊√D⌋,Q̂ ← 1,P←S,Q←D−P·P,L← ⌊2√2√D⌋,B← 2 ·L, andi← 0.Change Step 2b to: Let

g←Q/ gcd(Q, 2m). Ifg≤L, put (g,Pmodg) onto the QUEUE. [ Note: If you're reading the linked version of the Gowers and Wagstaff paper, this formula is erroneously given as (g,Bmodg); that error cost me several hours of pain. ]In Step 5, replace

QbyQ/ gcd(Q, 2m) before the factorQis output.

To find a factor, first use multiplier *m* = 1. If that fails, try another multiplier from the set 3, 5, 7, 11, 3 · 5 = 15, 3 · 7 = 21, 3 · 11 = 33, 5 · 7 = 35, 5 · 11 = 55, 7 · 11 = 77, 3 · 5 · 7 = 105, 3 · 5 · 11 = 165, 3 · 7 · 11 = 231, 5 · 7 · 11 = 385, 3 · 5 · 7 · 11 = 1155, or any other squarefree product of small odd primes.

Your task is to implement SQUFOF with multipliers. 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.

## Minimum And Maximum

### July 4, 2014

We have today a two-part interview question:

First, write a program that takes an array of integers and returns the minimum and maximum elements of the array. Use as few comparisons as possible.

Second, write a program that takes an array of integers and returns the second minimum and maximum elements of the array; that is, the second-smallest element and the second-largest element. Again, use as few comparisons as possible.

Your task is to write the two programs described above, and state the number of comparisons each makes in the general case; be sure they are proof against malicious input. 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.

## Moonrise, Moonset

### July 1, 2014

We have previously studied the times of sunrise and sunset and the phases of the moon. Today we look at the times of moonrise and moonset.

The standard source for astronomical calculations is the Naval Observatory; they point to a 1989 article in *Sky & Telescope* for the calculation. The article isn’t online, but the code is: a horribly ugly BASIC program, reproduced on the next page.

Your task is to write a program that calculates the times of moonrise and moonset. 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.

## Raindrops

### June 27, 2014

We’re going to try something different today. We often have interview questions here, but always of the type that require writing a program. Today, we will have one of the brain-teaser type of interview question:

How many raindrops fall on the planet every year?

Your task is to estimate the answer to the question posed above. When you are finished, you are welcome to read a suggested solution or to discuss your solution in the comments below.

## Busy Beaver

### June 24, 2014

Yesterday was Alan Turing’s birthday, so today we will write a program for a turing machine.

The Busy Beaver is a turing machine that performs the maximum work for a given configuration of machine; the concept was invented by Tibor Radó in his 1962 paper *On Non-Computable Functions*. We won’t look at the underlying theory, although it is fascinating if you have the time. Instead, we’ll be content to implement the first few busy beavers. Here are the two-symbol busy beavers for one, two, three and four states, from Wikipedia; the two symbols are 0 and 1, the states are letters A through D plus the halting state H, and the moves L and R are left and right:

A | |

0 | 1RH |

1 | unused |

A | B | |

0 | 1RB | 1LA |

1 | 1LB | 1RH |

A | B | C | |

0 | 1RB | 0RC | 1LC |

1 | 1RH | 1RB | 1LA |

A | B | C | D | |

0 | 1RB | 1LA | 1RH | 1RD |

1 | 1LB | 0RC | 1LD | 0RA |

Your task is to implement the four busy beavers. 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.

## Birthday Paradox

### June 20, 2014

The birthday paradox, which we studied in a previous exercise, states that in any group of 23 people there is a 50% chance that two of them share a birthday. The BBC recently published an article that shows 16 of the 32 World Cup teams, each consisting of 23 players, have shared birthdays, thus demonstrating the paradox precisely. Today’s exercise asks you to recreate their calculation.

You can obtain the same listing of player birthdays that the BBC used from FIFA. Another source is the player rosters at WikiPedia.

Your task is to demonstrate that the 2014 World Cup rosters honor the birthday paradox. 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.

## Collinearity

### June 17, 2014

Beware: today’s exercise sounds simple but is actually quite complex if you don’t look at it properly.

Your task is to write a function that takes three points in the *x*,*y* plane and determines if they are collinear; be sure to handle vertical lines and horizontal lines properly. 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.

## Minimax Pandigital Factor

### June 13, 2014

Over at /r/math, Darksteve writes of “A problem I came up with, and haven’t been able to solve for many years:”

I would like to present a mathematical problem that I came up with some years ago, and haven’t yet found a solution for:

If you take all the numbers that contain the digits 1 to 9 exactly once, and you write down the prime factorization of all those numbers, which one has the smallest biggest prime factor?

To illustrate what I mean, the number 879456123 contains the prime factors 3 7 13 and 491; making 491 this numbers biggest prime factor.

The number 213456789 contains 3 7 13 and 197 as factors, making 197 the biggest prime factor. Out of all the numbers I’ve tried, 213456789 has the smallest biggest prime factor.

Many other number have much bigger biggest prime factors, like 576492813 which contains 3 13 19 and 13649.

But I have not found a way to actually solve this problem, as I am lacking the programming skills, or the algebraic knowledge required. I would therefore greatly appreciate anyone capable of solving this.

Your task is to solve Darksteve’s problem. 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.

## Balanced Delimiters

### June 10, 2014

I heard today’s exercise as an interview question — you have five minutes to solve this task, while I watch — but it could equally be a homework problem:

Write a function to return true/false after looking at a string. Examples of strings that pass:

`{}, [], (), a(b)c, abc[d], a(b)c{d[e]}`

Examples of strings that don’t pass:

`{], (], a(b]c, abc[d}, a(b)c{d[e}]`

Your task is to write the function described above. 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: