Re: [xsl] A small programming challenge

Subject: Re: [xsl] A small programming challenge
From: James Fuller <jim.fuller@xxxxxxxxxxxxxx>
Date: Wed, 22 Jun 2005 12:09:06 +0200
Dimitre Novatchev wrote:

>Hi,
>
>I just started reading the book "The Da Vinci code" and this resulted
>in having some fun with XSLT 2.0, Saxon 8.4 and Saxon.NET.
>
>Here's the problem:
>
>    Find the first 10-digit prime in consecutive digits of F-3000						
> 	
>    Where F-N is the Nth Fibonacci number.
>    Here are the elements of the sequence of Fibonacci numbers from 0 to 11:
>                                                                            
>    Element:    1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144                   
>    Index:         0, 1, 2, 3, 4, 5,   6,   7,  8,   9,   10,  11
>                                                                            
>
>It is *very important* to note that the first "1" in the sequence has
>index "0", so that everyone will mean the same thing by F-3000
> 
>When you have the answer, please, let me know:
>
>  1. The 10 digit prime number
>  2. The xslt code
>  3. The stylesheet execution time.
>  4. Your computer -- CPU speed and RAM
>  5. Whether or not you used any specific extension functions.
>  
>
u are aware of OliverB old stylesheet....


<!-- 
   The Sieve of Eratosthenes
   GPL (c) Oliver Becker, 2000-06-13
   obecker@xxxxxxxxxxxxxxxxxxxxxxx
-->
<xslt:transform xmlns:xslt="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xslt:output method="text" />

<xslt:param name="bound" select="1000" />

<xslt:template match="/">
   <xslt:call-template name="eratosthenes">
      <xslt:with-param name="pos" select="2" />
      <xslt:with-param name="array">
         <xslt:call-template name="init-array">
            <xslt:with-param name="length" select="$bound" />
         </xslt:call-template>
      </xslt:with-param>
   </xslt:call-template>
   <xslt:text>
</xslt:text>
</xslt:template>


<!-- Initialize the array (string) with length $length -->
<xslt:template name="init-array">
   <xslt:param name="length" />
   <xslt:if test="$length &gt; 0">
      <xslt:text>-</xslt:text>
      <xslt:call-template name="init-array">
         <xslt:with-param name="length" select="$length - 1" />
      </xslt:call-template>
   </xslt:if>
</xslt:template>


<!-- Sieve of Eratosthenes: If the number at position $pos isn't 
     marked then it's a prime (and printed). If the position of the
     prime is lower or equal then the square root of $bound then the 
     new array will be computed by marking all multiples of $pos. -->
<xslt:template name="eratosthenes">
   <xslt:param name="array" />
   <xslt:param name="pos" />
   <xslt:if test="$pos &lt; $bound">
      <xslt:variable name="is-prime" select="substring($array,$pos,1) =
'-'" />
      <xslt:if test="$is-prime">
         <xslt:value-of select="$pos" />, <xslt:text />
      </xslt:if>
      <xslt:variable name="new-array">
         <xslt:choose>
            <xslt:when test="$is-prime and $pos*$pos &lt;= $bound">
               <xslt:call-template name="mark">
                  <xslt:with-param name="array" select="$array" />
                  <xslt:with-param name="number" select="$pos" />
               </xslt:call-template>
            </xslt:when>
            <xslt:otherwise>
               <xslt:value-of select="$array" />
            </xslt:otherwise>
         </xslt:choose>
      </xslt:variable>
      <xslt:call-template name="eratosthenes">
         <xslt:with-param name="array" select="$new-array" />
         <xslt:with-param name="pos" select="$pos + 1" />
      </xslt:call-template>
   </xslt:if>
</xslt:template>


<!-- Mark all multiples of $number in $array with '*' -->
<xslt:template name="mark">
   <xslt:param name="array" />
   <xslt:param name="number" />
   <xslt:choose>
      <xslt:when test="string-length($array) &gt; $number">
         <xslt:value-of select="substring ($array, 1, $number - 1)" />
         <xslt:text>*</xslt:text>
         <xslt:call-template name="mark">
            <xslt:with-param name="array" select="substring ($array,
$number + 1)" />
            <xslt:with-param name="number" select="$number" />
         </xslt:call-template>
      </xslt:when>
      <xslt:otherwise>
         <xslt:value-of select="$array" />
      </xslt:otherwise>
   </xslt:choose>
</xslt:template>

</xslt:transform>


this will take quite a long time...though uses no extension functions, I
would utilize other approaches to search the problem domain if simply
contrained by the requirement of being prime and 10 digit length.

wish I had the time, though will process in the background on a solution
to this.

cheers, Jim Fuller

Current Thread