2016-05-05-lightsword安装中遇到的坑

2016-05-05-lightsword安装中遇到的坑

lightsword是一个基于 Node.js 的 SOCKS5 代理 / Apple NE 服务器。LightSword 参考了 Shadowsocks 的协议,并用 Typescript 实现。想必大家已经知道这货是干嘛的了。项目的主页在这里:https://github.com/UnsignedInt8/LightSword 。

之前看到一篇介绍lightsword的文章,刚好上午没啥事,我就折腾了一下,遇到两个坑,这里说一下。

1.服务器
我服务器用的是ubuntu, 作者提供了一个一键安装脚本.
curl -sL https://raw.githubusercontent.com/UnsignedInt8/LightSword/master/misc/onekey_ubuntu.sh | bash –
但是这个脚本执行会报错,我直接打开那个脚本,里面内容是
#!/bin/bash
apt-get update -y
apt-get install curl -y
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash –
apt-get install -y nodejs
npm install lightsword -g
我直接在前面加一个sudo,就ok了。所以正确命令应该是
curl -sL https://raw.githubusercontent.com/UnsignedInt8/LightSword/master/misc/onekey_ubuntu.sh | sudo -E bash –
安装完成之后执行
lsserver -f -m aes-256-cfb -p 4433 -k your_password
就ok了。

2.本地
我本地是mac, 直接brew install node && npm install lightsword -g就安装好了。
我看到https://github.com/UnsignedInt8/LightSword/tree/master/client 这里有一个json文件,我就执行了lslocal -c config.json, 结果报错,我仔细一看作者提供的那个json文件里面倒数第二行是
“dontbypasslocal”, false
坑爹,这里面应该是
”dontbypasslocal”: false,
改正之后重新执行
lslocal -c config.json
就没有问题了。
后来我又仔细看了一下lsserver和lslocal的manual, 其时本地也不需要config.json文件, 直接执行
lslocal -f -m aes-256-cfb -s your_host_ip -p 4433 -k your_password -l 1081
就ok了。其中的your_host_ip填你自己的服务器的ip地址,your_password填你的密码。

就这样,lightsword就可以了,万一shaowsocks挂了,你还可以用这个做备用方案。

2014-11-07-XSLT_Tips

XSLT usage and performance tips

Eight tips for how to use XSLT efficiently:

  • Keep the source documents small. If necessary split the document first.
  • Keep the XSLT processor (and Java VM) loaded in memory between runs
  • If you use the same stylesheet repeatedly, compile it first.
  • If you use the same source document repeatedly, keep it in memory.
  • If you perform the same transformation repeatedly, don’t. Store the result instead.
  • Keep the output document small. For example, if you’re generating HTML, use CSS.
  • Never validate the same source document more than once.
  • Split complex transformations into several stages.

Eight tips for how to write efficient XSLT:

  • Avoid repeated use of “//item”.
  • Don’t evaluate the same node-set more than once; save it in a variable.
  • Avoid xsl:number if you can. For example, by using position().
  • Use xsl:key, for example to solve grouping problems.
  • Avoid complex patterns in template rules. Instead, use xsl:choose within the rule.
  • Be careful when using the preceding[-sibling] or following[-sibling] axes. This often indicates an algorithm with n-squared performance.
  • Don’t sort the same node-set more than once. If necessary, save it as a result tree fragment and access it using the node-set() extension function.
  • To output the text value of a simple #PCDATA element, use xsl:value-of in preference to xsl:apply-templates.

XSLT Best Practices

XSLT (Extensible Stylesheet Language Transformations) is a functional language for transforming XML documents into another file structure such as plain text, HTML, XML, etc. XSLT is available in multiple versions, but version 1.0 is the most commonly used version. XSLT is extremely fast at transforming XML and does not require compilation to test out changes. It can be debugged with modern debuggers, and the output is very easy to test simply by using a compare tool on the output. XSLT also makes it easier to keep a clear separation between business and display logic.

Uses

XSLT has numerous uses. XML is easy to generate and can easily be transformed to the desired layout of other systems. Many older EDI systems need to receive data in a fixed, flat file format. One such example of a fixed file format is the ABA file format used in the banking industry of Australia. XSLT can be used to transform your data source to a flat file format for another system to consume, and that same data source can then be used to transform the data into HTML for display in a web browser. In fact, it’s even possible to use XSLT to build an XSLT view engine for use with MVC to render content.

Another use for XSLT is creating dynamic documents in various formats such as Word, Excel, and PDF. Starting with Office 2003, Microsoft began supporting the WordML and ExcelML data formats. These data formats are XML documents that represent a Word document or an Excel spreadsheet. Data from a database can be easily transformed into either of these formats through the use of XSLT. In addition, the same data source can also be transformed into XSL-FO to create PDF documents.

Besides the two uses above, you may want to consider using XSLT whenever you are working with templates, when you are working with XML data, or when you are working with static data that doesn’t need to live in a database. An example of a template would be an email newsletter that gets sent out and is “mail-merged” with data from the database.

Of course there are times that you could use XSLT to accomplish a programming task, but it might not be the right choice. For instance, it might be easier to use LINQ to access data from an object hierarchy and then use a StringBuilder to build output rather than to use an XSLT to do the same thing. An XSLT might also not be appropriate for generating output if you need to do a large amount of string manipulation. Having to use certain string functions like replace or split are not as easy to accomplish in XSLT as they are in languages like C#.

Basics

Assuming that XSLT is the right solution for the task you are trying to accomplish, there are several basic things that a developer needs to be aware of. The first thing to remember is that XSLT is a functional language. Once a variable is set it cannot be changed. In order to change a value, you need to setup a template that you can call recursively. The following is an example of what that code might look like:

<xsl:template name="pad-left">
    <xsl:param name="totalWidth"/>
    <xsl:param name="paddingChar"/>
    <xsl:param name="value"/>
    <xsl:choose>
        <xsl:when test="string-length($value) &lt; $totalWidth">
            <xsl:call-template name="pad-left">
                <xsl:with-param name="totalWidth">
                    <xsl:value-of select="$totalWidth"/>
                </xsl:with-param>
                <xsl:with-param name="paddingChar">
                    <xsl:value-of select="$paddingChar"/>
                </xsl:with-param>
                <xsl:with-param name="value">
                    <xsl:value-of select="concat($paddingChar, $value)"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$value"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

The template above performs the equivalent function of the pad left function in .Net. The pad-left template takes in three parameters. It then checks to see if the length of the value passed in is less than the total length specified. If the length is less then the template calls itself again passing in the value passed to the function concatenated with the padding character and the desired length. This process is repeated until the value passed into the template is greater than or equal to the string length passed into the template.

Another important thing to know when working with XSLT is that namespaces affect how you select data from XML. For instance, let’s say you’re working with XML that starts with the following fragment:

<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">

In order to select data from this XML document, you need to include a reference to the namespace(s) used in the XML document that you are consuming in your XSLT. For the example above you would do something like this:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:fm="http://www.filemaker.com/fmpxmlresult"
    exclude-result-prefixes="msxsl fm">

<xsl:template match="fm:FMPXMLRESULT">
    <xsl:apply-templates select="fm:RESULTSET" />
</xsl:template>

The last area I would like to focus on is the use of templates. XSLT provides two techniques for accessing data. The push approach, as the name implies, pushes the source XML to the stylesheet, which has various templates to handle variable kinds of nodes. Such an approach makes use of several different templates and applies the appropriate template for a given node through the use of the xsl:apply-templates command. An example of this is as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="Orders">
        <html>
            <body>
                <xsl:apply-templates select="Invoice"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Invoice">
        <xsl:apply-templates select="CustomerName" />
        <p>
            <xsl:apply-templates select="Address" />
            <xsl:apply-templates select="City" />
            <xsl:apply-templates select="State" />
            <xsl:apply-templates select="Zip" />
        </p>
        <table>
            <tr>
                <th>Description</th>
                <th>Cost</th>
            </tr>
            <xsl:apply-templates select="Item" />
        </table>
        <p />
    </xsl:template>
    <xsl:template match="CustomerName">
        <h1><xsl:value-of select="." /></h1>
    </xsl:template>
    <xsl:template match="Address">
        <xsl:value-of select="." /><br />
    </xsl:template>
    <xsl:template match="City">
        <xsl:value-of select="." />
        <xsl:text>, </xsl:text>
    </xsl:template>
    <xsl:template match="State">
        <xsl:value-of select="." />
        <xsl:text> </xsl:text>
    </xsl:template>
    <xsl:template match="Zip">
        <xsl:value-of select="." />
    </xsl:template>
    <xsl:template match="Item">
        <tr>
            <xsl:apply-templates />
        </tr>
    </xsl:template>
    <xsl:template match="Description">
        <td><xsl:value-of select="." /></td>
    </xsl:template>
    <xsl:template match="TotalCost">
        <td><xsl:value-of select="." /></td>
    </xsl:template>
    <xsl:template match="*">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="text()" />
</xsl:stylesheet>

The pull approach on the other hand makes minimal use of xsl:apply-template instruction and instead pulls the xml through the transform with the use of the xsl:for-each and xsl:value-of instructions. Using the pull technique, the above template would look something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="Orders">
        <html>
            <body>
                <xsl:for-each select="Invoice">
                    <h1>
                        <xsl:value-of select="CustomerName" />
                    </h1>
                    <p>
                        <xsl:value-of select="Address" /><br />
                        <xsl:value-of select="City" />
                        <xsl:text>, </xsl:text>
                        <xsl:value-of select="State" />
                        <xsl:text> </xsl:text>
                        <xsl:value-of select="Zip" />
                    </p>
                    <table>
                        <tr>
                            <th>Description</th>
                            <th>Cost</th>
                        </tr>
                        <xsl:for-each select="Item">
                            <tr>
                                <td><xsl:value-of select="Description" /></td>
                                <td><xsl:value-of select="TotalCost" /></td>
                            </tr>
                        </xsl:for-each>
                    </table>
                    <p />
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

You can read more about these two approaches at http://www.xml.com/pub/a/2005/07/06/tr.html and http://www.ibm.com/developerworks/library/x-xdpshpul.html.

Best Practices

