Lillies in my garden:
Is this the best?
Or maybe this?
Here is another Project Euler problem – this time a littlebit more challenging:
“The Fibonacci sequence is defined by the recurrence relation:
It turns out that F_{541 }, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 19 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F_{2749 }, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 19 pandigital. Given that F_{k} is the first Fibonacci number for which the first nine digits AND the last nine digits are 19 pandigital, find k.” 
Does this sound XSLTish? No, not at all.
This solution:

has the following properties:
The index of the first Fibonacci number with the required properties is more than 300 thousand and the number itself has more than 68 thousand digits.
Not satisfied with the speed of this solution? Want even faster? Then here’s something you may consider:

We have added a a couple of lines of code and look what we have now:
As witnessed in the Project Euler thread for this problem (one must have provided the correct answer in order to access this thread), many solutions took longer: from 3.3 seconds, to 3040 minutes, to couple of hours.
One Python programmer even waited for the end of his bruteforce solution … 12 days
Here is another Project Euler problem that seems exactly what XSLT was not intended for:
Find the minimal path sum, in matrix.txt (right click and ‘Save Link/Target As…’), a 31K text file containing a 80 by 80 matrix, from the top left to the bottom right by only moving right and down.
My solution:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:saxon="http://saxon.sf.net/" xmlns:mx="my:my" excluderesultprefixes="xs saxon mx" > <xsl:output omitxmldeclaration="yes" indent="yes"/>
<xsl:variable name="vMatrix" as="element()*"> <xsl:variable name="vLines" as="xs:string*" select="tokenize(unparsedtext(‘matrix.txt’),’\s+’)[.]"/> <xsl:foreach select="$vLines"> <line> <xsl:foreach select="tokenize(.,’,’)"> <v><xsl:valueof select="."/></v> </xsl:foreach> </line> </xsl:foreach> </xsl:variable>
<xsl:variable name="vDimension" as="xs:integer" select="count($vMatrix)"/>
<xsl:template match="/"> <xsl:sequence select="mx:pathminSum(1,1,0)"/> </xsl:template>
<xsl:function name="mx:pathminSum" as="xs:integer" saxon:memofunction="yes"> <xsl:param name="pX" as="xs:integer"/> <xsl:param name="pY" as="xs:integer"/> <xsl:param name="pcurSum" as="xs:integer"/>
<xsl:variable name="curVal" as="xs:integer" select="mx:mtx($pX, $pY)"/>
<xsl:sequence select= "if($pX eq $vDimension and $pY eq $vDimension) then $curVal else for $nextX in min(($vDimension, $pX+1)), $nextY in min(($vDimension, $pY+1)), $s1 in if($nextY gt $pY) then $pcurSum + $curVal + mx:pathminSum($pX, $nextY, $pcurSum) else 999999999999, $s2 in if($nextX gt $pX) then $pcurSum + $curVal + mx:pathminSum($nextX, $pY, $pcurSum) else 999999999999 return min(($s1,$s2)) "/> </xsl:function>
<xsl:function name="mx:mtx" as="xs:integer" saxon:memofunction="yes"> <xsl:param name="pX" as="xs:integer"/> <xsl:param name="pY" as="xs:integer"/>
<xsl:sequence select="$vMatrix[$pY]/*[$pX]"/> </xsl:function> </xsl:stylesheet>

Properties:
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 5digit number, 16807=7^{5}, is also a fifth power. Similarly, the 9digit number, 134217728=8^{9}, is a ninth power.
How many ndigit 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/funcexp.xsl"/> <xsl:output method="text"/> <xsl:template match="/"> 
f:intppow(k, n) is an FXSL 2.x function with positive integer arguments that calculates k^{n.}
^{And if all this is possible even now, imagine yourself working with XSLT 2.1 in the near future… }
In his recent blogarticle: “XPath needs virtual axes. Making XPath more XPathy?” Rick Jelliffe sais:
“I really like XPath2. I would never recommend anyone start with XPath1, unless you were doing very basic transformations with no text processing or data formatting.
But the niggle I have with XPath2 is that it is less XPathy than XPath1. It does not significantly improve the central syntactical feature of XPaths: the location steps. (The only improvement that springs to mind is that XPath2 did improve the use of parentheses in location steps.) Instead, XPath2 provided much more conventional features like a for iterator. I think these significantly decrease the comprehensibility of an XPath, are anonymous and therefore require may comments to explain them, and fracture the line. To an extent, once you start to use nested syntaxes and iterators, why both using XPath at all?”
Rick gives this example:
findrep( findclient(//manager/clients/clientref)/repref)/name
and proposes to improve the expression above by introducing “virtual axes” so that it could be written as:
//manager/clients/clientref/findclient::repref/findrep::rep/name
So, curious reader, pause for a while, don’t read below, and think: has Rick uncovered a hole in XPath?
What you are asking for can be expressed almost exactly in the same form (actually I prefer the current XPath 2.0 form of expression).
You are asking for:
//manager/clients/clientref/findclient::repref/findrep::rep/name
One can write this in XPath 2.0 as:
//manager/clients/clientref/findclient(.)/repref/findrep(.)/name
The current XPath way of expressing this is cleaner — no need for virtual axes.
This is a feature of XPath 2.0 that is not widely used and known: any function can be used as the current location step. The syntax rules that resolve its use are (at http://www.w3.org/TR/2007/RECxpath2020070123/#idgrammar):
[27] StepExpr ==> [38] FilterExpr ==> [41] PrimaryExpr
and the fact that a FunctionCall is a PrimaryExpr:
[41]
PrimaryExpr
::=Literal  VarRef  ParenthesizedExpr  ContextItemExpr  FunctionCall
Also, consider that the axes in XPath have been useful so far mainly because there are just a few of them. Imagine having to deal with zillions of axes and struggling to remember what they mean. And if everyone can introduce their own axes, then why bother with them at all?
Dear reader, it’s up to you to decide… as I have already done.
In Part II of this post a solution was given to the problem presented in Part I:
"Can we implement an algorithm for repetitive prepends that will be run by P1 in linear time, in addition to its excellent linear performance of repetitive appends?"
The solution is implemented within the function my:prependiter() that is defined in the following way:
<xsl:function name="my:prependiter"> <xsl:param name="pNumTimes" as="xs:integer"/> <xsl:param name="pstartSeq" as="element()*"/> <xsl:param name="pInserts" as="element()*"/>
<xsl:sequence select= "if($pNumTimes gt 0) then my:prependiter ($pNumTimes 1, $pstartSeq, ($pInserts, reverse($vPrepends) ) ) else (reverse($pInserts), $pstartSeq) "/> </xsl:function> 
The first argument passed to this function, pNumTimes, is the number of times to perform the prepend operation on the initial sequence pstartSeq, which is passed as the second argument.
The third argument has an auxiliary purpose. As its name, pInserts suggests, it contains the accumulation of all sequences that are to be prepended to pstartSeq.
The idea is to produce the final result only when pNumTimes is zero, using at this time the accumulated prepends in pInserts and the initial sequence pstartSeq.
The idea of the algorithm is to replace the repetitive prepend operations with repetitive append operations, which are carried out by the xslt processor with linear complexity.
We notice that:
y2s ++ y1s ++ xs =
reverse( reverse(y1s) ++ reverse(y2s) ) ++ xs (1)
Or in general, if we have N sequences:
y1s, y2s, …, yNs
to be prepended to an initial sequence xs in that order,
yNs ++ yN1s ++ … y2s ++ y1s ++ xs =
reverse(reverse(y1s)++reverse(y2s) ++ …
… ++ reverse(yNs) ) ++xs (2)
Remarkably, the argument of the outermost reverse() is a repetitive appends operation and it has only linear complexity with P1!
We have added N+1 reverse() operations, but each of them is of linear complexity, so the total complexity of implementing (2) is O(N).
Certainly, the function my:prependiter() we have demonstrated is a simplified case of the many different real world scenarios we may encounter that will require repetitive prepends. Nevertheless its use is sufficient to prove and demonstrate how such an O(N) algorithm can be constructed.
We could use essentially the same algorithm, with a modification that accepts as argument a function
nextprepend(), which produces the next sequence to be prepended. To signal the end of the repetitive prepend operation, we can use a convention that nextprepend() will indicate this by producing some special sequence, such as the empty sequence.
In case you may be wondering how can a function be passed as an argument to another function in XSLT, do read about FXSL.