Archive for January, 2009

Pi

January 21, 2009

circle1

 

A simple program to computing Pi using the Monte Carlo method:

 

#include “stdlib.h”

#include “stdio.h”

#include “time.h”


int isinside(int , int);


int main()

{

        srand(time(NULL));

        double pi;

        int x,y;

        int i=0;

        int inside=0;

        int n=10000;


        while(i<n){

                x=rand()%11-5;

                y=rand()%11-5;

                if(isinside(x,y)==1)

                        inside++;

                i++;

        }

        printf(“%d\n”,inside);

        printf(“pi = %lf\n”,inside*4.0/n);

}


int isinside(int x,int y)

{

        if(x<0)

                x*=-1;

        if(y<0)

                y*=-1;

        if((x==5 && y==0) || (x==4 && y<4) || (x==3 && y<5) || (x==2 && y<5) || (x==1 && y<5) || x==0 )

                return 1;

        else

                return 0;

}


bramani@chassis01:/home/bramani/toyprojs> cc pi.c

bramani@chassis01:/home/bramani/toyprojs> a.out

6662

pi = 2.664800 [oops]

bramani@chassis01:/home/bramani/toyprojs> a.out

6812

pi = 2.724800 [aiyo]

 

These pi values are for r=5.

As r increases pi tends to 3.14. 

 

 

I loved this experiment and thought of sharing.
Inspired by Fooled by randomness ~ Nassim Nicholas Taleb.
Advertisements

I am. No, I am Not.

January 7, 2009

There was Schrodinger. There was no Schrodinger.

Pythoned

January 3, 2009

Was dabbling with python this lazy saturday afternoon.

Surprise.  I could write and run useful programs in no time at all.

I am Impressed.  This is very true.

 

PYTHON ROCKS !

A to Z

January 2, 2009

A to Z.