## Gaussian Integers, Part 2

### November 7, 2014

We continue today with our study of Gaussian integers.

The greatest common divisor of two Gaussian integers is computed by Euclid’s algorithm: repeatedly replace dividend and divisor with divisor and remainder until the divisor is zero, at which point the dividend is the greatest common divisor. One Gaussian integer divides another if the remainder is zero, and two Gaussian integers are co-prime if their greatest common divisor is one of the four units.

If a Gaussian integer is not a unit, it is either prime or composite. A Gaussian integer *a* + *b i* is prime if either *a* or *b* is zero and the other is a prime congruent to 3 (modulo 4), or if both *a* and *b* are non-zero and the norm is prime.

If a Gaussian integer is composite, it can be factored by the following algorithm described by Jim Lewis: Factor the norm. For each norm factor of 2, add a factor 1 + *i* to the list of output factors. For each two norm factors *p* that is congruent to 3 (modulo 4), add a factor *p* + 0 *i* to the list of output factors. Otherwise, norm factor *p* is congruent to 1 (modulo 4): find *k* such that *k* ^{2} = −1 (mod *p*), then add gcd(*p*, *k* + *i*) to the list of output factors. After considering all the norm factors, the original Gaussian integer divided by all the output factors will be a unit; if it is 1, then you are finished, otherwise add to the list of output factors the unit that remains after the division. To find *k*, choose some integer *n* between 2 and *p* −1, inclusive; if *n* ^{(p −1)/2} = −1, which will be true about half the time, then *k* = *n* ^{(p −1)/4}.

Your task is to write functions for greatest common divisor, identifying primes, and factoring composites for Gaussian integers. 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.

## Gaussian Integers, Part 1

### November 4, 2014

Gaussian integers are complex numbers of the form *a* + *b i* where both *a* and *b* are integers. They obey the normal laws of algebra for addition, subtraction and multiplication. Division works, too, but is a little bit complicated:

Addition: (*a* + *b i*) + (*x* + *y i*) = (*a* + *x*) + (*b* + *y*) *i*.

Subtraction: add the negative: (*a* + *b i*) − (*x* + *y i*) = (*a* − *x*) + (*b* − *y*) *i*.

Multiplication: cross-multiply, with *i* ^{2} = −1: (*a* + * b i*) × (*x* + *y i*) = (*a x* − *b y*) + (*a y* + *b x*) *i*.

Quotient: multiply by the conjugate of the divisor, then round: (*a* + *b i*) ÷ (*x* + *y i*) = ⌊(*a x* + *b y*) / *n*⌉ + ⌊(*b x* − *a y*) / *n*⌉ *i*, where *n* = *x* ^{2} + *y* ^{2}.

Remainder: compute the quotient, then subtract quotient times divisor from dividend.

Your task is to write a small library of functions for operating on Gaussian integers. 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.

## Belphegor Primes

### October 31, 2014

Belphegor is one of the Seven Princes of Hell, charged with helping people make ingenious inventions and discoveries. Simon Singh gave the name Belphegor’s Prime to the number 1000000000000066600000000000001, which is the Number of the Beast, 666, from the Apocalypse, surrounded on each side by an unlucky 13 zeroes. Generally, Belphegor numbers B_{n} = (10^(*n*+3) + 666)*10^(*n*+1) + 1 have a 1, followed by *n* zeroes, followed by 666, followed by *n* zeroes, followed by 1. There are eight known Belphegor primes, with *n* ∈ {0, 13, 42, 506, 608, 2472, 2623, 28291} (A232448). You can read more about Belphegor primes at Cliff Pickover’s page.

Your task is to write a program that enumerates the Belphegor primes. 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.

## Number Of Divisors In A Range

### October 28, 2014

We have today an interview question that was posted to the internet by a candidate who didn’t get the job, and wondered what he had done wrong. I’ll paraphrase his question:

The interview question was to find the number of integers between

xandythat are divisible byn; you may assume thatx,yandnare all positive and thatx<y. I know the simple way to solve this is to loop over the range fromxtoy, like this:

`for(int i=x;i<=y;i++) if(i%n ==0) counter++;`

but that is very slow when the range is large, for instance

x= 0 andy= 3000000000.There must be some method that lets me reduce the number of iterations and optimize the code. Can someone please help me with that?

Your task is to help the candidate get the job by proposing a better algorithm to solve the 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.

## Three Farmers

### October 24, 2014

Today’s exercise is a math puzzle from Terence Tao:

Three farmers were selling chickens at the local market. One farmer had 10 chickens to sell, another had 16 chickens to sell, and the last had 26 chickens to sell. In order not to compete with each other, they agreed to all sell their chickens at the same price. But by lunchtime, they decided that sales were not going so well, and they all decided to lower their prices to the same lower price point. By the end of the day, they had sold all their chickens. It turned out that they all collected the same amount of money, $35, from the day’s chicken sales. What was the price of the chickens before lunchtime and after lunchtime?

Your task is to calculate the price of chickens before and after lunch. 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.

## Two-Base Palindromes

### October 21, 2014

I wanted to do this exercise as a follow-up to the earlier exercise on generating palindromes, but didn’t get around to it until now.

Your task is to write a program that generates a list of numbers that are palindromes in base 10 and base 8; for instance 1496941_{10} = 5553555_{8}. 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.

## Blackjack

### October 17, 2014

Blackjack is a casino game of chance, played by a player and a dealer. Both player and dealer are initially dealt two cards from a standard 52-card deck. If the player’s initial hand consists of an ace and a ten or face card, the player wins, unless the dealer also has an ace and a ten or face card, in which case the game is a tie. Otherwise, the player draws cards until he decides to stop; if at any time the sum of the pips on the cards (aces count either 1 or 11, face cards count 10) exceeds 21, the player is busted, and loses. Once the player is finished, the dealer draws cards until he has 17 or more pips, or goes bust, at which time the game ends. If neither has gone bust, the hand with the most pips wins. There are many variant rules, but we’ll keep things simple with the rules described above.

Your task is to simulate Blackjack and determine the winning percentage for the player. 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.

## Spiral Wrapping

### October 14, 2014

Today’s exercise appears regularly on lists of interview questions. We’ve done something similar in the past, but since it’s so common we’ll do it again.

Given a matrix, print a list of elements of the matrix. Start in the top-right corner of the matrix, move left across the top row, then down the left column, then across the bottom row, then up the right column to the element below the top row, then left, then down, then right, and so on, collecting the elements of the matrix as it goes. For instance, with this matrix

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20the elements are collected in order 4, 3, 2, 1, 5, 9, 13, 17, 18, 19, 20, 16, 12, 8, 7, 6, 10, 14, 15, 11.

Your task is to write a program to unwrap the elements of a matrix in the indicated spiral. 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.

## Formatting Text, Again

### October 10, 2014

Today’s exercise is a continuation of the previous exercise, which filled text to the maximum column width. Today’s exercise is to add justification to the previous exercise, so that extra space at the end of line of text is distributed to the spaces between the words on the line, making each line end at the right margin.

Your task is to write a program that fills and justifies text. 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.

## Text Formatting

### October 7, 2014

Text formatting is a huge topic. Today’s exercise looks at a simple text formatter. Input to the formatter is a file of ascii text; the input is free-form, except that paragraphs are marked by blank lines (two successive newline). The formatter copies the file to its output, moving text from one line to the previous line to fill each line as much as possible. It is possible to specify the width of a line, but if none is given the width defaults to sixty characters.

Your task is to write a simple text formatter. 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.