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?

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

Posted at

前回の記事↓

正方形を敷き詰めるやり方。

このやり方では正解より必ず小さな値に成る。
pai.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[])
{
			double  n=1000.0f ;

	if(argc > 1 )
	{
		n=atof( argv[1] ) ;		
	}
    double x,y,xx,yy;
	long long i;

	now();
	//--- --- --- ---
		do{
				double hen = 2.0f / n ;
				double henf = hen / 2.0f ;
				double sum = hen * hen ; // 面積
				double len = 0.0f;
				for(x=henf ; x <= 1.0f -henf ; x+= hen)
				{
						xx = x + henf ;
						xx *= xx ;
						for(y=henf ; y <= 1.0f-henf ;y+=hen)
						{
								yy = y + henf ;
								yy *= yy ;
								if(xx + yy <= 1.0f) len += 1.0f ;
						}
				}
				printf("%1.15f log10(%.3f)分割 ",4.0f * len * sum ,log10(n));
				now();
				n *= 1.03f;
		}while(1);
	return 0;
}

出力結果.
3.141512401600000 log10(5.000)分割 11:54:09
3.141514585306505 log10(5.013)分割 11:54:22
3.141516901724735 log10(5.026)分割 11:54:35
3.141519624956707 log10(5.039)分割 11:54:50
3.141521733419711 log10(5.051)分割 11:55:07
3.141523616809169 log10(5.064)分割 11:55:23
3.141525718581799 log10(5.077)分割 11:55:40
3.141527528977863 log10(5.090)分割 11:55:59
3.141529637102726 log10(5.103)分割 11:56:18
3.141531361794161 log10(5.116)分割 11:56:39
3.141532997960393 log10(5.128)分割 11:57:02
3.141534862594533 log10(5.141)分割 11:57:25
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?