Duncan’s blog

October 31, 2008

Project Euler: problem 17

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

After completing the first eleven problems, I’ve started to cherry-pick the ones I think I can tackle. I jumped ahead a bit to problem 17.

Problem 17:

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.

So this is kind of a fun problem that I think is actually a useful thing to know as a web developer. You might not have a practical application where you need to convert digits to words, however there are similar applications. For instance you might want to output a date or time difference in words rather than numbers, e.g. ‘you last logged in four days / three weeks / two months / one year ago’. The principles are similar.

<cfset numbers = ArrayNew(1)>
<cfset words = ArrayNew(1)>
<cfset sum = 0>

<cfset numbers[1] = "one">
<cfset numbers[2] = "two">
<cfset numbers[3] = "three">
<cfset numbers[4] = "four">
<cfset numbers[5] = "five">
<cfset numbers[6] = "six">
<cfset numbers[7] = "seven">
<cfset numbers[8] = "eight">
<cfset numbers[9] = "nine">
<cfset numbers[10] = "ten">
<cfset numbers[11] = "eleven">
<cfset numbers[12] = "twelve">
<cfset numbers[13] = "thirteen">
<cfset numbers[14] = "fourteen">
<cfset numbers[15] = "fifteen">
<cfset numbers[16] = "sixteen">
<cfset numbers[17] = "seventeen">
<cfset numbers[18] = "eighteen">
<cfset numbers[19] = "nineteen">
<cfset numbers[20] = "twenty">
<cfset numbers[30] = "thirty">
<cfset numbers[40] = "forty">
<cfset numbers[50] = "fifty">
<cfset numbers[60] = "sixty">
<cfset numbers[70] = "seventy">
<cfset numbers[80] = "eighty">
<cfset numbers[90] = "ninety">

<cfloop index="i" from="1" to="1000">
	<cfset numberAsString = "">
	
	<!--- how many thousands? --->
	<cfset thousands = i \ 1000>
	<cfset hundreds = i \ 100>	<!--- DIV 100 --->
	<cfset tens = (i \ 10) MOD 10>	<!--- DIV 10 MOD 10 --->
	<cfset units = i MOD 10>
	
	<cfif thousands GT 0>
		<cfset numberAsString = numberAsString & numbers[thousands] & " thousand">
	</cfif>
	
	<cfif hundreds GT 0 AND hundreds LT 10>
		<cfset numberAsString = numberAsString & numbers[hundreds] & " hundred">
		
		<cfif (tens GT 0 AND tens LT 10) OR units GT 0>
			<cfset numberAsString = numberAsString & " and ">
		</cfif>
	</cfif>
	
	<cfif tens EQ 1>
		<cfset numberAsString = numberAsString & numbers[(tens*10) + units]>
	</cfif>
	
	<cfif tens GT 1 AND tens LT 10>
		<cfset numberAsString = numberAsString & numbers[tens*10] & " ">
	</cfif>
	
	<cfif units GT 0 AND tens NEQ 1>
		<cfset numberAsString = numberAsString & numbers[units]>
	</cfif>
	
	<cfoutput>#numberAsString#</cfoutput><br>
	
	<!--- strip out the spaces --->
	<cfset ArrayAppend(words, REReplace(numberAsString, "[[:space:]]", "", "ALL"))>
</cfloop>

<cfloop index="i" from="1" to="#ArrayLen(words)#">
	<cfset sum = sum + Len(words[i])>
</cfloop>

<cfoutput><strong>#sum#</strong></cfoutput>

This code’s not as lean as it could be. However I hope it’s easy to understand, which is perhaps more important!

Firstly I create an array for the words we’ll need to use to build up our string. I set the array index equal to whatever that number is. This means our array has lots of empty elements; we’ll be fine so long as we don’t try and loop through the array from 1 to the end.

Then I loop up to 1000. At each iteration, do some DIV and MOD calculations on the loop counter. In Coldfusion, the DIV operation is represented by a \ instead of the normal / used for floating point division. Just to clarify for anyone who doesn’t know what I mean by DIV: divide a by b. MOD gives you just the remainder, DIV gives you the integer part before the remainder (aka the ‘quotient‘). So 12 DIV 10 = 1. Or in CFML:
<cfset x = 12 \ 10>

Then gradually build up the string based on what number we have for each of the thousands, hundreds, tens and units. We need to make a special exception for the numbers between eleven and nineteen, because we don’t write them as ‘ten one’, ‘ten two’ etc.

Then I strip out the spaces, store this in a separate array, loop through that array and add up the lengths. This last step could be done in a variety of simple ways. There’s no real advantage to me using the array, I could just have easily ditched that array and second loop, and just made the last line inside my main loop
<cfset sum = sum + Len(REReplace(numberAsString, “[[:space:]]”, “”, “ALL”))>

