// you’re reading...

Project Euler Solutions

Project Euler Problem 8 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
… {continued}

Analysis

Starting with the largest 5 digit number, 99999, count downwards until that number is a substring of the 1000 digit number. Split out the five digits and multiply them together. Ignore any products that could evaluate to zero.

Solution

Runs < 1 second in Perl.

$string = <DATA>;
$p=1;
 
for ($i=99999; ; $i--) {
  if ($i !~ /0/ && $string =~ /$i/) {
    $p*=$_ for split //,$i;
    print "Answer to PE8 = $p";
    exit;
  }
}
__DATA__
731671765313306249...

Comments

Finds the maximum in 121 iterations.

Discussion

No comments for “Project Euler Problem 8 Solution”

Post a comment