Problem 8 is another one of those problems that’s pretty simple to approach. The only even minor twist is that it’s probably best to use a string in this one. But easily enough done.
Some on the P.E. discussion boards even did this one with no programming, just a visual scan of the number.
“Find the greatest product of five consecutive digits in the 1000-digit number.”
Strung out code -
-module(euler8).
-export([greatestproduct/0]).
greatestproduct() ->
Source = "731671765313306 [etc, really long number] 52963450",
N = 1,
InitialGreatest = 0,
Greatest = teststring(Source,N, InitialGreatest),
Greatest.
teststring(Source, N,Greatest) ->
if N < (length(Source)-5) ->
Anum = list_to_integer(string:substr(Source,N,1)),
Bnum = list_to_integer(string:substr(Source,N+1,1)),
Cnum = list_to_integer(string:substr(Source,N+2,1)),
Dnum = list_to_integer(string:substr(Source,N+3,1)),
Enum = list_to_integer(string:substr(Source,N+4,1)),
Product = Anum*Bnum*Cnum*Dnum*Enum,
if Product > Greatest ->
teststring(Source, N+1, Product);
true ->
teststring(Source, N+1, Greatest)
end;
true ->
Greatest
end.

Post a Comment