find the thirteen adjacent digits in the 1000-digit number that have the greatest product. what is the value of this product? in matlab

Assuming the 1000-digit number is stored in a string format in Matlab, the following code can be used to find the thirteen adjacent digits that have the greatest product:

main.m
numStr = '73167176531330624919225119674426574742355349194934...'; % the 1000-digit number
maxProd = 0;

for ii = 1:length(numStr)-12 % loop over all possible sets of 13 adjacent digits
    currProd = 1;
    for jj = 0:12 % calculate product of the 13 digits
        currProd = currProd * str2double(numStr(ii+jj));
    end
    if currProd > maxProd % update maximum product if necessary
        maxProd = currProd;
    end
end

disp(maxProd) % display the maximum product
472 chars
15 lines

Note that this code uses two nested loops to iterate over all possible sets of 13 adjacent digits in the 1000-digit number, and calculates their product using a variable called currProd. The maximum product found is stored in maxProd and is displayed at the end.

gistlibby LogSnag