Use hadoop to calculate pi values

Source: Internet
Author: User
Tags value of pi
I. method and principle of calculating pi values

Baidu, there are quite a few methods to calculate pi. However, in the hadoop examples code, note that the quasi-Monte Carlo algorithm is used to estimate the PI value.

Quasi-Monte Carlo's descriptive Comparison Theory in Wikipedia is difficult to understand.

Fortunately, Google found an article on the Stanford University website: Can PI be obtained by throwing a dart? The article is very short, illustrated and well understood.

Here I have taken a picture of the important part of the article:



The above figure is explained a little more:
1. figure2 is the part in the upper-right corner of Figure1.
2. throw a dart to figure2 several times (a large number), and each time it is still at a different point.
3. If the number of throws is very high, figure2 will be stabbed to be "riddled with holes ".
4. At this time, the "Number of throwing in the circle" is divided by the "Total throwing times" and multiplied by 4, which is the value of Pi! (For detailed derivation process, see the original article)


In this algorithm, it is very important to say how to "randomly throw at figure2", that is, how to achieve equal probability of each vertex in figure2 being cast.

In the hadoop examples code, Halton sequence is used to ensure this. For details about Halton sequence, refer to Wikipedia.

Here I will summarize the role of Halton sequence: In the square where 1 is multiplied by 1, there are non-repeated and even points. The abscissa and ordinate values of each point are between 0 and 1.

This ensures that "random throwing to figure2" is possible ".

Someone has concluded that this is actually called the Monte Carlo algorithm. If we create an incircle (Unit Circle) in a square (1 & times; 1) of a unit, the area of the unit square is as follows: inner cut Unit Circle Area = number of darts in the unit square: Number of darts in the inner cut unit circle. By calculating the number of darts, you can calculate the area of the unit circle and calculate the circumference rate.
Note that the accuracy is proportional to the number of darts you throw.

 

2. Run the hadoop PI estimation command

[Java]View plaincopyprint?

  1. <Span style = "white-space: pre"> </span> hadoop jar $ hadoop_home/hadoop-*-examples. Jar PI 100 100000000

Meanings of the following two numeric parameters:
1st 100 100 indicates that map tasks are to be run.
The number 2nd indicates how many times each map task will throw.

The product of the two parameters is the total number of throws.

My running results:


Iii. Summary

The PI Calculation Method in hadoop examples is a statistical method that uses a large number of samples, or a data-intensive job.

Reprinted please indicate the source: http://www.ming-yue.cn/hadoop-pi/


Use hadoop to calculate pi values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.