Fun with Project Euler

I have been having fun solving some Project Euler problems. Most of my 76 solutions were done in XSLT 2.0 last year.

For those who are busy arguing whether or not  XSLT is a programming language, here is one easy problem:

The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is a ninth power.

How many n-digit positive integers exist which are also an nth power?

Below is the solution, and it takes 20ms to run with Saxon 9.1.07:

<xsl:stylesheet version="2.0"
xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:f=http://fxsl.sf.net/>

<xsl:import href="../../../f/func-exp.xsl"/>

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:sequence select=
   "
(: count( :)
       for $k in 1 to 9,
           $n in 1 to 23,
           $k-pwr-n in f:intppow($k,$n),
           $low in f:intppow(10,$n -1) -1,
           $high in f:intppow(10,$n)
        return
          if($k-pwr-n gt $low and $k-pwr-n lt $high)
           then ($k, $n, $k-pwr-n, ‘
‘)
           else()
   (: ) :)
   "/>
</xsl:template>
</
xsl:stylesheet>

 

f:intppow(k, n)  is an FXSL 2.x function with positive integer arguments that calculates kn.

And if all this is possible even now, imagine yourself working with XSLT 2.1 in the near future…

Advertisements
This entry was posted in Project Euler. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s