October 30, 2008

UK Postcode validation

While looking at some old code at work that was doing postcode validation, I thought there must be a better way to do it with a regular expression. By chance I came across this page, UK Government Data Standards Catalogue, which documents the standard for Post Codes. They also have this XML document which basically gives the regex required.

I was going to submit it to the CFLib, but discovered there was already a IsZIPUK function which ostensibly did the same thing, dating back to about 2001! However its regular expression didn’t validate all postcodes correctly. So I submitted this as an update to that function, which has now been published.

The regular expression’s a bit of a mess, I’m no regex master, so if anyone has any tips on how to simplify it, let me know.

Project Euler: problem 13

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

Problem 13:

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
… etc.
53503534226472524250874054075591789781264330331690

So this seems reasonably straightforward. Reusing our <cfsavecontent> technique from problem 8 and problem 11, we can just loop through the numbers, adding them up, right?

<cfsavecontent variable="number">
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
...
53503534226472524250874054075591789781264330331690
</cfsavecontent>

<cfset sum = 0>

<cfloop index="i" list="#number#" delimiters="#Chr(13)##Chr(10)#">
	<cfset sum = sum + i>
</cfloop>

<cfoutput>#sum#</cfoutput>

This code returns a number in scientific notation, because of the limitations of 32-bit integers. If you’re using CFMX (I’m doing this in CF 5), you could just cast to a Java long int to get your answer. I tried a variety of ways to present the total sum, then realised the answer was right there on the screen.

The answer is in the form
1.23456789012E+051
i.e. the equivalent of
1.23456789012 x 1051
i.e. the equivalent of
123456789012 followed by forty digits.
I’m only looking for the first ten digits, so I just manually removed the decimal and grabbed those digits. KISS!

October 29, 2008

Project Euler: problem 11

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

This is one of those problems that at first glance looks really difficult, but when it comes down to it, wasn’t that hard.

Problem 11

In the 20 x 20 grid below, four numbers along a diagonal line have been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 x 63 x 78 x 14 = 1788696.

What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20 x 20 grid?

One thing it doesn’t mention in that spec is if the numbers have to be in a straight line, or if it means it could be up, left, diagonal, right all in the same set of four.  However, in the list of all the problems, it summarises it as "What is the greatest product of four numbers on the same straight line in the 20 by 20 grid?"

So basically this can be done with four sets of nested loops, one each for horizontal, vertical, and the two diagonals.

Going diagonally, we have to exclude some values in opposing corners. Laying it out like this helped me visualise what I need to do in the code:

Diagonal 1:

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

Diagonal 2:

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

This one was a bit like problem 8, with a large block of numbers which is ideal for plugging into <cfsavecontent>. Use a nested loop when constructing the array, as our block of numbers is delimited on the line by CRLF, and delimited between the numbers by spaces.

<cfsavecontent variable="grid">
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
</cfsavecontent>

<cfset numbers = ArrayNew(2)>
<cfset m = 1>
<cfset max = 0>
<cfset product = 0>

<!--- store all this in a 2D array --->
<cfloop index="i" list="#grid#" delimiters="#Chr(13)##Chr(10)#">
	<cfset n = 1>
	<cfloop index="j" list="#i#" delimiters=" ">
		<cfset numbers[m][n] = j>
		<cfset n = n + 1>
	</cfloop>
	<cfset m = m + 1>	
</cfloop>


<!--- across --->
<cfloop index="i" from="1" to="#ArrayLen(numbers)#">
	<cfloop index="j" from="1" to="#(ArrayLen(numbers[i])-3)#">
		<cfset product =numbers[i][j] 	* 
				numbers[i][j+1]	* 
				numbers[i][j+2]	* 
				numbers[i][j+3]>
		<cfif product GT max>
			<cfset max = product>
		</cfif>
	</cfloop>
</cfloop>

<!--- down --->
<cfloop index="i" from="1" to="#(ArrayLen(numbers)-3)#">
	<cfloop index="j" from="1" to="#ArrayLen(numbers[i])#">
		<cfset product =numbers[i][j] 	* 
				numbers[i+1][j]	* 
				numbers[i+2][j]	* 
				numbers[i+3][j]>
		<cfif product GT max>
			<cfset max = product>
		</cfif>
	</cfloop>
</cfloop>

<!--- diagonally 1, \\\\ --->
<cfloop index="i" from="1" to="#(ArrayLen(numbers)-3)#">
	<cfloop index="j" from="1" to="#(ArrayLen(numbers[i])-3)#">
		<cfset product =numbers[i][j]		* 
				numbers[i+1][j+1]	* 
				numbers[i+2][j+2]	* 
				numbers[i+3][j+3]>
		<cfif product GT max>
			<cfset max = product>
		</cfif>
	</cfloop>
