## Luhn algorithm in F#

The Luhn algorithm is a simple error detection formula based on the modulus operator which is widely used to validate credit card numbers or Canadian Social Insurance Numbers.

This checksum function can detect any single-digit error and operates verifying the number against its included check digit.

The algorithm is made of three steps:

1) starting from the rightmost digit and moving left, double the value of every second digit (or digits in even positions);

2) sum all resulting digits together with the original ones that were untouched;

3) if the result is a multiple of 10 (i.e. the result modulus 10 is zero) then the input value is valid.

This simple algorithm can be implemented with a few F# lines of code:

#light open System let double_digit n = let double = n * 2 if (double > 9) then double - 9 else double let rec luhn_loop isEven acc input = match input with | [] -> acc % 10 = 0 | x :: xs -> let num = Int32.Parse(x.ToString()) if (isEven) then luhn_loop (not isEven) (acc + (double_digit num)) xs else luhn_loop (not isEven) (acc + num) xs let luhn n = n.ToString() |> Seq.to_array |> Array.rev |> Seq.to_list |> luhn_loop false 0

I separated from the main loop the *double_digit* function, which takes a digit and multiplies it by two. If the result has two digits (i.e. it is greater than 9) with sum together the two digits (i.e. subtract 9 from the number), otherwise we simply return it.

The *luhn_loop* function iterates over the list of digit that is obtained by taking the original number, converting it into an array of digits and reversing it.

Inside each iteration we take into account a single digit, double it if it is placed in an even position and sum it to the accumulator.

When the list of digits to be examined is empty we are done with the loop and we just have to check if the value stored in the accumulator is evenly divisible by 10.

## Project Euler in F# – Problem 52

After a long break, let's resume with the **Project Euler** series.

The next one to be solved is Problem 52:

It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.

Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits.

As usual, we have to break the problem into small pieces and solve these pieces one by one.

As in many other Project Euler problems we have to extract the digits of a number, and this is done by converting the number into a string (i.e. an array of characters) and then casting the characters to integers.

We also need a function to check if two numbers are made of the same digits. For each number we create a set from the sequence of its digits, in order to ignore the order of the items and discard duplicates. Then we apply the standard set comparison operator to get the result.

We have to multiply each positive integer by 2, 3, 4, 5 and 6, so we can define the *multiples* function, which takes an integer *x* and returns a list containing five elements, corresponding to *2x*, *3x*, *4x*, *5x* and *6x*.

Thanks to these three functions, checking if a number and its multiples contain exactly the same digits is very easy. In fact, we generate the list of multiples and test all of them with the *same_digits* function.

The algorithm to get the answer to the problem is the following: generate an infinite list of integers (remember *seq_unfold*?), test each number with the *check* function and return the first value that passes the test.

#light let digits n = n.ToString() |> Seq.map (string >> int) let same_digits a b = Set.equal (Set.of_seq (digits a)) (Set.of_seq (digits b)) let multiples n = [2 .. 6] |> Seq.map (fun x -> n * x) let check n = n |> multiples |> Seq.for_all (same_digits n) let answer = 1 |> Seq.unfold (fun i -> Some (i, i + 1)) |> Seq.first (fun x -> if check x then Some (x) else None)

There is also a well-known (and tricky) solution for this problem, which was presented in the book "*The man who calculated*". Did you know it?

## Project Euler in F# – Problem 36

A number (in general a word) is *palindromic* when it can be read in the same way from left to right and in the opposite direction, i.e. it is "*symmetrical*".

According to the definition, a number can be palindromic when written in *base10* and not palindromic in *base2*, but there are some numbers that are palindromic in both bases.

In Project Euler Problem 36 we have to find all these numbers between 1 and 1 million:

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

To solve this problem we need two helper functions, one to check whether a number is palindromic and another one to convert a number from base10 to base2:

#light open Microsoft.FSharp.Math let isPalindromic n = let chars = n.ToString().ToCharArray() chars = Array.rev chars let toBinary (n : BigInt) = let rec toBinary1 a v = match v with | 0I -> a | _ -> let q,r = BigInt.DivMod (v,2I) toBinary1 (r.ToString()::a) q toBinary1 [] n |> List.reduce_left (^) |> BigInt.Parse let euler36 = [1I .. 1000000I] |> List.filter isPalindromic |> List.filter (fun x -> x |> toBinary |> isPalindromic) |> List.reduce_left (+)

The first function checks if a number is palindromic by converting it into an array of digits and checking if this array is equal to its reverse one.

Then we have the *toBinary* function that is based itself on an inner function called (with a lot of originality) *toBinary1*.

This recursive function uses the list *a* to store the remainders of the repeated divisions by 2, until the original number is over.

These digits, converted as strings, are joined with *^*, the *string concatenation operator* and eventually the result is converted to a *BigInt*.

To find the answer to the problem we start with the list of all numbers between 1 and 1 million and take (filter) only the elements which are palindromic in base10.

After this first step, we filter the list again, this time keeping only those numbers that are palindromic when converted to base2.

The last step is the easiest, we only need to sum the numbers left to get the answer to the problem.

## Project Euler in F# – Problem 25

After a long break, let's try to resume with a regular publication scheme, starting with **Project Euler's problem 25**, an easy one, which says:

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.

...

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

The problem definition also contains the algorithm to solve it, I told you it was easy.

Actually, we just have to write a function to compute the Fibonacci sequence and stop when a term contains a thousand digits.

Since we don't know when we are done evaluating the terms of the sequence we have to generate an **infinite sequence** and check every new item to see if we can stop.

Let's give a look at the **F#** code:

#light open Microsoft.FSharp.Math.BigInt let has1000Digits (n : Microsoft.FSharp.Math.BigInt) = n.ToString().Length = 1000 let euler25 = ((1I,1I) |> Seq.unfold (fun (n0, n1) -> (if has1000Digits n0 then None else Some(n0, (n1, n0 + n1)))) |> Seq.length) + 1

The *Seq.unfold* function is used to generate the infinite sequence, returning *Some* when we want to continue and *None* when we are done searching (i.e. the term has 1000 digits).

In the latter case, the length of the sequence represents the number of elements generated before the one with a thousand digits, so we have to sum 1 to get the desired answer.

## Project Euler in F# – Problem 48

The following problem from **Project Euler** is one of those supposed to be solved either with intensive computation or a "smarter" approach.

Here is the description of problem number 48:

The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.

Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.

If our language allows us to evaluate the series mentioned above (and F# does!), we can just do it and then **take the last 10 digits** of the result as required.

This can be implemented by the following F# code:

#light open Microsoft.FSharp.Math.BigInt let last10 num = num % (pow 10I 10I) let answer = [1I .. 1000I] |> Seq.map (fun x -> pow x x) |> Seq.fold (+) 0I |> last10

The first problem is **extracting the last ten digits from a given number**, and this is done by dividing the number by 10^10 and returning the remainder of the division (line 4).

Then we have to **generate the series** *n*^*n* for all *n* from 1 to 1000 and sum the items.

This sum is actually a huge number (more than 3000 digits) but we are only interested in the last ten digits, so we can use the **pipeline operator** (*|>*) to pass it to the *last10* function.

The only caveat is to use **BigInt**, otherwise the numbers we are talking about will never fit into the other numeric data types.

Easy, isn't it?

## Even Digits Multiple Of Nine

The mathematical exercise we are going to solve comes from mathschallenge.net and can be summarized in a single sentence:

Find the smallest multiple of nine containing only even digits.

We just need a function to **check if a number contains only even digits** and then pass to it the multiples of nine.

This algorithm can be translated into the following code:

#light open System let isEven n = (n % 2 = 0) let rec allEvenDigits n = match n with | n when n < 10 -> isEven n | n -> (isEven (n % 10)) && (allEvenDigits (Int32.div n 10)) let nums = 9 |> Seq.unfold (fun i -> Some (i, i + 9)) let answer = Seq.find allEvenDigits nums

The *isEven* function is just a "shortcut" for the modulus operator and it is used inside *allEvenDigits*, where it is applied to each digit of the given number.

Then, we have to generate the multiples of nine, but we don't know when to stop, so we need an **infinite sequence**, that can be produced by the *Seq.unfold* function.

At the end we use *Seq.find*, which takes a boolean function (*allEvenDigits*) and a sequence (*nums*), and returns the first element of the sequence for which the given function returns "true", i.e. **the smallest multiple of nine containing only even digits**, that is exactly what we had to find.

## Project Euler in F# – Problem 16

This time we are gonna "*cheat*".

As in Problem 20 we have to **sum the digits** of a number, so we can just copy and paste the whole code and edit a single line to have a working solution.

The problem says:

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 2^1000?

The only difference between this exercise and the last one is the number to evaluate.

However, as for the factorial, F# already provides us with a native function for the powers of a numbers, so the final code will be:

#light open Microsoft.FSharp.Math.BigInt let rec digits n = match n with | n when n < 10I -> n | n -> let x, y = divmod n 10I y + digits x let answer = digits (pow 2I 1000I)

If you compare this exercise with the last one, you can see that we actually changed one line, i.e. line 11.

Do you really want me to explain this code?

## Project Euler in F# – Problem 20

Project Euler's **Problem 20** was trickier than I expected.

I'm quite sure that there is a **smart solution**, but there is also a "*dumb*" one and this is the one I'm going to present you.

The problem says:

n! means n × (n ? 1) × ... × 3 × 2 × 1

Find the sum of the digits in the number 100!

Evaluating the **factoria**l is straightforward in any functional language, but it is even easier in F# since the language already provides us with the *factorial* function.

As usual, let's show the code and then analyze it:

#light open Microsoft.FSharp.Math.BigInt let rec digits n = match n with | n when n < 10I -> n | n -> let x, y = divmod n 10I y + digits x let answer = digits (factorial 100I)

The *open* directive at line 2 permits the use of the *BigInt* **namespace**, which is needed when working with very large numbers.

Then we have the code to sum the digits of a number, which is easily implemented with **pattern matching**.

The algorithm is based on the **modulus operator**, since taking the last digit of a number is equivalent to taking the remainder of the number itself divided by 10 (in a base 10 system).

Hence, we take the last digit and **recursively** apply the same function to the rest of the number, until there is only one digit left, i.e. the number is less than 10.

This operation is done by the *divmod* function, which returns the integer quotient and the remainder as a couple (x, y).

You may have noticed that there is a capital "*I*" after each number. That letter stands for **Big Integer** and is used to distinguish them from normal integers.

It's done, we just have to apply the digits function to the output of *factorial 100I*.

Why did I call this solution "*dumb*"?

Because I had to compute 100! (which is a number with **158 digits**) and then sum its digits, but this solution is not feasible if the number becomes much larger.

I think that there should be some **mathematical trick** to do the same job, but I don't know it.

Does anybody have a different approach to suggest?