xref: /aosp_15_r20/external/apache-xml/test/tests/contrib/xsltc/schemasoft/eratosthenes.xsl (revision 1212f9a0ffdc28482b8821715d2222bf16dc14e2)
1<?xml version="1.0"?>
2
3<!--
4   The Sieve of Eratosthenes
5   GPL (c) Oliver Becker, 2000-06-13
6   [email protected]
7-->
8
9<xslt:transform xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
10                version="1.0">
11
12<xslt:output method="text" />
13
14<xslt:param name="bound" select="1000" />
15
16<xslt:template match="/">
17   <xslt:call-template name="eratosthenes">
18      <xslt:with-param name="pos" select="2" />
19      <xslt:with-param name="array">
20         <xslt:call-template name="init-array">
21            <xslt:with-param name="length" select="$bound" />
22         </xslt:call-template>
23      </xslt:with-param>
24   </xslt:call-template>
25   <xslt:text>&#xA;</xslt:text>
26</xslt:template>
27
28
29<!-- Initialize the array (string) with length $length -->
30<xslt:template name="init-array">
31   <xslt:param name="length" />
32   <xslt:if test="$length &gt; 0">
33      <xslt:text>-</xslt:text>
34      <xslt:call-template name="init-array">
35         <xslt:with-param name="length" select="$length - 1" />
36      </xslt:call-template>
37   </xslt:if>
38</xslt:template>
39
40
41<!-- Sieve of Eratosthenes: If the number at position $pos isn't
42     marked then it's a prime (and printed). If the position of the
43     prime is lower or equal then the square root of $bound then the
44     new array will be computed by marking all multiples of $pos. -->
45<xslt:template name="eratosthenes">
46   <xslt:param name="array" />
47   <xslt:param name="pos" />
48   <xslt:if test="$pos &lt; $bound">
49      <xslt:variable name="is-prime"
50                     select="substring($array,$pos,1) = '-'" />
51      <xslt:if test="$is-prime">
52         <xslt:value-of select="$pos" />, <xslt:text />
53      </xslt:if>
54      <xslt:variable name="new-array">
55         <xslt:choose>
56            <xslt:when test="$is-prime and $pos*$pos &lt;= $bound">
57               <xslt:call-template name="mark">
58                  <xslt:with-param name="array" select="$array" />
59                  <xslt:with-param name="number" select="$pos" />
60               </xslt:call-template>
61            </xslt:when>
62            <xslt:otherwise>
63               <xslt:value-of select="$array" />
64            </xslt:otherwise>
65         </xslt:choose>
66      </xslt:variable>
67      <xslt:call-template name="eratosthenes">
68         <xslt:with-param name="array" select="$new-array" />
69         <xslt:with-param name="pos" select="$pos + 1" />
70      </xslt:call-template>
71   </xslt:if>
72</xslt:template>
73
74
75<!-- Mark all multiples of $number in $array with '*' -->
76<xslt:template name="mark">
77   <xslt:param name="array" />
78   <xslt:param name="number" />
79   <xslt:choose>
80      <xslt:when test="string-length($array) &gt; $number">
81         <xslt:value-of select="substring ($array, 1, $number - 1)" />
82         <xslt:text>*</xslt:text>
83         <xslt:call-template name="mark">
84            <xslt:with-param name="array"
85                             select="substring ($array, $number + 1)" />
86            <xslt:with-param name="number" select="$number" />
87         </xslt:call-template>
88      </xslt:when>
89      <xslt:otherwise>
90         <xslt:value-of select="$array" />
91      </xslt:otherwise>
92   </xslt:choose>
93</xslt:template>
94
95  <!--
96   * Licensed to the Apache Software Foundation (ASF) under one
97   * or more contributor license agreements. See the NOTICE file
98   * distributed with this work for additional information
99   * regarding copyright ownership. The ASF licenses this file
100   * to you under the Apache License, Version 2.0 (the  "License");
101   * you may not use this file except in compliance with the License.
102   * You may obtain a copy of the License at
103   *
104   *     http://www.apache.org/licenses/LICENSE-2.0
105   *
106   * Unless required by applicable law or agreed to in writing, software
107   * distributed under the License is distributed on an "AS IS" BASIS,
108   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
109   * See the License for the specific language governing permissions and
110   * limitations under the License.
111  -->
112
113</xslt:transform>
114