While XSLT is extremely fast and powerful, there are several rules to keep in mind in order to write quality code. They are as follows:

  • Avoid the use of the // near the root of the document especially when transforming very large XML document. The // selector selects nodes in the document from the current node that match the selection no matter where they are in the document. It is best to avoid using the // operator all together if possible. More scanning of the XML document is required which makes transforms take longer and makes them less efficient.
  • Avoid the use of very long xpath queries (i.e. more than a screen width long). It makes the XSLT logic difficult to read.
  • Set the indent attribute in the output declaration to off when outputting XML or HTML. Not only will this reduce the size of the file you generate, but it will also decrease the processing time.
  • Try to use template matching (push method) instead of named templates (pull method). Named templates are fine to use for utility functions like the padding template listed above. However, template matching will create cleaner and more elegant code.
    Make use of built in XSLT functions whenever possible. A good example of this is when you are trying to concatenate strings. One approach to accomplish this would be to utilize several xsl:value-of instructions. However, it is much cleaner to use the xsl concat() function instead.
  • If you are transforming a large amount of data through .Net code you should utilize the XmlDataReader and XmlDataWriter classes. If you try and use the XmlDocument class to read in your XML and the StringBuilder class to write out your XML you are likely to get an Out of Memory exception since data must be loaded in one continuous memory block.

Additional best practices can be found here:

http://www.xml.org//sites/www.xml.org/files/xslt_efficient_programming_techniques.pdf

XSLT Tips for Cleaner Code and Better Performance

Conclusion

There are many times to consider using XSLT. The language tends to be verbose and at times it can feel unnatural to program in if you are more accustomed to a procedural programming style. However, it is a flexible and powerful language that with a little time can be easy to pick up and learn. There are debugging and profiling tools available to make the development process easier. In addition, changes to an XSLT does not require compilation in order to test, which can easily be done by comparing output with a compare tool such as Araxis Merge.

XSLT Tips for Cleaner Code and Better Performance

On this page:

  • Avoid XSLT Named Templates; Use Template Match
  • Avoid xsl:for-each; Use Template Match
  • You don’t have to use xsl:element or xsl:attribute
  • Use the element name itself rather than xsl:element
  • Use the { } shorthand for writing values inside of attributes
  • Use template modes
  • Use in-built functions: concat()
  • Use in-built functions: boolean()
  • Use in-built functions: string()
  • Use in-built functions: number()
  • Use in-built functions: other
  • More tips

XSLT is a transformation language to convert XML from one format to another (or to another text-based output).

People seem to love or hate XSLT. Some find it hard to read or strange to get used to. Yet, it can be quite elegant when coded right. So this will be the first in a series of posts to show where it can be useful (and what its pitfalls/annoyances may be), how to make best use of XSLT, etc.

This first post looks at coding style in XSLT 1.0 and XPath 1.0.

I think some frustrations at this technology come from wanting to do procedural programming with it, whereas it is really more like a functional programming language; you define what rules to act against, rather than how to determine the rules (kind of).

For example, consider the following example where a named template may be used to create a link to a product:

<xsl:template name="CreateLink">
  <xsl:param name="product" />
  <xsl:element name="a">
    <xsl:attribute name="href">
      <xsl:value-of select="'/product/?id='" /><xsl:value-of select="normalize-space($product/@id)" />
    <xsl:value-of select="$product/name" />
  </xsl:element>
</xsl:template>

I have found the above to be a common way people initially code their XSLTs. Yet, the following is far neater:

<xsl:template match="product">
  <a href="{concat('/product/?id=', normalize-space(./@id))}">
    <xsl:value-of select="./@name" />
  </a>
</xsl:template>

Not only does such neater coding become easier to read and maintain, but it can even improve performance.

(Update: As Azat rightly notes in a comment below the use of ‘./’ is redundant. That is definitely true. I should have added originally that I tend to use that to help others in the team, especially those newer to XSLT to understand the context of which element your template is running under a bit more clearly.)

Lets look at a few tips on how this may be possible (a future post will concentrate on additional performance-related tips; the tips below are primarily on coding style):

Avoid XSLT Named Templates; Use Template Match

The first coding practice that leads to code bloat and hard to read XSLT is using named templates everywhere. Named templates give a procedural feel to coding. (You define templates with names, pass parameters as needed and do some stuff). This may feel familiar to most coders, but it really misses the elegance and flexibility of XSLT.

So, instead of this:

<xsl:template name="CreateLink">
  <xsl:param name="product" />
  <-- create the link here based on the product parameter -->
</xsl:template>

<-- The above would be called from elsewhere using this: -->
<xsl:call-template name="CreateLink"<>
  <xsl:with-param name="product" select="./product" />
</xsl:call-template>

Far neater would be this:

<xsl:template match="product">
  <-- create the link here based on the product parameter -->
</xsl:template>

<-- The above would be called from elsewhere using this: -->
<xsl:apply-templates select="./product" />

The above example doesn’t look like much on its own. When you have a real stylesheet with lots of template matches, (and modes, which we look at later) this gets a lot easier to read, and cuts a LOT of code, especially when calling/applying these templates.

(Of course, each tip has exceptions; named templates can be useful for utility functions. Sometimes XSLT extension objects can be useful for that too, depending on your parser and runtime requirements. A subsequent post on XSLT performance tips will cover that.)

Avoid xsl:for-each; Use Template Match

xsl:for-each is another programming construct that would appeal to many coders. But again, it is rarely needed. Let the XSLT processor do the looping for you (it has potential to be optimised further, too).

There are some instances or XSLT parsers that may perform a bit quicker using xsl:for-each because for-each avoids the XSLT processor having to determine which of possibly many matched templates is the suitable one to execute. However, matched templates that use modes can overcome those issues to most extents, and lend to highly elegant, reusable XSLT.

You don’t have to use xsl:element or xsl:attribute

You can use xsl:element and xsl:attribute, but it leads to very bloated code.

Here are a few examples of what you can do instead. In each example we will just assume we are working with some XML that represents some kind of product (it is not important what this structure is for this discussion).

Use the element name itself rather than xsl:element

Instead of

<xsl:element name="p">
  <xsl:value-of select="$product/name" />
</xsl:element>

This is a lot cleaner to read:

<p>
  <xsl:value-of select="$product/name" />
</p>

Sometimes I prefer this:

<p><xsl:value-of select="$product/name" /></p>

Use the { } shorthand for writing values inside of attributes

