Riddle 8: Largest Product in A Series

Riddle 8: Largest Product in A Series

Mia Temma's photo
Mia Temma
·Feb 11, 2022·

3 min read

The Task

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

This task is less difficult than the previous ones, as it does not require any optimization.


Step 1. Converting the list

First of all, we received the list as a long string, which does not help us much: What we need is a list of digits, (7 3 1 6 ...). So the first thing to do is to chop the list into single characters ("7" "3" ...) and convert these back to integers with the format function, which we apply to all list elements with mapcar.

: (setq Num 7316717653133.... ) # 1000 digits number
-> 7316717653133... 
: (setq L (mapcar format (chop Num)))
-> (7 3 1 6 7 1 7 6 5 3 1 3 3

Step 2. Multiplying the n'th elements

We are looking for the largest product of 13 adjacent digits. In other words, we should go over the list. First we multiply the digits 1 to 13, then digit 2 to 14, digit 3 to 15 and so on. We can get the product of the first 13 numbers of a list with

(apply * (head 13 L))

Now in order to walk down the list, we can use mapcon (read here for more on mapping functions). mapcon takes a function and applies it consecutively to a list and all its CDRs.

mapcon is concatenating the return values, which means that they must be lists. Thus we can create an anonymous function which takes a list as argument and returns a (one-element) list.

: (mapcon
   '((Lst) (list (apply * (head 13 Lst))))
   L)
-> (5000940 0 0 0 0 0 0 0 0 0 0 0 0 0 4199040 4898880 ... )

Since we are looking for the maximum value in the list, we could stop as soon as our list has less than 13 elements. But on the other hand, a continuous check might be more expensive then just letting it run. Let's double-check what happens if our remaining list has less than 13 elements:

: (head 4 '(1 2))
-> (1 2)

Our list (1 2) only has two elements, so (head 4 '( 1 2)) only returns these two elements, and (apply * (head 4 '(1 2))) returns 2. We know that this can't be the maximum we are looking for, but it also doesn't influence our result in any way. So let's just leave it like that.


Step 3: Finding the maximum value

Now we have a list of all consecutive products in our list. Now we want to find the maximum. We can do this with the maxi function (also described here). maxi takes a function as argument, but actually we don't need to apply any function. For this purpose we can use prog:

(prog . prg) -> any Executes prg, and returns the result of the last expression.

(prog L) just returns the list L, and (maxi prog L) returns the maximum value of a list.


So let's bring it all together. We define a function largest_prod that takes two arguments, Num for the 1000-digit number and I for the number of digits in our consecutive product. Our finished program looks like this:

(de largest_prod (Num I)
   (maxi prog
      (mapcon
         '((L) (list (apply * (head I L))))
         (mapcar format (chop Num)) ) ) )

You can find the source code of the finished program here.


Sources

 
Share this