A good place for algorithmic exercise learning-with different language implementations

Source: Internet
Author: User
Tags pow

This is not a personal pyramid, but in the course of learning to find a better place, so recommend to everyone, hope to be helpful ~

Although there is only one link, but the weight of the link is there, do not know how to send to the front page?

In order to make up the number of words, the following gives an article in the above site of a topic analysis:

------------------------------

Link Address: http://openhome.cc/Gossip/AlgorithmGossip/MathPI.htm

From[email protected]Algorithm Gossip: Monte Carlo method to seek PI to explain

Monte Carlo is the capital of the Kingdom of Morocco, the country of France and Italy, with a gambling name. The basic principle of Monte Carlo is to solve problems in the form of chaotic mating area formulas, which have the meaning of gambling in the way of the problem of the machine rate, although it is doubtful about the accuracy, but the thinking direction of the problem is a way worth learning.

Solution

Monte Carlo's solution is suitable for problems related to the area, such as the PI value or the elliptical orbits circle area, and this section is about how to find the pi value; there is a half diameter of 1, so the One-fourth circle is pi, and the One-fourth Circle Square is 1, as shown in the following illustration:


If the target is to be projected in a square (dot), then some of these targets will fall within One-fourth circles, the projection of the flight target (point) has n points, in the circle of the target (dot) c point, the proportion of the calculation, you get the last formula in the map.

As to how the result of the sentence falls within the circle, it is simple to make the chaos produce X and Y values, if the x^2+y^2 is less than 1 and falls in the circle.

Reality: C Java Python Scala Ruby JavaScript Haskell
    • C




#define N 50001




int sum = 0;

int i;



if ((x * x + y * y) < 1) {

}


printf ("PI =%f\n", (double) 4 * sum/(N-1));


    • Java
Import static java.lang.math.*;
public class Montecarlo {
public static void Main (string[] args) {
Final int N = 50001;

for (int i = 1; i < N; i++) if (POW (random (), 2) + POW (random (), 2) < 1) {
sum++;
}
System.out.printf ("PI =%f%n", 4.0 * Sum/(N-1));
}
}

    • Python
From random import random
N = 50001

if random () * * 2 + random () * * 2 < 1])/(N-1))

    • Scala
Import Java.lang.math._
Val N = 50000
printf ("PI =%f%n", 4.0 * (for (i <-1 to n
if (POW (random (), 2) + POW (random (), 2) < 1)) yield 1). size/n)

    • Ruby
N = 50000

Rand * * 2 + Rand * * 2 < 1? 1:0}.reduce (: +)/N

    • Javascript
Array.prototype.reduce = function (init, f) {
var value = init;
for (var i = 0; i < this.length; i++) {
Value = f (value, this[i]);
}
return value;
};

function range (n) {
var r = [];
for (var i = 0; i < n; i++) {
R[i] = i;
}
return R;
}

var n = 50000;
Print (4 * range (n). Map (function () {
var x = Math.random ();
var y = math.random ();
return x * x + y * y < 1? 1:0;
}). reduce (0, function (AC, elem) {
return AC + elem;
})/n);

    • Haskell
Import System.Random

Rand Gen n= take N $ randomrs (0.0, 1.0) Gen::[float]

Main = Do
Gen1 <-Getstdgen
Gen2 <-Newstdgen
Let n = 50000

Zip (Rand gen1 N) (Rand Gen2 N), (x * * 2 + y * * 2) < 1]
Print (4 * fromintegral ic/fromintegral N)

A good place for algorithmic exercise learning-with different language implementations

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.