[ create a new paste ] login | about

Link: http://codepad.org/iSGk2FuE    [ raw code | fork ]

C++, pasted on May 25:
#include <iostream>
#include <ctime>
#include <cmath>
#include <boost/random.hpp>

class Napier {
  int n;
  int* box;
public:
  Napier(int nn) : n(nn), box(new int[n]) {}
  ~Napier() { delete[] box; }
  double napiers() {
    boost::mt19937 gen(static_cast<unsigned long>(std::time(0)));
    boost::uniform_real<> range(0, 1);
    boost::variate_generator<boost::mt19937&, boost::uniform_real<> > randomValue(gen, range);

    for (int i = 0; i < n; i ++) // 箱を1で初期化
      box[i] = 1;

    int cnt = 0;
    int j;

    for (int i = 0; i < n; i ++) {
      j = static_cast<int>(n * randomValue()); // 入る箱の番号
      box[j] = 0;
    }
    for (int i = 0; i < n; i ++)  // 残った箱をカウント
      cnt += box[i];

    double napier = static_cast<double>(n) / cnt;
    return napier;
  }
};

int main()
{
  const int CNTS = 10000000;
  Napier np(CNTS);

  std::cout << np.napiers() << std::endl;
  std::cout << "Real Napier Numbers = " << std::exp(1.0) << std::endl;
}


Create a new paste based on this one


Comments: