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?

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

Last updated at Posted at 2025-02-13
kome_pi.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[])
{
	unsigned long long  n=8 ;

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

	long len = n ;
		for(i=0.0f;i<len;i+=1.0f) n *= 2.0f;

	printf("2^%dまで計算可能,start=%ld \n",sizeof(n)*8,n );
		now();
	while(1)
	{
		double ai = 2.0f / (float)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 ,{%d},",4.0f* en / all ,len);

		now();
		n *= 2 ;
		len++ ;
	}
	return 0;
}

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?