## Concatenate N N Times

### May 10, 2016

A number like 7777777 consists of the number 7 concatenated to itself 7 times. A number like 121212121212121212121212 consists of the number 12 concatenated to itself 12 times.

Your task is to write a program that calculates the number that is concatenated to itself the number of times as the number is (that’s hard to say). 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.

## Baby Steps, Giant Steps

### May 6, 2016

In a previous exercise we discussed the discrete logarithm problem, which is to compute the exponent *y* in the expression *x ^{y}* ≡

*n*(mod

*m*), given

*x*,

*n*, and

*m*; the modulus

*m*is usually taken as prime. Today we look at an algorithm, known as baby steps, giant steps, that was developed by Daniel Shanks in 1971:

1. Compute limits:

*b* = ⌈ √*m* ⌉

*h* = (*x*^{−1})^{b}

2. Construct lists:

*A* = { *x ^{i}* :

*i*= 0, 1, …,

*b*− 1 } // giant steps

*B* = { *n h ^{j}* :

*j*= 0, 1, …,

*b*− 1 } // baby steps

3. Sort and find intersection:

Sort the lists *A* and *B*

Find an intersection, say *x ^{i}* =

*n h*

^{j}Return *y* = *i* + *j b*

Since *m* is prime, there must be some *y* ∈ [0, *m*) for which *x*^{y} ≡ *n* (mod *m*). Write *y* = *i* + *j b*, where *b* = ⌈ √*m* ⌉. Since *y* must exist, so too *i* (which counts the giant steps) and *j* (which counts the baby steps) must exist, and there must be an intersection between the baby steps and the giant steps.

Time complexity is obviously O(sqrt *m*), which beats the O(*m*) time complexity of the brute-force algorithm of the previous exercise. There are better algorithms for computing discrete logarithms, which we will study in future exercises.

Your task is to write a program that calculates discrete logarithms using the baby steps, giant steps algorithm. 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.

## Discrete Logarithms

### May 3, 2016

The discrete logarithm problem is to compute the exponent *y* in the expression *x ^{y}* ≡

*n*(mod

*m*), given

*x*,

*n*, and

*m*;

*x*and

*m*must be relatively prime, which is usually enforced by taking the modulus

*m*as prime. For instance, in the expression 3

^{y}≡ 13 (mod 17), the discrete logarithm

*y*= 4, since 3

^{4}≡ 13 (mod 17). The discrete logarithm problem is of fundamental importance in some branches of cryptography, and bears many similarities to factoring integers. Although we have states the discrete logarithm problem using integers, in many cases some other group is used, for instance calculating discrete logarithms on an elliptic curve.

The simplest algorithm for finding the discrete logarithm is simply to try each *y* from 0 to *m*; if *m* is prime, one of the *y* is certain to work. Unfortunately, this algorithm is very slow, taking time O(*m*). We’ll see better algorithms in future exercises; our purpose today is to introduce the concept of the discrete logarithm, and to provide a known good algorithm as a base for testing future algorithms.

Your task is to write a program that computes discrete logarithms by trying each possible value in succession until the answer is found. 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.

## Binary Search

### April 29, 2016

I goofed.

While writing a program (it may appear in a future exercise) I needed to search a sorted array for a target value. I should have copied an existing binary search, but instead I wrote my own, since I’m a good programmer and can certainly write a simple function like that. You won’t have any trouble guessing what happened next.

Your task is to write a binary search function; do it yourself, without looking at any library implementations or searching the internet. You might also want to write a test script to give you confidence in your function. 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.

## An Integer Formula For Fibonacci Numbers

### April 26, 2016

Today’s exercise isn’t really an exercise but an astonishing integer formula for computing the *n*th Fibonacci number; here it is in Python:

def fib(n): return (4 << n*(3+n)) // ((4 << 2*n) − (2 << n) − 1) & ((2 << n) − 1)

You can see an explanation here and discussion here.

Your task is to translate the program to your favorite language. 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.

## GCD Sum

### April 22, 2016

Today’s exercise is inspired by A018804: Find the sum of the greatest common divisors gcd(*k*, *n*) for 1 ≤ *k* ≤ *n*.

Your task is to write a function that finds the sum of the greatest common divisors. 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.

## Interview Timing

### April 19, 2016

I came across an interesting interview question recently. I’ll tell you the question shortly. What made it interesting was that the same question was given to all candidates, and they were timed in getting a solution; candidates with shorter times were given higher scores than candidates with longer times.

Your task is to write the requested program, which you can access HERE after you are set up for timing.

## Ten-Digit Pandigital Numbers Divisible By 1 Through 9

### April 15, 2016

Today’s exercise solves somebody’s homework problem — but not too much, since he had to write his program in Java:

Find the two smallest ten-digit pandigital numbers (numbers that contain all the digits from 0 through 9) that are divisible by the numbers 1 through 9. Then find the two largest pandigital numbers that are divisible by the numbers 1 through 9.

Your task is to solve the homework 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.

## Titlecase

### April 12, 2016

A string is titlecased when the first letter of each word is capitalized and the remaining letters are lower case. For instance, the string “programming PRAXIS” becomes “Programming Praxis” when titlecased.

Your task is to write a function that takes a string and returns it in titlecase. 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.

## Google Interview Question

### April 8, 2016

Today’s exercise is an interview question from Google:

Given a list of words, find the maximum value of the product of the lengths of two words from the list, subject to the constraint that the two words cannot share any letters. For instance, given the words ABCW, BAZ, FOO, BAR, XTFN, and ABCDEF, the pair FOO BAR has a product of 9, the pair BAZ XFTN has a product of 12, and the pair ABCW XTFN has a product of 16, which is the maximum. Note that the pair ABCW ABCDEF doesn’t work because the two words share three letters.

Your task is to write a program to solve Google’s interview question. 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.