Duncan's blog

October 26, 2008

Project Euler: problem 8

Filed under: Coldfusion,Project Euler — duncan @ 7:00 am
Tags: , , , ,

Problem 8:

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

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

This time it doesn’t give us any smaller test case, so we’ll need to just trust our code is correct. I found this one fairly simple, maybe because there’s no difficult maths involved. I expect you could probably code a more interesting solution. For example you could eliminate any set of 5 digits with a zero. You could also keep a tally of digits you’ve already tried, e.g. for the first two sets of 5 digits, you could have:
tally[1] = 13677;
tally[2] = 11367;
etc. This way you could eliminate the need to do the multiplication for anything you’ve already done. It’s doubtful there’d be any advantage in doing this, for this sort of number. Might be more useful on larger numbers.

```<cfsavecontent variable="bigNumber">
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
</cfsavecontent>

<cfset bigString = "">

<cfset max = 0>

<!--- first, turn this into a proper string --->
<cfloop index="i" list="#bigNumber#" delimiters="#Chr(13)##Chr(10)#">
<cfset bigString = bigString & i>
</cfloop>

<!--- loop through string --->
<cfloop index="i" from="1" to="996">
<!--- get our 5 digits --->
<cfset a = Mid(bigString, i, 1)>
<cfset b = Mid(bigString, i+1, 1)>
<cfset c = Mid(bigString, i+2, 1)>
<cfset d = Mid(bigString, i+3, 1)>
<cfset e = Mid(bigString, i+4, 1)>

<cfset sum = a * b * c * d * e>
<cfoutput>#a# * #b# * #c# * #d# * #e# = #sum#</cfoutput><br>

<cfif sum GT max>
<cfset max = sum>
</cfif>
</cfloop>

<cfoutput>#max#</cfoutput>
```

I decided to treat the number given exactly as it was, and not to manually eliminate the linebreaks. Using <cfsavecontent>, we can then treat it as a list with CRLF linebreaks. I turn the whole thing into a string (alternatively I could have just used Replace to get rid of the linebreaks). Then I loop through it, up to 5 digits before the end of the string. Using Mid() to get each number out of the string, do the multiplication, and compare it to whatever is the maximum value so far.

Another way along this line might have been to turn the whole thing into a 1000 element array, and loop through the array. Typically array methods in Coldfusion will work faster than list and string methods that do the same thing.

1. […] one was a bit like problem 8, with a large block of numbers which is ideal for plugging into <cfsavecontent>. Use a nested […]

Pingback by Project Euler: problem 11 « Duncan’s blog — October 29, 2008 @ 6:52 am

2. […] this seems reasonably straightforward. Reusing our <cfsavecontent> technique from problem 8 and problem 11, we can just loop through the numbers, adding them up, […]

Pingback by Project Euler: problem 13 « Duncan’s blog — October 30, 2008 @ 6:39 am

3. […] previously blogged about this Project Euler puzzle 6 years ago, using ColdFusion.  This is my approach using PHP as a simple practical exercise for myself, and […]

Pingback by Project Euler: problem 8 (PHP) | Duncan's blog — September 26, 2014 @ 8:00 am

Create a free website or blog at WordPress.com.