</cfloop>

<!--- diagonally 2, //// --->
<cfloop index="i" from="4" to="#ArrayLen(numbers)#">
	<cfloop index="j" from="1" to="#(ArrayLen(numbers[i])-3)#">
		<cfset product =numbers[i][j]		* 
				numbers[i-1][j+1]	* 
				numbers[i-2][j+2]	* 
				numbers[i-3][j+3]>
		<cfif product GT max>
			<cfset max = product>
		</cfif>
	</cfloop>
</cfloop>

<cfoutput><strong>#max#</strong></cfoutput>

For each of the loops, we don’t want to go all the way to the end of the line. For instance on the horizontal one, we loop as far as the 3rd last, because we’re going to grab those last 3 values anyway.
The vertical loop just switches round the values for i and j from the horizontal loop.
The diagonal loops require manipulating both i and j values.

October 28, 2008

Project Euler: problem 10

This one had me stumped for a while. The code isn’t that difficult, but making something that can run efficiently on my old CF 5 server seemed a bit trickier. In the end I used something that took ages to run, and decided to refactor my code once I’d read some of the discussions you get access to after completing these problems.

Problem 10:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

The brute force way to do this would be to loop from 1-2000000, checking if each number is prime before adding it to your total. A slightly less brute method would be to do it in steps of 2, avoiding the even numbers (except 2). I tried to avoid doing that, using various methods I’d already been using in earlier ones, trying to apply the Sieve of Eratosthenes, stepping in sixes, etc but with no luck. So I went back to the brute force, which took many minutes. Then rewrote my code.

I initially hadn’t been too far away from getting the solution, just needed a bit of tidying up. Here’s the revised code:

<cfset limit = 2000000>
<cfset numbers = ArrayNew(1)>

<!--- initialise our array --->
<cfset ArraySet(numbers, 2, limit, 1)>

<!--- switch off all the even numbers except 2 --->
<cfloop index="i" from="4" to="#ArrayLen(numbers)#" step="2">
	<cfset numbers[i] = 0>
</cfloop>

<!--- we only need to loop up to the root of our limit --->
<cfloop index="i" from="3" to="#SQR(limit)#" step="2">
	<cfif numbers[i]>
		<cfloop index="j" from="#(i * i)#" to="#limit#" step="#(2 * i)#">
			<cfset numbers[j] = 0>
		</cfloop>
	</cfif>
</cfloop>

<cfset sum = 0>

<cfloop index="i" from="2" to="#limit#">
	<cfif numbers[i]>
		<cfset sum = sum + i>
	</cfif>
</cfloop>

<cfoutput>#sum#</cfoutput> 

I’m still lacking a true understanding of how to generate prime numbers efficiently. The things that I didn’t get right in my first go round:

  • only looping to the square root of our limit
  • not needing to actually check if a number is prime
  • the values for the from and step attributes in this line:
    <cfloop index=”j” from=”#(i * i)#” to=”#limit#” step=”#(2 * i)#”>
    I had them as from=”#(i * 2)#” and step=”#i#”

Interestingly, at one point I rewrote my code from normal CFML syntax to all be in CFScript, thinking that perhaps multiple <cfset> statements would run faster like this. That used to be the conventional wisdom in CF 5; multiple cfsets should be moved to a cfscript block; but no longer applies in CFMX. However I found it took almost twice as long to run.

October 27, 2008

Project Euler: problem 9

Problem 9:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

This one took a few goes to figure out. Initially I’d made a mistake in my loop, which meant I wasn’t getting back any successful results. Then I had a brainwave why this might be (before I realised my mistake): a and b (and even c) could be negative!

I then wasted time coming up with a clever nested loop that allowed me to generate all the variations of +/- a, b and c. If I’d just spent some time reading up on natural numbers, I’d have discovered they have to be positive.

When that didn’t give me the results I wanted, I went back to my code, realised my earlier mistake and corrected it (I’d not been incrementing a and b properly).

<cfloop index="a" from="1" to="500">
 	<cfloop index="b" from="1" to="500">
	 	<cfset pythagoras = (a * a) + (b * b)>
		<cfset c = Sqr(pythagoras)>
		
		<cfif c EQ Round(c)>
		<!--- it's an integer --->
			<cfset sum = a + b + c>
			<cfif sum EQ 1000>
				<cfset product = a * b * c>
				<cfoutput>
				#a# + #b# + #c# = #sum#<br>
				#a# * #b# * #c# = <strong>#product#</strong>
				</cfoutput>
				<cfabort>
			</cfif> 
		</cfif>
	</cfloop>
</cfloop>

