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?

10th,円周率を三角関数を使わず計算する【C言語】

Posted at

正三角形をしきつめたり、
素数を使ったり色々やって
第10段目。

平行四辺形を埋めて計算する

pai10.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);
}

double yret(double x)
{
	return sqrtf(1.0f - x*x);
}

int main(int argc,char *argv[])
{
	unsigned long long	nnn = time(NULL),o=0;
	double n=10.0f;
	if(argc > 1 )
	{
		n=atof( argv[1] ) ;
	}
	printf("---START---");	now();//時刻表示
	//--- --- --- ---
	
	double ksns;
	double x ,y ;
	double s ;
	int m=10;

	while(m)
	{
		s=0.0f ;
		ksns = 1.0f / n ;
		for(x=ksns ; x <=1.0f ; x += ksns)
		{
			y = yret( x ) ;
			s += ksns * y ;
		}

		nnn = time(NULL);
		if ( nnn >= o)
		{
			o = nnn + 3 ;
			printf("%.12f ,e%.2f割",s*4.0f,log10f(n));
			now();
		}
		
		n *= 1.03f;
//		m--;
	}
	return 0;
}

中々優秀で1分もしないうちに、
3.141592まで正解出来た

log.log
---START---20:16:08
2.904518324137 ,e1.00割20:16:08
3.141592438298 ,e6.97割20:16:11
3.141592540680 ,e7.25割20:16:14
3.141592577478 ,e7.42割20:16:17
3.141592597510 ,e7.55割20:16:20
3.141592603984 ,e7.64割20:16:23

正三角形埋めの時は5時間掛けても
3.1415までしか近づかなかった。

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?