Using xsl:value-of for many attributes can get verbose very quickly. There is more code to read. So the code just looks uglier and more bloated. For attributes only then, with most XSLT parsers, you can use the shorthand { as a replacement for .

In between { and } you just put in your normal select expression.

So, instead of

<h3>
    <xsl:attribute name="class">
        <xsl:value-of select="$product/@type" />
    </xsl:attribute>
    <xsl:value-of select="$product/name" />
</h3>

This is a lot cleaner to read:

<h3 class="{$product/name}">
  <xsl:value-of select="$product/name" />
</h3>

Or, instead of

<xsl:element name="img">
    <xsl:attribute name="src" select="$product/image/@src" />
    <xsl:attribute name="width" select="$product/image/@width" />
    <xsl:attribute name="height" select="$product/image/@height" />
    <xsl:attribute name="alt" select="$product/image" />
    <xsl:attribute name="class" select="$product/@type" />
</xsl:element>

This is a lot cleaner to read:

<img
    src="{$product/image/@src}"
    width="{$product/image/@width}"
    height="{$product/image/@height}"
    alt="{$product/image}"
    class="{$product/@type}"
    />

The above is only put onto multiple lines for this web page. In a proper editor sometimes a one-liner is even easier to read:

<img src="{$product/image/@src}" width="{$product/image/@width}" height="{$product/image/@height}" alt="{$product/image}" class="{$product/@type}" />

The above is also looking a lot like some templating languages now, and you might see why I am wondering why there are so many proprietary ones people have to learn, when XSLT is an open, widely supported, standard with transferable skills!

The above also doesn’t show how clean the code would really be, because someone using xsl:attribute is likely to use xsl:element as well, so really we should compare the legibility of this:

<xsl:element name="h3">
    <xsl:attribute name="class">
        <xsl:value-of select="$product/@type" />
    </xsl:attribute>
    <xsl:value-of select="$product/name" />
</xsl:element>

… versus this:

<h3 class="{$product/name}">
    <xsl:value-of select="$product/name" />
</h3>

Use template modes

Often, you will want to use a template match for totally different purposes. Rather than pass unnecessary parameters or resort to different named templates, a mode attribute on the template can do the trick.

For example, suppose you are showing an order history for some e-commerce site. Suppose you want a summary of orders at the top that anchor to the specific entries further down the page.

You can have more than one template have the same match, and use mode to differentiate or indicate what they are used for.

Consider this example. First, here is a starting point in the XSLT. The idea is to reuse the Orders element, one for summary purpose, the next for details.

<!-- starting point -->
<xsl:template match="/">
    <h1>Order summary</h1>
    <h2>Summary of orders</h2>
    <p><xsl:apply-templates select="./Orders" mode="summary-info" /></p>
    <h2>Table of orders</h2>
    <xsl:apply-templates select="./Orders" mode="order-summary-details" />
</xsl:template>

Next, we match Orders with the summary-info mode:

<xsl:template match="Orders" mode="summary-info">
    <xsl:value-of select="concat(count(./Order), ' orders, from ', ./Order[1]/@date, ' to ', ./Order[last()]/@date)" />
</xsl:template>

We can also match Orders for the order-summary-details mode. Note how the variable has also re-used the other mode to get the summary for the table’s summary attribute.

<xsl:template match="Orders" mode="order-summary-details">
    <xsl:variable name="summary">
        <xsl:apply-templates select="." mode="summary-info" />
    </xsl:variable>
    <table summary="{normalize-space($summary)}">
        <thead>
            <tr>
                <th scope="col">Order number</th>
                <th scope="col">Amount</th>
                <th scope="col">Status</th>
            </tr>
        </thead>
        <tbody>
            <xsl:apply-templates select="./Order" mode="order-summary-details" />
        </tbody>
    </table>
</xsl:template>

Note how the same mode name can be used for additional matches. This is a neat way to keep related functionality together:

<xsl:template match="Order" mode="order-summary-details">
    <tr>
        <td><a href="/order/details/?id={./@id}"><xsl:value-of select="./@id" /></a></td>
        <td><xsl:value-of select="./amount" /></td>
        <td><xsl:value-of select="./status" /></td>
    </tr>
</xsl:template>

In many real XSLTs I have written these modes can be re-used many times over. They help with performance, while maintaining this elegance/reduction of code because the XSLT processor can use that to narrow down which possible template matches to select from when looking for the one to execute.

The use of modes (and other features such as importing other XSLTs and overriding moded templates) has allowed us to create multiple sub-sites in parallel (e.g. an ecommerce site that sells books, entertainment products (CDs, DVDs, computer games, etc) that all run off the same XSLTs with some minor customisation in each sub-site. Although the actual data is different, they fall into the same XML structure — they are products after all! — thus making the XSLTs highly reusable. A future post will describe arranging XSLTs in an almost object-oriented fashion).

Use in-built functions: concat()

The concat() function allows you to remove unnecessary and excessive uses of statements one after the other (and with the accompanying xsl:text /xsl:text type of trick to get a white space in there).

Code looks easier to read, in most cases, and typically performs better too.

Example:

Instead of this:

<xsl:value-of select="$string1" /><xsl:text> </xsl:text><xsl:value-of select="$string2" />

This is much cleaner to read:

<xsl:value-of select="concat($string1, ' ', $string2)" />

Or,

Instead of this:

<a>
    <xsl:attribute name="href">
        <xsl:value-of select="$domain" />/product/?<xsl:value-of select="$someProductId" />
    </xsl:attribute>
    <xsl:value-of select="$productDescription" />
</a>

This is much cleaner to read:

<a href="{concat($domain, '/product/?', $someProductId}">
    <xsl:value-of select="$productDescription" />
</a>

Storing a string resulting from a concat into a variable is also efficient from a performance point of view (storing node-sets does not cache the result, as in most DOM and XSLT implementations, node-sets are live collections. More on that in a future post).

(Update: Azat notes in a comment below that the above href attribute can be even further simplified into this: href=”{$domain}/product/?{$someProductId}”.)

Use in-built functions: boolean()

How many times have we seen code like this:

<xsl:if test="$new = 'true'"> ... </xsl:if>

While it works, it is not ideal using string comparison, especially if this kind of test is going to be repeated in a template.

It would be better to create a variable using this kind of syntax:

<xsl:variable name="isNew" select="boolean($new = 'true')" />

Then, in your code, when you need to use it, you can do things like:

<xsl:if test="$isNew"> ... </xsl:if>

or

<xsl:if test="$isNew = true()"> ... </xsl:if>

or

<xsl:if test="$isNew = false()"> ... </xsl:if>

or

<xsl:if test="not($isNew)"> ... </xsl:if>

These above variations are down to style/preference, but is better from a coding perspective than constant testing of strings. (Sometimes the calculation of what true or false means may require testing many values, such as true, True, 1, Y, etc. This can all be hidden away in that one variable declaration, and the rest of the code is unchanged.)

(Update: Azat rightly notes in a comment below that the variable declaration can be made smaller by omitting the actual boolean function so it is just this: . I find the explicit use of boolean can aid with readability, especially for those new to XSLT so might be useful to retain under such situations.)

Use in-built functions: string()

Instead of this:

<xsl:variable name="mystring">my text</variable>

Consider this:

<xsl:variable name="mystring" select="'my text'" />

Or this:

<xsl:variable name="mystring" select="string('my text')" />

Or, more importantly, instead of this:

<xsl:variable name="bookTitle"><xsl:value-of select="./title" /></xsl:variable>

Consider this:

<xsl:variable name="mystring" select="string(./title)" />

Why?

Code is cleaner to read.

But it is also more optimal; casting to a string instead of storing the node will result in the variable value being cached in most XSLT processors, rather than being re-evaluated each time it is accessed. (XML nodes are live collections according to W3C which means they may change. Hence references to nodes require evaluation each time they are accessed.)

Use in-built functions: number()

For similar reasons as above to use string(), number() should be used too.

Use in-built functions: other

XPath functions such as starts-with(), string-length() are handy.

For example, it is common to see code to test for the presence of strings by testing if a variable equals the empty string (”). But as most programmers should know, it is more efficient to test for the presence of a string by testing its length. In XPath expressions you can use string-length() function for this.

For more information and full list of XPath functions, consider the following:

  • The XPath 1.0 Specification from the W3C
  • The MSDN XPath reference from Microsoft (Same as the W3C information, of course, but has useful examples)
  • Documentation from Stylus (also has some useful examples)

More tips

The above is about XPath 1.0 and XSLT 1.0. Even with the above tips, some XSLT can require more code than ideal, which XSLT 2.0 and XPath 2.0 help to address. The features in those are very useful for sure, but not as widely implemented as 1.0. My experiences are almost entirely in 1.0 which we use in live, production/run-time environments.

Here are a some additional useful tips:

  • There are Monsters in My Closet or How Not to Use XSLT by R. Alexander Milowski from the School of Information Management and Systems, Berkeley
  • XSLT by Example is a blog of XSLT examples by Miguael deMelo

Do you have any useful tips to augment/improve the above? Let me know and I will add them above

Be Sociable, Share!

2013-03-13-html5设计原理

今天看了一篇文章,是Jeremy Keith在 Fronteers 2010 上的主题演讲
(http://adactio.com/articles/1704/)的全文翻译
(http://www.cn-cuckoo.com/2010/10/21/the-design-of-html5-2151.html)。

这是一篇很好的文章,因为里面主要讲的是设计原理,
(文章开头就提到“设计原理本质上是一种信念、一种想法、一个概念,是你行动的支柱。”)
而且有很多很有意思的设计原理方面的格言:
1.美国的设计原理:
我们认为这些真理是不言而喻的,人人生而平等,
造物主赋予了每个人不可剥夺的权利,包括生存、自由和追求幸福。
We hold these Truths to be self-evident, that all Men are created equal,
that they are endowed by their Creator with certain unalienable Rights,
that among these are Life, Liberty and the pursuit of Happiness.
2.卡尔·马克思的社会主义设计原理:各尽所能,各取所需。
Karl Marx’s design principle for socialism:
From each according to his ability, to each according to his need.
3.圣经:人人为我,我为人人。
Jesus Christ: Do unto others as you would have them do unto you.
4.动物庄园设计原理:四条腿的都是好人,两条腿的都是坏蛋!
Animal Farm’s design principle was: four legs good, two legs bad.
5.阿西莫夫机器人三大法则:
机器人不得伤害人类,或袖手旁观人类受伤害。
机器人必须服从人类命令,除非命令违反第一法则。
机器人必须自卫,只要不违背第一和第二法则。
Isaac Asimov three laws of robotics:
A robot may not injure a human being or,
through inaction, allow a human being to come to harm.
A robot must obey any orders given to it by human beings,
except where such orders would conflict with the First Law.
A robot must protect its own existence as long as such protection
does not conflict with the First or Second Law.
6.伯斯塔尔法则:发送时要保守;接收时要开放。
Postel’s Law: Be conservative in what you send; be liberal in what you accept.
7.html5: 避免不必要的复杂性。
html5: Avoid needless complexity.
8.html5: 支持已有的内容。
html5: Support existing content.
9.html5: 解决现实的问题。
html5: Solve real problems.
10.html5: 求真务实。
html5: Pave the cowpaths.
11.html5: 平稳退化。
html5: Degrade gracefully.
12.梅特卡夫定律:网络价值同网络用户数量的平方成正比。
Metcalfe’s Law:
The value of a network is proportional to the square of the number of connected users of the system.
13.html5: 最终用户优先。
一旦遇到冲突,最终用户优先,其次是作者,其次是实现者,
其次标准制定者,最后才是理论上的完满。
html5: Priority of constituencies.
In case of conflict, consider users over authors
over implementors over specifiers over theoretical purity.
14.因此,我认为无论你做什么,不管是构建像HTML5这样的格式,
还是构建一个网站,亦或一个内容管理系统,明确你的设计原理都至关重要。
软件,就像所有技术一样,具有天然的政治性。代码必然会反映作者的选择、偏见和期望。
So I think that whatever you’re doing, whether it’s building a format like HTML5 or
whether it’s building a website, whether it’s building a content management system,
having design principles is really really useful.
Software, like all technologies, is inherently political.
Code inevitably reflects the choices, biases and desires of its creators.
15.马克·博尔顿和丽莎·雷贺特的设计原理:
简化最常见的任务,让不常见的任务不至于太麻烦。
只为80%设计。
给内容创建者最大的权利。
默认设置智能化。
Mark Boulton and Leisa Reichelt’s four design principles:
Make the most frequent tasks easy and less frequent tasks achievable.
Design for the 80%.
Privilege the Content Creator.
Make the default settings smart.
16.微格式的设计原理:首先为人类设计,其次为机器设计。
Microformats’s design principles: Design for humans first, machines second.
17.互联网哲学:大多数人的意见和运行的代码。
Internet Philosophy:Rough consensus and running code.

我的观点:
我感觉设计原理是一种接近哲学的概念,虽然我以前没有仔细想过设计原理这个概念,
但是直觉告诉我这是一个很重要的,长久以来被我忽略的概念,
而且了解和思考这个概念必将对我的未来产生很大的影响。
下面是我对上面那些设计原理的一些看法,
当然我不可能一一铺开去讲,因为每一条原理都能铺开成一篇文章甚至一本书。
a. 关于美国的设计原理。其实之前看经济学的时候我就研究过这句话。
从某种角度讲,生存,自由和追求幸福的权利是相互重叠的。
这三条其实对应了生命权,自由权和财产权。这三条可以归并成一条,就是自由权。
因为我认为自由权就包括了你有权自由的支配自己的生命和合法财产。
这就是为什么我一直很强调自由权的重要。
这里合法财产也是一个很大的课题,我倾向于奥地利学派的观点。
b. 关于马克思的社会主义想法。历史已经证明了他的观点只能是一个美好的愿望。
因为人与人之间本质就不是相同的,这里的本质我是指的欲望,能力等等方面的不同。
而且人有天生的惰性,天生就想不劳而获,各取所需注定是一句空话。
有的人可能会相信马克思主义者的辩护,
“现在不可以,但是生产力发展到物质极度丰富的时候就能实现各取所需了。”
他们没有想到的是生产力发展会带来欲望的提高,
也就是各取所需中的“需”会提着生产力发展而提高。
比如十年前的人不可能有要iphone的需求,但是到现在这只是一个很普通的需求。
c. 关于人人为我,我为人人。这一条原理也存在问题。
问题就在于这个人人为我和我为人人有没有报酬,也就是用不用付钱。
如果付钱,那就是市场交易,整个经济学都是建立在这个基础上,互通有无,交换提高双方的幸福度。
如果不付钱,那就是宗教了。宗教鼓励我为人人,这样自然而然就会人人为我。
但是其中的问题是每个人都人人为我多一点,我为人人少一点,能占便宜就占便宜。
这是天性,由基因决定的,无法改变。
因为那些整天想着我为人人的物种或者个体已经被进化所淘汰了。
所以人人为我,我为人人这种宗教机制用来作为社会的缓解矛盾的缓冲器还可以,
如果想用它来作为整个社会的基础,那它就跟马克思主义没有分别了,注定是行不通的。
d. 关于机器人三大法则。我之前看过阿西莫夫的小说,
小说很精彩,但是现实中我觉得至少现在是没有办法让机器人来遵守三大法则的。
而现实中的机器人已经可以做很多事了,美国甚至已经研制出了用来杀人的机器人。
不知道这是不是一件的可怕的事。
e. 关于html5的几条设计原理。我觉得这些原理不仅仅对html5的设计有用,
对任何程序的设计都是有用的。比如发送时要保守;接收时要开放。比如避免不必要的复杂性。
比如支持已有的内容。还有解决现实的问题。
f. 关于求真务实(Pave the cowpaths.)。
我认为这个翻译其实不太准确。我觉得应该翻译成承认既定事实。
我以前在关于美国的历史上立法过程的文章中就看到过这种原理。
这里面的逻辑其实是最终的用户自己就会产生一种解释问题的方案,
不论是争论法律争端还是解决html中的问题,用户是最先接触问题的人,
所以最有可能想出解决问题的方法。立法者或者标准制定者只要择其善者而从之就可以了。
g. 关于平稳退化。
其实我觉得平稳退化,支持已有内容和接收时要开放可以归为一条,保持连续性。
h. 关于最终用户优先。
其实这一条与求真务实有相通之处,都是要把最终用户放在优先的位置。
我觉得这一条用在公司管理里面也是对的,一个成功的公司一定是解决了用户的一种需求。
所以一定要把用户放在优先的位置,公司内部越接近客户就越重要。
老板作为规则制定者要把自己摆在最后,尊重支持比自己优先级高的人,
如果发现优先级高的人产生了对用户有用的方案,那就推广它,把它制定成标准。
i. 关于代码有政治性。
我有体会,因为有些人的代码只是为了解决问题,不去考虑性能,扩展性,代码是否简洁这些问题,
而另外一些人力图把代码模块化,简洁优雅。我以前觉得这只是代码风格和编码素质的问题。
现在仔细想想其实这些区别反映了代码作者的设计思想甚至价值观,有的作者只是为完成任务,
而另外一些作者是为了创造自己的作品甚至想到把代码共享出去造福社会。
j. 关于马克·博尔顿的几个设计原理。
以前没有意识到设计原理的重要性,所以我也没有总结归纳自己的设计原理。
现在看来我要开始留心总结归纳自己的设计原理了。我直觉中想到的有这么几条:
一.要模块化。二.要简洁。三.要有连续性。四.要有统一的风格和清楚的注释。

2011-04-04-圆周率pi和自然对数底e小数点后一万位

2011-04-04-圆周率pi和自然对数底e小数点后一万位(2011-04-04 13:52:14)

Fortran77源程序计算圆周率pi和自然对数底e小数点后一万位

前段时间看到google的一个招聘广告:

Google在旧金山的101公路上曾放出一块广告牌,写着:“常数E里出现的连续的第一个10位质数.Com”。

虽然在网上可以找到答案,但是我就很好奇,想要自己编程计算出来。然后就用fortran77写了程序来计算自然对数底e和圆周率pi.

fortran77的编译器可以从这里下载
http://www.math.hawaii.edu/lab/197/fortran_instr_hile.html
我编的程序计算了pi和e小数点后面一万位,精确到小数点后9996位。
计算结果给出了pi和e的值,还给出了一万位以内的所有十位数的质数。

算法分别是
pi=16actan(1/5)-4actan(1/239)
actan(x)=x-x*3/3+x5/5-x*7/7+….

e=1+1/2!+1/3!+1/4!+…..+1/n!

程序的算法没有经过优化,因为反正是自娱自乐,对计算速度没什么要求。
在本人的机器上计算出来结果只需要不到十秒钟。

欢迎大家转载,但是请保留原文链接。
http://blog.sina.com.cn/s/blog_4fb4b6ba0100qfln.html
下面是计算的程序和结果。
有兴趣的同学可以下个fortran77的编译器自己运行编译一下,很酷哦!

计算圆周率:

C    This program is free software: you can redistribute it and/or
      C    modify
      C    it under the terms of the GNU General Public License as
      C    published by
      C    the Free Software Foundation, either version 3 of the
      C    License, or
      C    (at your option) any later version.
      C
      C    This program is distributed in the hope that it will be
      C    useful,
      C    but WITHOUT ANY WARRANTY; without even the implied warranty
      C    of
      C    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      C    GNU General Public License for more details.
      C
      C   You should have received a copy of the GNU General Public
      C   License
      C    along with this program.  If not, see
      C    <http://www.gnu.org/licenses/>.
      C
      C Author: Cheng GONG, gongchengym@sina.com 
      C Date: 2011-040-04
      C pi=16actan(1/5)-4actan(1/239
      C actan(x)=x-x^3/3+x^5/5-x^7/7+x^9/9+....
      C pi=3.2-4/239-1/3*(3.2/25-4/239**3)+1/5*(3.2/25**2-4/239**5)
      implicit real*8(a-h,o-z)
      parameter(nk=10000)
      dimension ns(nk),na1(nk),na2(nk),nt1(nk),nt2(nk),np1(nk),np2(nk)
      do i=1,nk
      ns(i)=0
      na1(i)=0
      na2(i)=0
      nt1(i)=0
      nt2(i)=0
      np1(i)=0
      np2(i)=0
      end do
      nt1(2)=3
      nt1(3)=2
      nt2(2)=4
      do i=2,nk
      na2(i)=nt2(i)
      nd=mod(na2(i-1),239)*10+na2(i)
      nt2(i)=nd/239
      na2(i)=mod(nd,239)
      end do
      do i=1,nk
      ns(i)=ns(i)+nt1(i)
      end do
      do i=nk,2,-1
      ntmp=ns(i)
      if(ntmp.ge.nt2(i)) ns(i)=ns(i)-nt2(i)
      if(ntmp.lt.nt2(i)) then
          ns(i)=ns(i)+10-nt2(i)
          ns(i-1)=ns(i-1)-1
      end if
      end do
      n=1
      nflag=1
      do j=1,nk
      n=n+2
      nflag=-1*nflag
      do i=2,nk
      na1(i)=nt1(i)
      nd=mod(na1(i-1),25)*10+na1(i)
      nt1(i)=nd/25
      na1(i)=mod(nd,25)
      end do
      do i=2,nk
      na1(i)=nt1(i)
      nd=mod(na1(i-1),n)*10+na1(i)
      np1(i)=nd/n
      na1(i)=mod(nd,n)
      end do
      do i=2,nk
      na2(i)=nt2(i)
      nd=mod(na2(i-1),57121)*10+na2(i)
      nt2(i)=nd/57121
      na2(i)=mod(nd,57121)
      end do
      do i=2,nk
      na2(i)=nt2(i)
      nd=mod(na2(i-1),n)*10+na2(i)
      np2(i)=nd/n
      na2(i)=mod(nd,n)
      end do
      if(nflag.lt.0) then
          do i=2,nk
          ns(i)=ns(i)+np2(i)
          end do
          do i=nk,2,-1
          ntmp=ns(i)
          if(ntmp.ge.np1(i)) ns(i)=ns(i)-np1(i)
          if(ntmp.lt.np1(i)) then
              ns(i)=ns(i)+10-np1(i)
              ns(i-1)=ns(i-1)-1
          end if
          ns(i-1)=ns(i-1)+ns(i)/10
          ns(i)=mod(ns(i),10)
          end do
      end if
      if(nflag.gt.0) then
          do i=2,nk
          ns(i)=ns(i)+np1(i)
          end do
          do i=nk,2,-1
          ntmp=ns(i)
          if(ntmp.ge.np2(i)) ns(i)=ns(i)-np2(i)
          if(ntmp.lt.np2(i)) then
              ns(i)=ns(i)+10-np2(i)
              ns(i-1)=ns(i-1)-1
          end if
          ns(i-1)=ns(i-1)+ns(i)/10
          ns(i)=mod(ns(i),10)
          end do
      end if
      end do
      open(60,file="pi.txt")
      write(60,'(1a)'),"pi=3."
      write(60,'(50i1)')(ns(i),i=3,nk)
      write(*,'(a)'),'The ten digital primes in pi:'
      write(60,'(a)'),'The ten digital primes in pi:'
      do i=2,nk-10
      tmp=0
      if (ns(i).ne.0) then
          do j=i,i+9
          tmp=tmp*10+ns(j)
          end do
          nst=sqrt(tmp)
          do it=2,nst
          if (mod(tmp,it).eq.0) then 
              goto 13
          end if 
          end do
          13 if (it.gt.nst) then
              write(*,'(i5,f15.1)'),i,tmp
              write(60,'(i5,f15.1)'),i,tmp
          end if
      end if
      end do
      close(60)
      stop
      end

结果:
pi=3.
14159265358979323846264338327950288419716939937510
58209749445923078164062862089986280348253421170679
82148086513282306647093844609550582231725359408128
48111745028410270193852110555964462294895493038196
44288109756659334461284756482337867831652712019091
45648566923460348610454326648213393607260249141273
72458700660631558817488152092096282925409171536436
78925903600113305305488204665213841469519415116094
33057270365759591953092186117381932611793105118548
07446237996274956735188575272489122793818301194912
98336733624406566430860213949463952247371907021798
60943702770539217176293176752384674818467669405132
00056812714526356082778577134275778960917363717872
14684409012249534301465495853710507922796892589235
42019956112129021960864034418159813629774771309960
51870721134999999837297804995105973173281609631859
50244594553469083026425223082533446850352619311881
71010003137838752886587533208381420617177669147303
59825349042875546873115956286388235378759375195778
18577805321712268066130019278766111959092164201989
38095257201065485863278865936153381827968230301952
03530185296899577362259941389124972177528347913151
55748572424541506959508295331168617278558890750983
81754637464939319255060400927701671139009848824012
85836160356370766010471018194295559619894676783744
94482553797747268471040475346462080466842590694912
93313677028989152104752162056966024058038150193511
25338243003558764024749647326391419927260426992279
67823547816360093417216412199245863150302861829745
55706749838505494588586926995690927210797509302955
32116534498720275596023648066549911988183479775356
63698074265425278625518184175746728909777727938000
81647060016145249192173217214772350141441973568548
16136115735255213347574184946843852332390739414333
45477624168625189835694855620992192221842725502542
56887671790494601653466804988627232791786085784383
82796797668145410095388378636095068006422512520511
73929848960841284886269456042419652850222106611863
06744278622039194945047123713786960956364371917287
46776465757396241389086583264599581339047802759009
94657640789512694683983525957098258226205224894077
26719478268482601476990902640136394437455305068203
49625245174939965143142980919065925093722169646151
57098583874105978859597729754989301617539284681382
68683868942774155991855925245953959431049972524680
84598727364469584865383673622262609912460805124388
43904512441365497627807977156914359977001296160894
41694868555848406353422072225828488648158456028506
01684273945226746767889525213852254995466672782398
64565961163548862305774564980355936345681743241125
15076069479451096596094025228879710893145669136867
22874894056010150330861792868092087476091782493858
90097149096759852613655497818931297848216829989487
22658804857564014270477555132379641451523746234364
54285844479526586782105114135473573952311342716610
21359695362314429524849371871101457654035902799344
03742007310578539062198387447808478489683321445713
86875194350643021845319104848100537061468067491927
81911979399520614196634287544406437451237181921799
98391015919561814675142691239748940907186494231961
56794520809514655022523160388193014209376213785595
66389377870830390697920773467221825625996615014215
03068038447734549202605414665925201497442850732518
66600213243408819071048633173464965145390579626856
10055081066587969981635747363840525714591028970641
40110971206280439039759515677157700420337869936007
23055876317635942187312514712053292819182618612586
73215791984148488291644706095752706957220917567116
72291098169091528017350671274858322287183520935396
57251210835791513698820914442100675103346711031412
67111369908658516398315019701651511685171437657618
35155650884909989859982387345528331635507647918535
89322618548963213293308985706420467525907091548141
65498594616371802709819943099244889575712828905923
23326097299712084433573265489382391193259746366730
58360414281388303203824903758985243744170291327656
18093773444030707469211201913020330380197621101100
44929321516084244485963766983895228684783123552658
21314495768572624334418930396864262434107732269780
28073189154411010446823252716201052652272111660396
66557309254711055785376346682065310989652691862056
47693125705863566201855810072936065987648611791045
33488503461136576867532494416680396265797877185560
84552965412665408530614344431858676975145661406800
70023787765913440171274947042056223053899456131407
11270004078547332699390814546646458807972708266830
63432858785698305235808933065757406795457163775254
20211495576158140025012622859413021647155097925923
09907965473761255176567513575178296664547791745011
29961489030463994713296210734043751895735961458901
93897131117904297828564750320319869151402870808599
04801094121472213179476477726224142548545403321571
85306142288137585043063321751829798662237172159160
77166925474873898665494945011465406284336639379003
97692656721463853067360965712091807638327166416274
88880078692560290228472104031721186082041900042296
61711963779213375751149595015660496318629472654736
42523081770367515906735023507283540567040386743513
62222477158915049530984448933309634087807693259939
78054193414473774418426312986080998886874132604721
56951623965864573021631598193195167353812974167729
47867242292465436680098067692823828068996400482435
40370141631496589794092432378969070697794223625082
21688957383798623001593776471651228935786015881617
55782973523344604281512627203734314653197777416031
99066554187639792933441952154134189948544473456738
31624993419131814809277771038638773431772075456545
32207770921201905166096280490926360197598828161332
31666365286193266863360627356763035447762803504507
77235547105859548702790814356240145171806246436267
94561275318134078330336254232783944975382437205835
31147711992606381334677687969597030983391307710987
04085913374641442822772634659470474587847787201927
71528073176790770715721344473060570073349243693113
83504931631284042512192565179806941135280131470130
47816437885185290928545201165839341965621349143415
95625865865570552690496520985803385072242648293972
85847831630577775606888764462482468579260395352773
48030480290058760758251047470916439613626760449256
27420420832085661190625454337213153595845068772460
29016187667952406163425225771954291629919306455377
99140373404328752628889639958794757291746426357455
25407909145135711136941091193932519107602082520261
87985318877058429725916778131496990090192116971737
27847684726860849003377024242916513005005168323364
35038951702989392233451722013812806965011784408745
19601212285993716231301711444846409038906449544400
61986907548516026327505298349187407866808818338510
22833450850486082503930213321971551843063545500766
82829493041377655279397517546139539846833936383047
46119966538581538420568533862186725233402830871123
28278921250771262946322956398989893582116745627010
21835646220134967151881909730381198004973407239610
36854066431939509790190699639552453005450580685501
95673022921913933918568034490398205955100226353536
19204199474553859381023439554495977837790237421617
27111723643435439478221818528624085140066604433258
88569867054315470696574745855033232334210730154594
05165537906866273337995851156257843229882737231989
87571415957811196358330059408730681216028764962867
44604774649159950549737425626901049037781986835938
14657412680492564879855614537234786733039046883834
36346553794986419270563872931748723320837601123029
91136793862708943879936201629515413371424892830722
01269014754668476535761647737946752004907571555278
19653621323926406160136358155907422020203187277605
27721900556148425551879253034351398442532234157623
36106425063904975008656271095359194658975141310348
22769306247435363256916078154781811528436679570611
08615331504452127473924544945423682886061340841486
37767009612071512491404302725386076482363414334623
51897576645216413767969031495019108575984423919862
91642193994907236234646844117394032659184044378051
33389452574239950829659122850855582157250310712570
12668302402929525220118726767562204154205161841634
84756516999811614101002996078386909291603028840026
91041407928862150784245167090870006992821206604183
71806535567252532567532861291042487761825829765157
95984703562226293486003415872298053498965022629174
87882027342092222453398562647669149055628425039127
57710284027998066365825488926488025456610172967026
64076559042909945681506526530537182941270336931378
51786090407086671149655834343476933857817113864558
73678123014587687126603489139095620099393610310291
61615288138437909904231747336394804575931493140529
76347574811935670911013775172100803155902485309066
92037671922033229094334676851422144773793937517034
43661991040337511173547191855046449026365512816228
82446257591633303910722538374218214088350865739177
15096828874782656995995744906617583441375223970968
34080053559849175417381883999446974867626551658276
58483588453142775687900290951702835297163445621296
40435231176006651012412006597558512761785838292041
97484423608007193045761893234922927965019875187212
72675079812554709589045563579212210333466974992356
30254947802490114195212382815309114079073860251522
74299581807247162591668545133312394804947079119153
26734302824418604142636395480004480026704962482017
92896476697583183271314251702969234889627668440323
26092752496035799646925650493681836090032380929345
95889706953653494060340216654437558900456328822505
45255640564482465151875471196218443965825337543885
69094113031509526179378002974120766514793942590298
96959469955657612186561967337862362561252163208628
69222103274889218654364802296780705765615144632046
92790682120738837781423356282360896320806822246801
22482611771858963814091839036736722208883215137556
00372798394004152970028783076670944474560134556417
25437090697939612257142989467154357846878861444581
23145935719849225284716050492212424701412147805734
55105008019086996033027634787081081754501193071412
23390866393833952942578690507643100638351983438934
15961318543475464955697810382930971646514384070070
73604112373599843452251610507027056235266012764848
30840761183013052793205427462865403603674532865105
70658748822569815793678976697422057505968344086973
50201410206723585020072452256326513410559240190274
21624843914035998953539459094407046912091409387001
26456001623742880210927645793106579229552498872758
461012648369998922569596881592056001016552563793
The ten digital primes in pi:
6 5926535897.0
38 4197169399.0
42 1693993751.0
49 7510582097.0
89 4825342117.0
135 5822317253.0
162 2841027019.0
173 8521105559.0
191 8954930381.0
225 4756482337.0
243 2712019091.0
274 5432664821.0
276 3266482133.0
288 6072602491.0
317 5588174881.0
324 8815209209.0
334 6282925409.0
339 2540917153.0
357 5903600113.0
375 8204665213.0
384 3841469519.0
402 4330572703.0
415 5959195309.0
427 8611738193.0
433 8193261179.0
452 8074462379.0
476 5752724891.0
483 8912279381.0
492 1830119491.0
602 2000568127.0
621 8277857713.0
627 7713427577.0
637 8960917363.0
653 1468440901.0
690 2796892589.0
710 6112129021.0
727 3441815981.0
736 1362977477.0
749 9960518707.0
776 8049951059.0
779 9951059731.0
784 5973173281.0
787 3173281609.0
819 3026425223.0
847 3118817101.0
852 1710100031.0
869 2886587533.0
893 7669147303.0
895 6914730359.0
938 3787593751.0
965 1226806613.0
1128 5331168617.0
1159 3746493931.0
1170 2550604009.0
1197 8240128583.0
1221 1047101819.0
1257 2553797747.0
1263 7747268471.0
1270 4710404753.0
1291 8425906949.0
1314 9891521047.0
1323 7521620569.0
1375 9647326391.0
1388 9272604269.0
1389 2726042699.0
1432 2458631503.0
1459 4983850549.0
1487 7210797509.0
1532 5499119881.0
1579 1757467289.0
1588 9097777279.0
1595 2793800081.0
1616 5249192173.0
1623 1732172147.0
1670 3475741849.0
1679 9468438523.0
1682 8438523323.0
1691 3907394143.0
1698 1433345477.0
1735 1922218427.0
1756 8767179049.0
1814 1454100953.0
1874 6945604241.0
1906 4427862203.0
1916 9194945047.0
1917 1949450471.0
1947 9172874677.0
1952 7467764657.0
1963 7396241389.0
1993 7802759009.0
2004 4657640789.0
2006 5764078951.0
2020 6839835259.0
2062 6848260147.0
2073 9909026401.0
2093 5305068203.0
2113 4939965143.0
2130 1906592509.0
2137 5093722169.0
2143 2169646151.0
2167 9788595977.0
2180 5498930161.0
2184 9301617539.0
2190 7539284681.0
2198 8138268683.0
2230 4595395943.0
2411 9452267467.0
2416 6746767889.0
2436 5499546667.0
2451 9864565961.0
2465 4886230577.0
2491 6817432411.0
2515 5109659609.0
2524 9402522887.0
2547 1368672287.0
2574 6179286809.0
2589 7609178249.0
2590 6091782493.0
2612 9675985261.0
2623 6554978189.0
2643 6829989487.0
2654 2658804857.0
2678 5132379641.0
2727 1413547357.0
2738 9523113427.0
2742 1134271661.0
2746 2716610213.0
2771 2484937187.0
2788 6540359027.0
2841 6833214457.0
2850 7138687519.0
2857 5194350643.0
2941 2371819217.0
2973 1426912397.0
2991 1864942319.0
2997 2319615679.0
3031 8193014209.0
3050 5956638937.0
3104 3068038447.0
3138 4974428507.0
3139 9744285073.0
3150 5186660021.0
3159 1324340881.0
3207 5081066587.0
3213 6587969981.0
3219 9981635747.0
3243 1028970641.0
3273 7595156771.0
3293 7869936007.0
3313 7635942187.0
3315 3594218731.0
3331 2053292819.0
3351 8673215791.0
3450 3965725121.0
3456 5121083579.0
3462 3579151369.0
3477 1444210067.0
3537 1685171437.0
3540 5171437657.0
3570 8599823873.0
3595 4791853589.0
3600 5358932261.0
3607 2618548963.0
3613 8963213293.0
3656 9859461637.0
3672 9819943099.0
3696 8905923233.0
3722 3573265489.0
3726 2654893823.0
3728 5489382391.0
3730 8938239119.0
3760 4281388303.0
3786 4374417029.0
3787 3744170291.0
3788 7441702913.0
3821 6921120191.0
3841 1976211011.0
3948 6978028073.0
3951 8028073189.0
3956 7318915441.0
4001 9666557309.0
4026 3466820653.0
4029 6820653109.0
4036 1098965269.0
4090 7648611791.0
4101 4533488503.0
4149 5560845529.0
4211 7659134401.0
4240 3899456131.0
4268 3269939081.0
4283 4645880797.0
4297 2668306343.0
4306 3285878569.0
4308 8587856983.0
4319 5235808933.0
4323 8089330657.0
4451 1129961489.0
4563 1472213179.0
4569 3179476477.0
4592 4540332157.0
4599 1571853061.0
4617 5850430633.0
4641 2371721591.0
4689 8433663937.0
4690 4336639379.0
4802 6617119637.0
4813 9213375751.0
4814 2133757511.0
4830 1566049631.0
4944 6932599397.0
4951 3978054193.0
4957 4193414473.0
4959 9341447377.0
4976 3129860809.0
4985 9988868741.0
5018 7302163159.0
5028 8193195167.0
5041 8129741677.0
5065 6543668009.0
5071 8009806769.0
5121 9409243237.0
5130 7896907069.0
5149 5082216889.0
5151 8221688957.0
5154 1688957383.0
5160 7383798623.0
5175 3776471651.0
5180 7165122893.0
5196 5881617557.0
5278 2154134189.0
5286 8994854447.0
5300 7383162499.0
5330 3863877343.0
5333 3877343177.0
5371 6609628049.0
5388 1975988281.0
5399 1332316663.0
5443 2803504507.0
5461 1058595487.0
5470 7027908143.0
5521 3033625423.0
5532 7839449753.0
5558 7119926063.0
5561 9926063813.0
5588 9833913077.0
5614 6414428227.0
5635 4745878477.0
5641 8477872019.0
5656 2807317679.0
5680 7306057007.0
5729 1798069411.0
5747 4701304781.0
5751 3047816437.0
5774 4520116583.0
5776 2011658393.0
5784 9341965621.0
5785 3419656213.0
5787 1965621349.0
5820 6904965209.0
5841 2426482939.0
5850 9728584783.0
5880 6248246857.0
5961 8320856611.0
5997 7724602901.0
6046 6455377991.0
6074 8963995879.0
6076 6399587947.0
6108 9091451357.0
6114 1357111369.0
6129 1939325191.0
6143 2082520261.0
6153 8798531887.0
6176 7781314969.0
6180 3149699009.0
6186 9009019211.0
6191 1921169717.0
6257 8951702989.0
6263 2989392233.0
6309 1228599371.0
6312 8599371623.0
6337 9038906449.0
6349 4400619869.0
6370 3275052983.0
6372 7505298349.0
6420 5039302133.0
6468 5527939751.0
6477 1754613953.0
6478 7546139539.0
6490 6833936383.0
6523 5685338621.0
6551 2328278921.0
6571 4632295639.0
6577 5639898989.0
6606 3564622013.0
6608 6462201349.0
6616 4967151881.0
6626 9097303811.0
6654 6854066431.0
6662 3193950979.0
6671 9019069963.0
6709 2292191393.0
6711 9219139339.0
6718 3391856803.0
6729 4903982059.0
6732 3982059551.0
6745 2635353619.0
6750 5361920419.0
6783 9597783779.0
6786 7783779023.0
6798 2161727111.0
6812 4343543947.0
6829 5286240851.0
6856 6986705431.0
6880 5503323233.0
6915 6627333799.0
6919 3337995851.0
7068 6487985561.0
7121 7056387293.0
7128 2931748723.0
7133 4872332083.0
7147 1230299113.0
7158 7938627089.0
7164 7089438799.0
7174 3620162951.0
7178 1629515413.0
7219 6535761647.0
7254 9653621323.0
7296 7277605277.0
7313 6148425551.0
7343 2234157623.0
7346 4157623361.0
7369 5008656271.0
7387 4658975141.0
7425 6078154781.0
7458 3315044521.0
7464 4521274739.0
7513 2071512491.0
7550 6235189757.0
7594 4239198629.0
7603 9164219399.0
7605 6421939949.0
7621 3464684411.0
7645 4437805133.0
7648 7805133389.0
7653 3338945257.0
7656 8945257423.0
7658 4525742399.0
7679 8508555821.0
7689 5725031071.0
7737 4154205161.0
7740 4205161841.0
7776 9960783869.0
7795 2884002691.0
7801 2691041407.0
7802 6910414079.0
7808 4079288621.0
7843 1206604183.0
7859 3556725253.0
7879 2910424877.0
7910 3562226293.0
7972 3398562647.0
7991 6284250391.0
7995 2503912757.0
8005 7102840279.0
8047 9670266407.0
8089 4127033693.0
8091 2703369313.0
8128 4343476933.0
8160 3014587687.0
8189 9939361031.0
8206 1528813843.0
8217 9099042317.0
8250 5297634757.0
8254 6347574811.0
8255 3475748119.0
8279 1721008031.0
8297 3090669203.0
8312 2203322909.0
8330 5142214477.0
8366 7511173547.0
8378 1855046449.0
8403 8244625759.0
8410 7591633303.0
8414 6333039107.0
8461 8747826569.0
8491 3752239709.0
8548 5827658483.0
8554 8483588453.0
8559 8845314277.0
8568 7568790029.0
8581 1702835297.0
8703 7267507981.0
8741 4669749923.0
8758 9478024901.0
8777 8281530911.0
8780 1530911407.0
8790 9073860251.0
8804 4299581807.0
8865 1860414263.0
8910 6697583183.0
8912 9758318327.0
8923 3142517029.0
8956 9275249603.0
8972 6925650493.0
8998 2934595889.0
9007 9706953653.0
9029 6544375589.0
9063 4482465151.0
9095 3754388569.0
9151 9896959469.0
9170 1865619673.0
9199 8628692221.0
9221 5436480229.0
9226 8022967807.0
9300 8012248261.0
9303 2248261177.0
9308 6117718589.0
9347 1375560037.0
9376 7830766709.0
9404 5437090697.0
9445 6144458123.0
9487 4701412147.0
9504 5105008019.0
9522 3027634787.0
9545 9307141223.0
9553 2339086639.0
9582 6431006383.0
9598 3893415961.0
9642 1438407007.0
9657 4112373599.0
9688 2352660127.0
9701 4830840761.0
9815 2358502007.0
9927 4579310657.0
9939 2955249887.0


计算自然对数底e

C    This program is free software: you can redistribute it and/or modify
C    it under the terms of the GNU General Public License as published by
C    the Free Software Foundation, either version 3 of the License, or
C    (at your option) any later version.
C
C    This program is distributed in the hope that it will be useful,
C    but WITHOUT ANY WARRANTY; without even the implied warranty of
C    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
C    GNU General Public License for more details.
C
C   You should have received a copy of the GNU General Public License
C    along with this program.  If not, see <http://www.gnu.org/licenses/>.
C
C Author: Cheng GONG, gongchengym@sina.com 
C Date: 2011-040-04
C e=1+1/1!+1/2!+1/3!+1/4!+1/5!+...+1/n!

implicit real*8(a-h,o-z)
parameter(nk=10000,np=100000)
dimension ns(nk),na(nk),nt(nk),nprime(np/10)


C calculate how many digitals.
C s=1.0
C do i=1,5000
C s=s+log10(i*1.0)
C end do
C print *,s
do i=1,nk
na(i)=0
ns(i)=0
nt(i)=0
end do
na(1)=1
ns(1)=2


n=1
do j=1,5000
n=n+1
do i=2,nk
nd=mod(na(i-1),n)*10+nt(i) 
nt(i)=nd/n
na(i)=mod(nd,n)
end do
do i=1,nk
na(i)=nt(i)
ns(i)=ns(i)+nt(i)
end do
do i=nk,2,-1
ns(i-1)=ns(i-1)+ns(i)/10
ns(i)=mod(ns(i),10)
end do
end do


open(60,file="e.txt")
write(60,'(1a)'),"e=2."
write(60,'(8x,77i1)')(ns(i),i=2,77)
write(60,'(80i1)')(ns(i),i=78,nk)
write(*,'(a)'),'The ten digital primes in e:'
write(60,'(a)'),'The ten digital primes in e:'
do i=2,nk-10
C i=2
tmp=0
if(ns(i).ne.0) then
do j=i,i+9
tmp=tmp*10+ns(j)
end do
nst=sqrt(tmp)
do it=2,nst
if (mod(tmp,it).eq.0) then 
goto 13
end if 
end do
13 if (it.gt.nst) then
write(*,'(i5,f15.1)'),i,tmp
write(60,'(i5,f15.1)'),i,tmp
end if
end if
end do




close(60)


stop
end

结果:

e=2.
7182818284590452353602874713526624977572470936999595749669676277240766303535
47594571382178525166427427466391932003059921817413596629043572900334295260595630
73813232862794349076323382988075319525101901157383418793070215408914993488416750
92447614606680822648001684774118537423454424371075390777449920695517027618386062
61331384583000752044933826560297606737113200709328709127443747047230696977209310
14169283681902551510865746377211125238978442505695369677078544996996794686445490
59879316368892300987931277361782154249992295763514822082698951936680331825288693
98496465105820939239829488793320362509443117301238197068416140397019837679320683
28237646480429531180232878250981945581530175671736133206981125099618188159304169
03515988885193458072738667385894228792284998920868058257492796104841984443634632
44968487560233624827041978623209002160990235304369941849146314093431738143640546
25315209618369088870701676839642437814059271456354906130310720851038375051011574
77041718986106873969655212671546889570350354021234078498193343210681701210056278
80235193033224745015853904730419957777093503660416997329725088687696640355570716
22684471625607988265178713419512466520103059212366771943252786753985589448969709
64097545918569563802363701621120477427228364896134225164450781824423529486363721
41740238893441247963574370263755294448337998016125492278509257782562092622648326
27793338656648162772516401910590049164499828931505660472580277863186415519565324
42586982946959308019152987211725563475463964479101459040905862984967912874068705
04895858671747985466775757320568128845920541334053922000113786300945560688166740
01698420558040336379537645203040243225661352783695117788386387443966253224985065
49958862342818997077332761717839280349465014345588970719425863987727547109629537
41521115136835062752602326484728703920764310059584116612054529703023647254929666
93811513732275364509888903136020572481765851180630364428123149655070475102544650
11727211555194866850800368532281831521960037356252794495158284188294787610852639
81395599006737648292244375287184624578036192981971399147564488262603903381441823
26251509748279877799643730899703888677822713836057729788241256119071766394650706
33045279546618550966661856647097113444740160704626215680717481877844371436988218
55967095910259686200235371858874856965220005031173439207321139080329363447972735
59552773490717837934216370120500545132638354400018632399149070547977805669785335
80489669062951194324730995876552368128590413832411607226029983305353708761389396
39177957454016137223618789365260538155841587186925538606164779834025435128439612
94603529133259427949043372990857315802909586313826832914771163963370924003168945
86360606458459251269946557248391865642097526850823075442545993769170419777800853
62730941710163434907696423722294352366125572508814779223151974778060569672538017
18077636034624592787784658506560507808442115296975218908740196609066518035165017
92504619501366585436632712549639908549144200014574760819302212066024330096412704
89439039717719518069908699860663658323227870937650226014929101151717763594460202
32493002804018677239102880978666056511832600436885088171572386698422422010249505
51881694803221002515426494639812873677658927688163598312477886520141174110913601
16499507662907794364600585194199856016264790761532103872755712699251827568798930
27617611461625493564959037980458381823233686120162437365698467037858533052758333
37939907521660692380533698879565137285593883499894707416181550125397064648171946
70834819721448889879067650379590366967249499254527903372963616265897603949857674
13973594410237443297093554779826296145914429364514286171585873397467918975712119
56187385783644758448423555581050025611492391518893099463428413936080383091662818
81150371528496705974162562823609216807515017772538740256425347087908913729172282
86115159156837252416307722544063378759310598267609442032619242853170187817729602
35413060672136046000389661093647095141417185777014180606443636815464440053316087
78314317444081194942297559931401188868331483280270655383300469329011574414756313
99972217038046170928945790962716622607407187499753592127560844147378233032703301
68237193648002173285734935947564334129943024850235732214597843282641421684878721
67336701061509424345698440187331281010794512722373788612605816566805371439612788
87325273738903928905068653241380627960259303877276977837928684093253658807339884
57218746021005311483351323850047827169376218004904795597959290591655470505777514
30817511269898518840871856402603530558373783242292418562564425502267215598027401
26179719280471396006891638286652770097527670697770364392602243728418408832518487
70472638440379530166905465937461619323840363893131364327137688841026811219891275
22305625675625470172508634976536728860596675274086862740791285657699631378975303
46606166698042182677245605306607738996242183408598820718646826232150802882863597
46839654358856685503773131296587975810501214916207656769950659715344763470320853
21560367482860837865680307306265763346977429563464371670939719306087696349532884
68336130388294310408002968738691170666661468000151211434422560238744743252507693
87077775193299942137277211258843608715834835626961661980572526612206797540621062
08064988291845439530152998209250300549825704339055357016865312052649561485724925
73862069174036952135337325316663454665885972866594511364413703313936721185695539
52108458407244323835586063106806964924851232632699514603596037297253198368423363
90463213671011619282171115028280160448805880238203198149309636959673583274202498
82456849412738605664913525267060462344505492275811517093149218795927180019409688
66986837037302200475314338181092708030017205935530520700706072233999463990571311
58709963577735902719628506114651483752620956534671329002599439766311454590268589
89791158370934193704411551219201171648805669459381311838437656206278463104903462
93950029458341164824114969758326011800731699437393506966295712410273239138741754
92307186245454322203955273529524024590380574450289224688628533654221381572213116
32881120521464898051800920247193917105553901139433166815158288436876069611025051
71007392762385553386272553538830960671644662370922646809671254061869502143176211
66814009759528149390722260111268115310838731761732323526360583817315103459573653
82235349929358228368510078108846343499835184044517042701893819942434100905753762
57767571118090088164183319201962623416288166521374717325477727783488774366518828
75215668571950637193656539038944936642176400312152787022236646363575550356557694
88865495002708539236171055021311474137441061344455441921013361729962856948991933
69184729478580729156088510396781959429833186480756083679551496636448965592948187
85178403877332624705194505041984774201418394773120281588684570729054405751060128
52580565947030468363445926525521370080687520095934536073162261187281739280746230
94685367823106097921599360019946237993434210687813497346959246469752506246958616
90917857397659519939299399556754271465491045686070209901260681870498417807917392
40719459963230602547079017745275131868099822847308607665368668555164677029113368
27563107223346726113705490795365834538637196235856312618387156774118738527722922
59474337378569553845624680101390572787101651296663676445187246565373040244368414
08144887329578473484900030194778880204603246608428753518483649591950828883232065
22128104190448047247949291342284951970022601310430062410717971502793433263407995
96053144605323048852897291765987601666781193793237245385720960758227717848336161
35826128962261181294559274627671377944875867536575448614076119311259585126557597
34573015333642630767985443385761715333462325270572005303988289499034259566232975
78248873502925916682589445689465599265845476269452878051650172067478541788798227
68065366506419109734345288783386217261562695826544782056729877564263253215942944
18039943217000090542650763095588465895171709147607437136893319469090981904501290
30709956622662030318264936573369841955577696378762491885286568660760056602560544
57113372868402055744160308370523122425872234388541231794813885500756893811249353
86318635287083799845692619981794523364087429591180747453419551420351726184200845
50917084568236820089773945584267921427347756087964427920270831215015640634134161
71664480698154837644915739001212170415478725919989438253649505147713793991472052
19529079396137621107238494290616357604596231253506068537651423115349665683715116
60422079639446662116325515772907097847315627827759878813649195125748332879377157
14590910648416426783099497236744201758622694021594079244805412553604313179926967
39157542419296607312393763542139230617876753958711436104089409966089471418340698
36299367536262154524729846421375289107988438130609555262272083751862983706678722
44301957937937860721072542772890717328548743743557819665117166183308811291202452
04048682200072344035025448202834254187884653602591506445271657700044521097735585
89762265548494162171498953238342160011406295071849042778925855274303522139683567
90180764060421383073087744601708426882722611771808426643336517800021719034492342
64266292261456004337383868335555343453004264818473989215627086095650629340405264
94324426144566592129122564889356965500915430642613425266847259491431423939884543
24863274618428466559853323122104662598901417121034460842716166190012571958707932
17569698544013397622096749454185407118446433946990162698351607848924514058940946
39526780735457970030705116368251948770118976400282764841416058720618418529718915
40196882532893091496653457535714273184820163846448324990378860690080727093276731
27581966563941148961716832980455139729506687604740915420428429993541025829113502
24169076943166857424252250902693903481485645130306992519959043638402842926741257
34224477655841778861717372654620854982944989467873509295816526320722589923687684
57017823038096567883112289305809140572610865884845873101658151167533327674887014
82916741970151255978257270740643180860142814902414678047232759768426963393577354
29301867394397163886117642090040686633988568416810038723892144831760701166845038
87212364367043314091155733280182977988736590916659612402021778558854876176161989
37079438005666336488436508914480557103976521469602766258359905198704230017946552
053
The ten digital primes in e:
100 7427466391.0
124 7413596629.0
150 6059563073.0
172 3490763233.0
183 2988075319.0
202 1573834187.0
215 7021540891.0
219 5408914993.0
255 6480016847.0
296 9920695517.0
310 1838606261.0
314 6062613313.0
323 3845830007.0
400 1692836819.0
439 4425056953.0
441 2505695369.0
474 5490598793.0
506 1782154249.0
508 8215424999.0
517 9229576351.0
537 9519366803.0
538 5193668033.0
548 1825288693.0
578 8294887933.0
600 1730123819.0
618 4039701983.0
646 4804295311.0
668 8194558153.0
670 9455815301.0
688 1332069811.0
703 6181881593.0
706 1881593041.0
710 5930416903.0
729 1934580727.0
744 3858942287.0
782 4841984443.0
820 1978623209.0
857 3140934317.0
871 3640546253.0
893 8887070167.0
902 7683964243.0
922 4563549061.0
1016 3343210681.0
1017 3432106817.0
1036 7880235193.0
1065 3041995777.0
1123 4716256079.0
1147 5124665201.0
1209 5695638023.0
1217 2363701621.0
1314 4833799801.0
1330 2278509257.0
1351 2648326277.0
1374 2772516401.0
1421 6318641551.0
1474 7546396447.0
1520 8958586717.0
1522 5858671747.0
1541 7573205681.0
1590 8816674001.0
1608 8040336379.0
1617 9537645203.0
1640 5278369511.0
1684 6234281899.0
1697 7332761717.0
1711 8034946501.0
1734 4258639877.0
1739 3987727547.0
1742 7727547109.0
1829 2549296669.0
1832 9296669381.0
1836 6693811513.0
1872 2481765851.0
1888 3644281231.0
1898 4965507047.0
1900 6550704751.0
1932 8668508003.0
1943 8532281831.0
1947 2818315219.0
1960 3735625279.0
1963 5625279449.0
1979 4188294787.0
1996 3981395599.0
2028 8462457803.0
2060 2626039033.0
2062 2603903381.0
2149 3946507063.0
2166 5466185509.0
2171 8550966661.0
2177 6661856647.0
2181 8566470971.0
2183 6647097113.0
2205 6262156807.0
2207 6215680717.0
2250 5968620023.0
2264 8588748569.0
2276 2200050311.0
2277 2000503117.0
2290 9207321139.0
2303 3293634479.0
2322 2773490717.0
2325 3490717837.0
2326 4907178379.0
2360 5440001863.0
2373 9149070547.0
2402 9669062951.0
2428 5236812859.0
2457 9833053537.0
2460 3053537087.0
2463 3537087613.0
2465 3708761389.0
2466 7087613893.0
2494 7223618789.0
2495 2236187893.0
2622 3370924003.0
2690 5442545993.0
2691 4425459937.0
2698 9376917041.0
2771 2231519747.0
2787 6967253801.0
2836 4421152969.0
2853 8740196609.0
2906 9639908549.0
2929 6081930221.0
2946 3300964127.0
2950 9641270489.0
2953 1270489439.0
2958 8943903971.0
2960 4390397177.0
2962 9039717719.0
2973 1806990869.0
2974 8069908699.0
3003 3765022601.0
3163 8816359831.0
3185 1174110913.0
3206 6629077943.0
3215 3646005851.0
3264 5182756879.0
3308 5838182323.0
3316 2336861201.0
3343 8585330527.0
3350 5275833337.0
3356 3337939907.0
3382 9887956513.0
3429 6481719467.0
3432 1719467083.0
3456 7906765037.0
3525 4410237443.0
3537 7093554779.0
3562 3645142861.0
3572 7158587339.0
3597 9561873857.0
3632 5611492391.0
3638 2391518893.0
3640 9151889309.0
3644 8893099463.0
3650 9463428413.0
3657 4139360803.0
3659 3936080383.0
3670 9166281881.0
3676 1881150371.0
3681 5037152849.0
3712 6807515017.0
3713 8075150177.0
3737 3470879089.0
3742 7908913729.0
3762 5159156837.0
3814 6192428531.0
3821 5317018781.0
3833 2960235413.0
3911 3316087783.0
3936 4229755993.0
3937 2297559931.0
3970 5383300469.0
4035 7407187499.0
4058 4414737823.0
4063 3782330327.0
4066 2330327033.0
4095 2857349359.0
4104 9475643341.0
4150 8487872167.0
4162 6701061509.0
4185 8733128101.0
4187 3312810107.0
4203 2237378861.0
4230 3961278887.0
4236 8887325273.0
4242 5273738903.0
4272 7960259303.0
4323 7460210053.0
4343 3850047827.0
4395 5143081751.0
4401 1751126989.0
4440 8324229241.0
4501 6382866527.0
4530 4392602243.0
4576 6690546593.0
4607 1364327137.0
4618 8884102681.0
4703 6996313789.0
4704 9963137897.0
4706 6313789753.0
4708 1378975303.0
4714 5303466061.0
4718 4660616669.0
4723 1666980421.0
4740 5605306607.0
4752 8996242183.0
4757 4218340859.0
4797 7468396543.0
4859 9715344763.0
4868 3470320853.0
4876 5321560367.0
4936 9719306087.0
4961 3613038829.0
4975 4080029687.0
4989 1170666661.0
4998 1468000151.0
5017 5602387447.0
5034 7693870777.0
5039 7077775193.0
5105 6797540621.0
5119 8064988291.0
5125 8291845439.0
5132 4395301529.0
5153 5498257043.0
5176 5312052649.0
5183 6495614857.0
5201 6206917403.0
5246 9451136441.0
5247 4511364413.0
5318 1232632699.0
5329 1460359603.0
5349 3684233639.0
5370 1161928217.0
5399 8802382031.0
5409 9814930963.0
5440 4568494127.0
5480 9227581151.0
5494 1492187959.0
5518 6698683703.0
5527 3730220047.0
5641 6534671329.0
5654 5994397663.0
5656 9439766311.0
5685 8370934193.0
5721 9459381311.0
5735 3765620627.0
5751 4903462939.0
5772 1648241149.0
5812 6629571241.0
5815 9571241027.0
5820 4102732391.0
5889 2468862853.0
5890 4688628533.0
5959 9011394331.0
6025 3883096067.0
6034 7164466237.0
6046 2264680967.0
6086 7595281493.0
6105 1126811531.0
6148 3459573653.0
6156 5382235349.0
6204 4517042701.0
6207 7042701893.0
6209 4270189381.0
6215 9381994243.0
6238 5776757111.0
6254 8164183319.0
6258 1833192019.0
6298 2778348877.0
6312 5188287521.0
6319 5215668571.0
6320 2156685719.0
6366 5278702223.0
6418 1710550213.0
6442 3444554419.0
6509 1959429833.0
6552 9481878517.0
6586 1984774201.0
6595 1418394773.0
6661 9265255213.0
6725 7823106097.0
6748 4623799343.0
6762 6878134973.0
6798 9091785739.0
6841 5686070209.0
6873 1739240719.0
6899 7901774527.0
6938 6855516467.0
6942 5164677029.0
6969 3467261137.0
6975 1137054907.0
7022 4118738527.0
7036 2259474337.0
7045 7378569553.0
7058 6246801013.0
7062 8010139057.0
7174 4836495919.0
7184 5082888323.0
7197 5221281041.0
7221 2913422849.0
7259 1502793433.0
7282 3144605323.0
7325 2372453857.0
7363 1289622611.0
7429 1265575973.0
7465 8576171533.0
7485 5720053039.0
7515 9757824887.0
7531 5916682589.0
7583 4785417887.0
7594 8227680653.0
7650 2056729877.0
7708 8846589517.0
7722 1476074371.0
7788 6984195557.0
7793 9555776963.0
7922 8635287083.0
7970 7453419551.0
8030 9214273477.0
8066 6406341341.0
8094 7644915739.0
8110 1704154787.0
8139 5147713793.0
8141 4771379399.0
8150 9147205219.0
8168 6137621107.0
8298 9512574833.0
8302 5748332879.0
8306 3328793771.0
8308 2879377157.0
8355 8622694021.0
8382 3604313179.0
8390 7992696739.0
8399 9157542419.0
8425 4213923061.0
8506 1375289107.0
8540 7518629837.0
8556 2244301957.0
8605 5578196651.0
8606 5781966511.0
8659 2544820283.0
8664 2028342541.0
8687 1506445271.0
8726 5484941621.0
8730 9416217149.0
8755 1406295071.0
8765 8490427789.0
8794 3567901807.0
8817 3087744601.0
8831 2688272261.0
8834 8272261177.0
8875 3426426629.0
8903 8335555343.0
8949 3404052649.0
9013 6847259491.0
9033 8454324863.0
9051 8466559853.0
9054 6559853323.0
9092 4271616619.0
9095 1616619001.0
9099 6190012571.0
9107 7195870793.0
9121 6969854401.0
9156 4464339469.0
9183 9245140589.0
9212 9700307051.0
9231 4877011897.0
9298 6534575357.0
9302 5753571427.0
9322 8464483249.0
9323 4644832499.0
9350 9327673127.0
9360 5819665639.0
9362 1966563941.0
9375 9617168329.0
9473 4814856451.0
9528 5841778861.0
9533 7886171737.0
9656 8151167533.0
9665 3327674887.0
9686 9701512559.0
9700 7270740643.0
9701 2707406431.0
9760 3018673943.0
9769 3971638861.0
9814 8921448317.0
9819 4831760701.0
9832 8450388721.0
9842 2364367043.0
9847 6704331409.0
9878 6590916659.0
9889 1240202177.0
9924 3800566633.0
9935 4884365089.0
9955 3976521469.0
9981 9870423001.0

2007-07-12-三个圆柱体相交部分的体积

三个圆柱体相交部分的体积 2007-07-12 22:30:50
分类:
下午和一个兄弟一起做了这个题目。

就是求三个轴心分别沿正交XYZ轴的半径为一的圆柱体相交部分的体积。

下面是用Matlab编程的源程序和结果:

clear();
for i=1:10
    N=1000000;
    n=0;
    x=0;y=0;z=0;
    for j=1:N
        x=rand;
        y=rand;
        z=rand;
        if x*x+y*y<=1
            if y*y+z*z<=1
                if x*x+z*z<=1
                    n=n+1;
                end
            end
        end
    end
    v(i)=8*n/N;
end
v
mean(v)

结果:

v =
  Columns 1 through 7
    4.6910    4.6828    4.6829    4.6895    4.6921    4.6810    4.6913
  Columns 8 through 10
    4.6886    4.6881    4.6866

ans =
4.6874

基本思想就是在第一象限0<=x<=1,0<=y<=1,0<=z<=1的一个正方体里面投点,然后判断所投的点是不是在三个圆柱所围个的体积里面,如果在,则计数器n数值加1。总共投N个点。这样根据大数定理,点落在所求体积内的概率为第一象限内所求体积与正方体体积之比。n/N=V1/1*1*1.
所求的体积为第一象限体积V1的8倍。所以V=8*n/N.(N=1000000)
所得结果为4.6874.

现在我们来求该部分的精确结果:

求该体积的积分比较麻烦:
我们可以看到所求体积在8个象限里面是对称的,所以先求出在第一象限里面的体积。
联立x^2+y^2=1,y^2+z^2=1,x^2+z^2=1三个圆柱方程,我们可以得到三个圆柱的交点为
(sqrt(2)/2,sqrt(2)/2,sqrt(2)/2).再分析第一象限里面体积,我们可以知道该部分的体积又可以分成三部分。每一部分为交点与xyz坐标轴上单位1的点所夹的部分。该部分又可分为对称的两部分。
第一部分为0<=y<=sqrt(2)/2,0<=z<=y,y<=x<=sqrt(1-y^2)围成的体积。
该部分体积可以积分:
Integrate[y*(Sqrt[1 – y^2] – y), {y, 0, Sqrt[2]/2}]
所以所求体积为:
Integrate[y*(Sqrt[1 – y^2] – y), {y, 0, Sqrt[2]/2}]*6*8=8(2-sqrt(2))=4.68629.

以上为在Mathmatics里面用积分求出来的所求体积的精确结果。