Figure out what a2 + b2 is. Is it an integer? If so, add a, b and c. Do they add up to 1000? If so, multiply them together, and then stop.

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.

October 25, 2008

Project Euler: problem 7

Problem 7:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

Prime numbers again, good job I spent so long trying various things out on problem 3. I just copied the IsPrime function I did then (except removed the custom Mod function and just used CF’s MOD, as we’re dealing with smaller numbers here).

I’m using a brute force method of continually looping, building up an array of primes, until we get to the 10001st one. This time, as we know primes start at 2, but are odd after that, I decided to pre-populate my array with 2, and then loop in steps of 2 from 3 upwards.

<cfset primes = ArrayNew(1)>
<cfset limit = 10001>
<cfset done = 0>
<cfset i = 1>
<cfset ArrayAppend(primes, 2)>

<cfscript>
function isPrime(x)
{
	var isPrime = true;
	var i = 0; 
	
	if (x LT 2)
	{
		return false;
	}
	
	if ((NOT x MOD 2) AND (x GT 2))
	{	// a multiple of 2, but not 2 itself
		return false;
	}
	
	for (i = 3; i LTE SQR(x); i = i + 2)
	{
		if (NOT x MOD i)
		{	// found a factor of x
			return false;
		}
	}
	
	return isPrime;
}
</cfscript>

<cfloop condition="NOT done">
	<cfset i = i + 2>
	
	<cfif IsPrime(i)>
		<cfset ArrayAppend(primes, i)>
		
		<cfif ArrayLen(primes) EQ limit>
			<cfset done = 1>
		</cfif>
	</cfif>
</cfloop>

<cfoutput>
#primes[limit]#
</cfoutput>

October 24, 2008

Project Euler: problem 6

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

Problem 6:

The sum of the squares of the first ten natural numbers is,
12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is,
(1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 – 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

A nice easy one this time. Basically we want to loop from 1 to 100. Use two variables. Sum1 = a sum of the values of 1-100 squared. Sum2 = a sum of the values of 1-100 (not squared). At the end of the loop, square Sum2. Subtract Sum1 from Sum2.

<cfset sum1 = 0>
<cfset sum2 = 0>

<cfloop index="i" from="1" to="100">
	<cfset sum1 = sum1 + (i * i)>
	
	<cfset sum2 = sum2 + i>
</cfloop>

<cfset sum2 = sum2 * sum2>
<cfset difference = sum2 - sum1>

<cfoutput>
	sum1: #sum1#<br>
	sum2: #sum2#<br>
	difference: #difference#<br>
</cfoutput>

The only thing to watch out for is where it asks for “the difference“. I just did a straight subtraction. However, in other problems where it might be harder to guess which of two values would be greater, this should probably be wrapped in the Abs() function, to prevent a negative value being returned.

October 23, 2008

Project Euler: problem 5

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

This was the problem that first attracted my attention to Project Euler, after reading about it on the Daily WTF.

Problem 5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

Rather than try doing it with multiple nested MOD statements, I thought it made more sense to do it properly as a loop. Took me a few goes to get this one right. I kept going into infinite loops because I was stepping too far; the reason I don’t like using while loops.

<cfset found = 0>
<cfset step = 20>
<cfset factor = step - 1>
<cfset i = step>

<cfloop condition="NOT found">
	<cfoutput>#i#<br></cfoutput>
	<cfif NOT i MOD factor>
	<!--- factor goes into i exactly --->
		<hr>
		<!--- it must be a multiple of this number --->
		<cfif factor EQ 11>
			<cfset found = 1>
			<cfbreak>
		</cfif>
		
		<cfset factor = factor - 1>	
		<!--- each successful iteration, step down once --->
		<cfset step = i>
		<cfoutput>step = #step#, factor = #factor#<br></cfoutput>
	<cfelse>
		<cfset i = i + step>
	</cfif>
</cfloop>

<cfoutput><strong>#i#</strong></cfoutput>

There’s a little bit of redundancy in the loop; as I’m using <cfbreak>, I don’t need to also set my ‘found’ flag to true, as the loop will end anyway. In fact the loop might just as well say condition=”1 GT 0″, as I’m wanting to loop forever until we get that last factor.

In the document you can access after solving this problem, it goes into using prime factors and logarithms and various complicated mathematical ways to do the same thing, but I like to keep it simple.

Basically we start at 20 (although I could have started at 2520), incrementing by 20 each time. First try doing MOD 19 (we don’t need to bother with MOD 20 because we already know it’s a multiple of 20). Once we’ve got a number that both 19 and 20 are factors for, we increase our increment from 20 to whatever this number is. Repeat in that fashion until we hit a number that satisfies MOD 11. We don’t need to bother with 1-10, because all those numbers are already factors of numbers from 11-20.

Next Page »

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.