Reputation: 2125
I disgress, I am stuck and I really can't wrap my head around what's wrong. The problem reads.
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Now I store the digit as a string, I only fetch the numbers, I loop through and grab each 13-digit "substring" as an array, multiply them together and compare them. Now I have verified that I am only getting a 1000-digit char array, I have verified that I get exactly 75 char arrays of equal size. Yet I am not getting the right answer.
Here is the code
let problem8() =
let str = @"731671765313306..."
|> Seq.filter (Char.IsDigit)
|> Seq.toArray
(* We only need to go to 987 because 1000 isn't divisble by 13 and if we were to take the last 11 digits from 987
we would end up with 0 anyhow. *)
seq { for i in 0.. 13 ..987 -> str.[i..i + 12] }
|> Seq.map (Seq.fold (fun acc chr -> acc * int64 (Char.GetNumericValue(chr))) 1L)
|> Seq.max
problem8()
|> printfn "%d"
Upvotes: 0
Views: 218
Reputation: 243096
If you are using 0 .. 13 .. 987
in the sequence expression, then you are partitioning the array like this (for simplicity, using 3-sized blocks in 10 digits):
[012][345][678]9
I suppose that the question wants you to look for all possible sub-strings, i.e.
[012][345][678]9
0[123][456][789]
01[234][567]89
So, you probably need to try all indices using 0 .. 987
.
By the way, I'd suspect that converting char to int64
will be faster using int64 c - 48L
.
Upvotes: 1