functionProductInVector(digits)#' @@name ProductInVector#'#' @@description#' #' This function finds the largest product of cosecutive digits in vector all_digits.#' The product is determined by the amount of the input digits#'#' @@arg digits: A positive integer that determines how many cosecutive digits will be multiplied#'#' @@return The maximum product of cosecutive digits#' @@return The vector of all the consecutive digits that will be multiplied#' @@return The index of the first product in the vector all_digits max_product =1 index =0 vector = []for i in1:length(all_digits) product =1for j =1:digitsif ((i + j -1) >1000)breakend product = product*all_digits[i + j -1]endif product > max_product max_product = product index = iendendfor k =1:digits vector =push!(vector, all_digits[index + k -1])endreturn max_product, vector, indexend
ProductInVector (generic function with 1 method)
let’s now test our code with the given example
Code
ProductInVector(4)
(5832, Any[9, 9, 8, 9], 616)
Therefore we get that the maximum product of four adjacent digits is \(5832 = 9 \times 9 \times 8 \times 9\) and this is occurs at the index 616 in all_digits.
Now let’s find the maximum product of thirteen adjacent digits
Therefore we get that the maximum product of thirteen adjacent digits is \(23,514,624,000 = 5 \times 5 \times 7 \times 6 \times \times 6 \times 8 \times 9 \times 6 \times 6 \times 4 \times 8 \times 9 \times 5\) and this product occurs at index 198.