Last active
January 15, 2018 16:50
-
-
Save ScratchyCode/8966638ebecfa8d8a486b8304ab21013 to your computer and use it in GitHub Desktop.
Some programs that implement algorithms wich approximate the pi value.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coded by ScratchyCode | |
/* | |
This program approximates the value of Pi with a rapid decimal expansion thanks to a summation, | |
that converges to the pi value very quickly, called Bailey-Borwein-Plouffe, discovered in 1995. | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
long double BBPpigreco(long long int i); | |
int main(){ | |
long long int i; | |
double sum=0; | |
do{ | |
printf("\nEnter the number N of iterations (N > 0) to approximate Pi with the BBP summation: "); | |
scanf("%lld",&i); | |
}while(i <= 0); | |
sum = BBPpigreco(i); | |
printf("\nThe calculated Pi value is:\n%.48lf\n",sum); | |
return 0; | |
} | |
long double BBPpigreco(long long int i){ | |
long double k, sum=0; | |
for(k=0; k<i; k++){ | |
sum = sum + ((4/(8*k+1))-(2/(8*k+4))-(1/(8*k+5))-(1/(8*k+6)))*(1/(pow(16,k))); | |
} | |
return sum; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** Buffon's needle simulations *** | |
Needle len: 5 | |
Lines distance: 8 | |
Launches: 50 | |
Mean valure: 3.232631 | |
Minimum valure: 1.953125 | |
Maximum valure: 8.928571 | |
Launches: 100 | |
Mean valure: 3.196333 | |
Minimum valure: 2.155172 | |
Maximum valure: 6.250000 | |
Launches: 1000 | |
Mean valure: 3.149384 | |
Minimum valure: 2.821670 | |
Maximum valure: 3.521127 | |
Launches: 10000 | |
Mean valure: 3.141393 | |
Minimum valure: 3.027367 | |
Maximum valure: 3.260303 | |
Launches: 100000 | |
Mean valure: 3.141506 | |
Minimum valure: 3.102353 | |
Maximum valure: 3.187718 | |
Launches: 1000000 | |
Mean valure: 3.141689 | |
Minimum valure: 3.126086 | |
Maximum valure: 3.152426 | |
Launches: 10000000 | |
Mean valure: 3.141605 | |
Minimum valure: 3.137224 | |
Maximum valure: 3.145644 | |
Launches: 100000000 | |
Mean valure: 3.141583 | |
Minimum valure: 3.140220 | |
Maximum valure: 3.142722 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coded by ScratchyCode | |
// Approximate pi value with Buffon's needle | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <time.h> | |
#define MAX_THETA (M_PI/2) | |
#define SIMULATIONS 1000 | |
double input1(); | |
int input2(); | |
double realRand(double min, double max); | |
double minimum(double array[], int dim); | |
double maximum(double array[], int dim); | |
void statistics(double array[], int len, double *mean, double *min, double *max); | |
int main(){ | |
double L, d, theta, x, pi; | |
int N, S, i, j; | |
srand48(time(NULL)); | |
do{ | |
printf("Enter the length 'L' of Buffon's needle (0cm < L): "); | |
L = input1(); | |
printf("Enter the distance 'd' between the parallel lines (0cm < d): "); | |
d = input1(); | |
if(L > d){ | |
printf("\nThe needle length must be less than the distance between lines.\nTry again: \n"); | |
} | |
}while(L > d); | |
printf("Enter the number of launches to be simulated: "); | |
N = input2(); | |
double *array = calloc(SIMULATIONS,sizeof(double)); | |
for(i=0; i<SIMULATIONS; i++){ | |
S = 0; | |
for(j=0; j<N; j++){ | |
theta = realRand(0,MAX_THETA); | |
x = realRand(0,d/2); | |
if(x < ((L/2) * sin(theta))){ | |
S++; | |
} | |
} | |
pi = (2*L*N) / (S*d); | |
array[i] = pi; | |
} | |
FILE *write = fopen("pi.dat","w"); | |
if(write == NULL){ | |
perror("\nError"); | |
exit(1); | |
} | |
for(i=0; i<SIMULATIONS; i++){ | |
fprintf(write,"%lf\n",array[i]); | |
} | |
fclose(write); | |
double mean, min, max; | |
statistics(array,SIMULATIONS,&mean,&min,&max); | |
printf("\nMean value:\t%lf",mean); | |
printf("\nMinimum value:\t%lf",min); | |
printf("\nMaximum value:\t%lf\n",max); | |
free(array); | |
return 0; | |
} | |
double input1(){ | |
double value; | |
do{ | |
scanf("%lf",&value); | |
if(value <= 0){ | |
printf("\nThe value must be greater than 0!\nTry again: "); | |
} | |
}while(value <= 0); | |
return value; | |
} | |
int input2(){ | |
int value; | |
do{ | |
scanf("%d",&value); | |
if(value <= 0){ | |
printf("\nThe number of launches must be greater than 0!\nTry again: "); | |
} | |
}while(value <= 0); | |
return value; | |
} | |
double realRand(double min, double max){ | |
if(min >= max){ | |
printf("\nErrore: maximum value less than or equal to the minimum value entered!\n"); | |
exit(1); | |
} | |
double range = max - min; | |
double denom = RAND_MAX / range; | |
return min + lrand48() / denom; | |
} | |
double average(double array[], int len){ | |
int i; | |
double sum=0; | |
for(i=0; i<len; i++){ | |
sum += array[i]; | |
} | |
sum /= len; | |
return sum; | |
} | |
double maximum(double array[], int dim){ | |
int i; | |
double max=-1E20; | |
for(i=0; i<dim; i++){ | |
if(max < array[i]){ | |
max = array[i]; | |
} | |
} | |
return max; | |
} | |
double minimum(double array[], int dim){ | |
int i; | |
double min=1E20; | |
for(i=0; i<dim; i++){ | |
if(min > array[i]){ | |
min = array[i]; | |
} | |
} | |
return min; | |
} | |
void statistics(double array[], int len, double *mean, double *min, double *max){ | |
*mean = average(array,len); | |
*min = minimum(array,len); | |
*max = maximum(array,len); | |
return ; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coded by ScratchyCode | |
// The program approximates the decimal digits of pi through a infinite products that uses primes numbers. | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
long long int control(register long long int num); | |
int main(){ | |
long long int i=2, j=0, num=3, lim; | |
double prod=1, pi; | |
do{ | |
printf("Enter the number of primes to use: "); | |
scanf("%lld",&lim); | |
if(lim <= 0){ | |
printf("\nThe number of primes must be greater than zero.\n"); | |
exit(1); | |
} | |
}while(lim <= 0); | |
long long int *primes = malloc(lim * sizeof(long long int)); | |
if(primes == NULL){ | |
perror("\nError: "); | |
exit(0); | |
} | |
primes[0] = 1; | |
primes[1] = 2; | |
// look for required primes number | |
while(j != lim){ | |
if(num%2 != 0){ // exclusion of parity | |
if(control(num)){ | |
primes[i] = num; | |
i++; // array index | |
j++; // number of primes found | |
} | |
} | |
num++; // number to test | |
} | |
// show the primes found | |
printf("\nNumeri primes usati:\n"); | |
for(i=0; i<j; i++){ | |
printf("%lld ",primes[i]); | |
} | |
printf("\n"); | |
for(i=1; i<j; i++){ | |
prod = prod * (1 - (1/(pow((double)primes[i],(double)2)))); | |
} | |
// calculation of pi | |
pi = sqrt(6/prod); | |
printf("\nApproximated pi value with %lld primes numbers:\n%0.30lf\n",j,pi); | |
free(primes); | |
primes = NULL; | |
return 0; | |
} | |
long long int control(long long int num){ | |
long long int i; | |
long long int tmp; | |
tmp = (int)sqrt(num); | |
for(i=2; i<=tmp; i++) | |
if(i%2 != 0 && num%i == 0) | |
return 0; | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coded by ScratchyCode | |
// Simple program that, given an input number, iterates the Wallis formula to approximate pi. | |
#include <stdio.h> | |
#include <math.h> | |
int main(){ | |
int choice=0; | |
double n, i, num, denom; | |
double result=1; | |
do{ | |
printf("\nEnter a positive natural number to approximate pi: "); | |
scanf("%lf",&n); | |
}while(n <= 0); | |
num = 0; | |
denom = 0; | |
result = 1; | |
for(i=1; i<=n; i++){ | |
num = (4*pow(i,2)); | |
denom = ((4*pow(i,2))-1); | |
result = result*(num/denom); | |
} | |
result = result * 2; | |
printf("The approximate pi value is: %0.30lf\n",result); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment