コラッツ・角谷の問題
Posted feedbacks - XSLT
OS: WindowsVista(Home Premium 32bit) CPU: AMD Turion64X2 (1.6GHz) メモリ: 1.5GB で、 XSLTプロセッサ環境が、 Saxon 9.0.0.6J from Saxonica Java version 1.6.0_06 出力: n=837799, f(n)=524 かかった時間は、 Stylesheet compilation time: 586 milliseconds Execution time: 391187 milliseconds だそうです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="uri:ja.doukaku.org:my-functions"
exclude-result-prefixes="my"
>
<xsl:output method="text" />
<xsl:template match="/" >
<xsl:variable name="collatzresult" as="xs:integer*">
<xsl:for-each select="1 to 1048576">
<xsl:value-of select="my:collatz(.,0)" />
</xsl:for-each>
</xsl:variable>
<xsl:variable name="max" as="xs:integer" select="fn:max($collatzresult)" />
<xsl:text>n=</xsl:text>
<xsl:for-each select="$collatzresult">
<xsl:if test="$max=.">
<xsl:value-of select="fn:position()" />
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>f(n)=</xsl:text>
<xsl:value-of select="$max" />
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:function name="my:collatz" as="xs:integer">
<xsl:param name="n" as="xs:integer" />
<xsl:param name="cnt" as="xs:integer" />
<xsl:choose>
<xsl:when test="$n=1">
<xsl:value-of select="$cnt" />
</xsl:when>
<xsl:when test="($n mod 2)=0" >
<xsl:value-of select="my:collatz($n idiv 2,$cnt+1)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="my:collatz($n*3+1,$cnt+1)" />
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>
|

ところてん
#4969()
Rating2/2=1.00
see: コラッツの問題の成り立つ範囲
[ reply ]