0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

2nd,円周率を「米粒を敷き詰めて計算」する方法【C言語】

Posted at

前回の記事は


今回は↓

kome_pi_2.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


void now(void)
{
	time_t t = time(NULL);
	struct tm *local = localtime(&t);
/*
	printf("%04d/", local->tm_year + 1900);
	printf("%02d/", local->tm_mon + 1);
	printf("%02d", local->tm_mday);

	printf(" ");
*/
	printf("%02d:", local->tm_hour);
	printf("%02d:", local->tm_min);
	printf("%02d\n", local->tm_sec);
	fflush(stdout);
}

int main(int argc,char *argv[])
{
	float  n=8 ;

	if(argc > 1 )
	{
		n=atof( argv[1] ) ;		
	}
    double i,k ,all,en;


	now();
	while(1)
	{
		double ai = 2.0f / n;
		en = 0.0f ;
		for(i=-1.0f ; i<=1.0f ; i+=ai)
		{
				for(k=-1.0f ; k<=1.0f ; k += ai)
				{

						if( i*i + k*k <= 1.0f) en+=1.0f ;
				}
     	}
		all = (float)n ;
		all *= all ;
		printf("π=%.20f ,log10(%.3f)分割,",4.0f* en / all ,log10f(n));
		now();
		n *= 1.03f ;
	}
	return 0;
}

これをgccでコンパイルするには、-lmオプションが必要で

gcc -lm pi.c -o pi

てする。


実行結果

./pai 47000

π=3.14159185822306241676 ,log10(4.663)分割,07:08:02
π=3.14159174870428703485 ,log10(4.676)分割,07:08:11
π=3.14159258840777200206 ,log10(4.688)分割,07:08:21
π=3.14159269341565083877 ,log10(4.701)分割,07:08:31
π=3.14159238385188466225 ,log10(4.714)分割,07:08:43
π=3.14159216209102698514 ,log10(4.727)分割,07:08:55
π=3.14159242503394686707 ,log10(4.740)分割,07:09:07
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?