CPIARCH means - Compute Pi Archimedes formula.
CPIARCH - C source code
// Calculate the value of pi with use Archimedes method started from hexagon with side length equal to initiated radius value.
// Program is comparing Archimedes Pi with MPFR Canonical Pi.
// CPIARCH algorithm. Compute Pi - Archimedes algorithm
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate MPFR Canonical Pi and other Archimedes Pi value from polygons to an arbitrary degree of precision.
// There is no warranty or guarantee of any kind.
// The mpfr library has further restrictions.
// To Compile:
// gcc -o cpiarch cpiarch.c -lmpfr
// Usage in command line:
// ./cpiarch 1000 1000 1000 // bigger values gives better approximation
// More info at YouTube :
// Finding Pi by Archimedes' Method https://www.youtube.com/watch?v=_rJdkhlWZVQ on MathWithoutBorders channel.
// Finding Pi by Archimedes' Method (Follow-up) https://www.youtube.com/watch?v=9zO0-QOcJQ0 on MathWithoutBorders channel.
//
#include
#include
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
int cpiarch(char *stop, int radius, int decimals)
{
mpfr_t n, s1, s2, a, b, news, pin, pdin, pout, pdout, r, pow_r, temp1, temp2, temp3, i, six, sixtimes, div, pi, goldenpi, middlepi, threepi, archimedespi;
mpfr_inits2(decimals, n, s1, s2, a, b, news, pin, pdin, pout, pdout, r, pow_r, temp1, temp2, temp3, i, six, sixtimes, div, pi, archimedespi, NULL);
// a = sqrt(1-((b/2)^2)) = sqrt(1-0.25) = sqrt(0.75) = 0,866025404
// b = 1 - a = 0,133974596
// news = sqrt(b^2 + s2^2)
// pin = six * s1,
// pout = pin * r/a
// pdout = pout/2 = is PI oversized
// Initialize hexagon and circle data
mpfr_set_ui(r, radius, MPFR_RNDN); // radius 1
mpfr_set(s1, r, MPFR_RNDN); // side s1 of hex = radius
mpfr_set_ui(six, 6, MPFR_RNDN); // 6 sides
mpfr_set_ui(sixtimes, 6, MPFR_RNDN); // 6 sides
mpfr_mul(pin, six, s1, MPFR_RNDN); // inner circumference 6 times s1
mpfr_div_ui(s2, s1, 2, MPFR_RNDN); // half of the side if s1 = 1 => s2 = 0.5
mpfr_pow_ui(temp1, s2, 2, MPFR_RNDN); // s2 power to 2
mpfr_pow_ui(pow_r, r, 2, MPFR_RNDN);
mpfr_sub(temp2, pow_r, temp1, MPFR_RNDN); // 1 - (s/2)^2
mpfr_sqrt(a, temp2, MPFR_RNDD); // sqrt(1 - (s/2)^2)
mpfr_sub(b, r, a, MPFR_RNDD); // 1 - (s/2)^2 // 1 - 0.866
// From Pythagorean theorem we can find the new polygon news side lenght value
// news = sqrt(b^2 + s2^2)
mpfr_pow_ui(temp3, b, 2, MPFR_RNDN); // b^2
mpfr_add(temp3, temp1, temp3, MPFR_RNDN); // (s2^2+b^2)
mpfr_sqrt(temp3,temp3,MPFR_RNDN); // news
mpfr_set(news, temp3, MPFR_RNDN);
mpfr_mul_ui(div, r, 2, MPFR_RNDN); // div = r * 2
// PIN Perimeter of inner polygon
mpfr_mul(pin, six, s1, MPFR_RNDN);
mpfr_div(pdin, pin, div, MPFR_RNDN);
// POUT Perimeter of outer polygon
mpfr_div(temp1, r, a, MPFR_RNDN);
mpfr_mul(pout, pin, temp1, MPFR_RNDN);
mpfr_div(pdout, pout, div, MPFR_RNDN);
mpfr_printf("n ,");
mpfr_printf("s ,");
mpfr_printf("s/2 ,");
mpfr_printf("a ,");
mpfr_printf("b ,");
mpfr_printf("new s ,");
mpfr_printf("p (in) ,");
mpfr_printf("p/d (in) ,");
mpfr_printf("p (out) ,");
mpfr_printf("p/d (out) ");
mpfr_printf("\n");
mpfr_printf("%.0RNf,", sixtimes);
mpfr_printf("%.0RNf,", s1);
mpfr_printf("%.6RZf,", s2);
mpfr_printf("%.*RZf,", decimals, a);
mpfr_printf("%.*RZf,", decimals, b);
mpfr_printf("%.*RZf,", decimals, news);
mpfr_printf("%.*RZf,", decimals, pin);
mpfr_printf("%.*RZf,", decimals, pdin);
mpfr_printf("%.*RZf,", decimals, pout);
mpfr_printf("%.*RZf", decimals, pdout);
mpfr_printf("\n");
mpfr_set_si (i, 0, MPFR_RNDD); // step
mpfr_set_str (n, stop, 10, MPFR_RNDD); // limit of the loop
while(mpfr_cmpabs(i,n)<0)
{
mpfr_sub_si(i, i, 1, MPFR_RNDD); //// or add_si
mpfr_mul_ui(sixtimes,sixtimes,2, MPFR_RNDN); // 12 * 2 sides
mpfr_set(s1, news, MPFR_RNDN); //
mpfr_div_d(s2, s1, 2.0, MPFR_RNDN); // s2 half of the side s1
mpfr_pow_ui(temp1, s2, 2, MPFR_RNDN); // temp1 power of 2
mpfr_sub(temp2, pow_r, temp1, MPFR_RNDN); // 1 - (b/2)^2
mpfr_sqrt(a, temp2, MPFR_RNDN); // sqrt(1 - (b/2)^2)
mpfr_sub(b, r, a, MPFR_RNDN); // 1 - sqrt(1 - (b/2)^2) // 1 - 0.866
//From pythagorean theorem we can find the new polygon S/2 side lenght value
//news = sqrt(b^2 + s2^2)
mpfr_pow_ui(temp3, b, 2, MPFR_RNDN); // b^2
mpfr_add(temp3, temp1, temp3, MPFR_RNDN);
mpfr_sqrt(news,temp3,MPFR_RNDN); // new s
// PIN Perimeter of inner polygon
mpfr_mul(pin, sixtimes, s1, MPFR_RNDN);
mpfr_div(pdin, pin, div, MPFR_RNDN);
// POUT Perimeter of outer polygon
mpfr_div(temp1, r, a, MPFR_RNDN);
mpfr_mul(pout, pin, temp1, MPFR_RNDN);
mpfr_div(pdout, pout, div, MPFR_RNDN);
/* Uncomment to print all results from the loop
mpfr_printf("%.0RNf,", sixtimes);
mpfr_printf("%.0RNf,", s1);
mpfr_printf("%.6RZf,", s2);
mpfr_printf("%.*RZf,", decimals, a);
mpfr_printf("%.*RZf,", decimals, b);
mpfr_printf("%.*RZf,", decimals, news);
mpfr_printf("%.*RZf,", decimals, pin);
mpfr_printf("%.*RZf,", decimals, pdin);
mpfr_printf("%.*RZf,", decimals, pout);
mpfr_printf("%.*RZf", decimals, pdout);
mpfr_printf("\n");
*/
}
/* Print out last result from the loop */
mpfr_printf("%.0RNf,", sixtimes);
mpfr_printf("%.0RNf,", s1);
mpfr_printf("%.6RZf,", s2);
mpfr_printf("%.*RZf,", decimals, a);
mpfr_printf("%.*RZf,", decimals, b);
mpfr_printf("%.*RZf,", decimals, news);
mpfr_printf("%.*RZf,", decimals, pin);
mpfr_printf("%.*RZf,", decimals, pdin);
mpfr_printf("%.*RZf,", decimals, pout);
mpfr_printf("%.*RZf", decimals, pdout);
mpfr_printf("\n");
mpfr_const_pi(pi, MPFR_RNDN); // MPFR Canonical Pi computed with Brent Salamin formula
mpfr_add(archimedespi, pdout, pdin, MPFR_RNDN); // Archimedes Pi (Outer Pi from polygon + Inner Pi from polygon) / 2
mpfr_div_d(archimedespi, archimedespi, 2.0, MPFR_RNDN);
mpfr_printf("Final results for the polygon with n sides where n = %.0RZf:", sixtimes);
mpfr_printf("\n");
mpfr_printf("MPFR Canonical Pi %.*RZf", decimals, pi);
mpfr_printf("\n");
mpfr_printf("Archimedes Pi %.*RZf", decimals, archimedespi);
mpfr_printf("\n");
// MPFR Canonical Pi
if(mpfr_cmpabs(pi,pdin)>0)
{
mpfr_printf("MPFR Canonical Pi value higher than inner Pi derived from polygon. OK.");
mpfr_printf("\n");
}
else
{
mpfr_printf("MPFR Canonical Pi value lower than inner Pi derived from polygon. BAD!");
mpfr_printf("\n");
}
if(mpfr_cmpabs(pi,pdout)<0)
{
mpfr_printf("MPFR Canonical Pi value lower than outer Pi derived from polygon. OK.");
mpfr_printf("\n");
}
else
{
mpfr_printf("MPFR Canonical Pi value higher than outer Pi derived from polygon. BAD!.");
mpfr_printf("\n");
}
if(mpfr_cmpabs(pi,archimedespi)<0)
{
mpfr_printf("MPFR Canonical Pi is lower than Archimedes Pi derived from 2 polygons (outer and inner). Why? Test more.");
mpfr_printf("\n");
}
if(mpfr_cmpabs(pi,archimedespi)>0)
{
mpfr_printf("MPFR Canonical Pi is higher than Archimedes Pi derived from 2 polygons (outer and inner). Why? Test more.");
mpfr_printf("\n");
}
if(mpfr_cmpabs(pi,pdin)==0)
{
mpfr_printf("MPFR Canonical Pi equal to inner Pi derived from polygon. SUPER.");
mpfr_printf("\n");
}
if(mpfr_cmpabs(pi,pdout)==0)
{
mpfr_printf("MPFR Canonical Pi equal to outer Pi derived from polygon. SUPER.");
mpfr_printf("\n");
}
if(mpfr_cmpabs(pi,archimedespi)==0)
{
mpfr_printf("MPFR Canonical Pi equal to Archimedes Pi derived from polygon. SUPER.");
mpfr_printf("\n");
}
// Clean up
mpfr_clears(n, s1, s2, a, b, news, pin, pdin, pout, pdout, r, pow_r, temp1, temp2, temp3, i, six, sixtimes, div, pi, archimedespi, NULL);
}
int main(int argc, char * argv[]) {
int r, d;
char * i;
if (argc <= 3)
{
printf ("Usage: %s \n", argv[0]);
return 1;
}
i = argv[1];
assert(i != NULL);
r = atoi(argv[2]); // radius of the circle from 1 to MAX_LONG_INT
assert( r >= 1);
d = atoi(argv[3]); // decimal places from 10 to 1000000 or more
assert( d >= 1);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
cpiarch(i, r, d); // Change the argument i to adjust the number of iterations and change argument r to set radius of the circle and change argument d for decimal precision of the values of variables
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
mpfr_free_cache ();
return 0;
}
CPIARCH algorithm related data.
CPILM means - Compute Pi Long Madhava formula.
CPILM - C source code
// Madhava iterative algorithm for Pi approximation
// CPILM - COMPUTE PI LONG MADHAVA
// More info: https://publications.azimpremjiuniversity.edu.in/2757/1/17_Mayadhar_TwoFamousSeriesForPI.pdf
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate pi to an arbitrary degree of precision.
// There is no warranty or guarantee of any kind.
// The mpfr library has further restrictions.
// To Compile:
// gcc -o cpilm cpilm.c -lmpfr
// Usage:
// ./cpilm 1000 1000
#include
#include // for floating point mumbers
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
int cpilm(char *stop, unsigned int *bt){
/* Applying Classic Madhava Formula */
/*
for(i=0;i< *stop;i++)
{
term = pow(-1, i) / ((2*i+1)*3^i);
sum += term;
}
pi = (2*sqrt(3)) * sum;
printf("\nPI = %.16lf \n", pi);
*/
mpfr_t sum3, term3, pi3, i3, n3, x3, i2, one3,two3, three3, power3, power_three3, div3, div33;
mpfr_inits2 (*bt, sum3, term3, pi3, i3, n3, x3, i2, one3, two3, three3, power3, power_three3, div3, div33, NULL);
mpfr_set_si (i3, 0, MPFR_RNDD);
mpfr_set_si(x3, 0, MPFR_RNDD);
mpfr_set_d (sum3, 0.0, MPFR_RNDD);
mpfr_set_d (term3, 0.0, MPFR_RNDD);
mpfr_set_d (div3, 0.0, MPFR_RNDD);
mpfr_set_si (one3, -1, MPFR_RNDD);
mpfr_set_si (two3, 2, MPFR_RNDD);
mpfr_set_si (three3, 3, MPFR_RNDD);
mpfr_set_d (pi3, 0.0, MPFR_RNDD);
mpfr_set_str (n3, stop, 10, MPFR_RNDD);
mpfr_sub_si(n3, n3, 1, MPFR_RNDD);
while(mpfr_cmpabs(i3,n3)<0)
{
mpfr_sub_si(i3, i3, 1, MPFR_RNDD);
mpfr_pow (power3, one3, x3, MPFR_RNDD);
mpfr_mul(div3, two3, x3, MPFR_RNDD);
mpfr_add_si(div3,div3,1,MPFR_RNDD);
mpfr_pow(power_three3,three3,x3,MPFR_RNDD);
mpfr_mul(div3, div3, power_three3, MPFR_RNDD);
mpfr_div(term3,power3,div3,MPFR_RNDD);
mpfr_add(sum3,sum3,term3,MPFR_RNDD);
mpfr_add_si(x3, x3, 1, MPFR_RNDD);
}
//Print put last - 1 answer
mpfr_sqrt(three3,three3,MPFR_RNDD);
mpfr_mul_si(three3,three3,2,MPFR_RNDD);
mpfr_mul(pi3,sum3,three3,MPFR_RNDD);
mpfr_printf ("\n===================\nPI for (n = %.*RZf): ",0,x3);
mpfr_out_str (stdout, 10, *bt, pi3, MPFR_RNDD);
printf ("\n===================\n\n");
// do one more step
mpfr_add_si(x3, x3, 1, MPFR_RNDD);
mpfr_pow (power3, one3, x3, MPFR_RNDD);
mpfr_mul(div3, two3, x3, MPFR_RNDD);
mpfr_add_si(div3,div3,1,MPFR_RNDD);
mpfr_pow(power_three3,three3,x3,MPFR_RNDD);
mpfr_mul(div3, div3, power_three3, MPFR_RNDD);
mpfr_div(term3,power3,div3,MPFR_RNDD);
mpfr_add(sum3,sum3,term3,MPFR_RNDD);
mpfr_mul(pi3,sum3,three3,MPFR_RNDD);
//Print out last answer
mpfr_printf ("\n===================\nPI for (n = %.*RZf): ",0,x3);
mpfr_out_str (stdout, 10, *bt, pi3, MPFR_RNDD);
printf ("\n===================\n\n");
mpfr_clears (sum3, term3, pi3, i3, n3, x3, i2, one3, two3, three3, power3, power_three3, div3, div33, NULL);
return 0;
}
int main(int argc, char * argv[]){
unsigned int b;
char * i;
if (argc <= 2){
printf ("Usage: %s \n", argv[0]);
return 1;
}
i = argv[1];
b = atoi(argv[2]);
assert(i != NULL);
assert( b >= 1);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
// Run the main procedure.
cpilm(i,&b); // change the arguments value to do more interations or use larger precision
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
mpfr_free_cache ();
return 0;
}
CPILM algorithm related data.
CPILL means - Compute Pi Long Leibniz formula.
CPILL - C source code
// Madhava–Leibniz, Gottfried Wilhelm Leibniz iterative algorithm for Pi approximation
// CPILL1 - COMPUTE PI LONG LEIBNIZ V1
//
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate pi to an arbitrary degree of precision.
// There is no warranty or guarantee of any kind.
// The mpfr library has further restrictions.
// To Compile:
// gcc -o cpill1 cpill1.c -lmpfr
// Usage:
// ./cpill1 1000 1000
#include
#include // for floating point mumbers
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
int cpill1(char *stop, unsigned int *bt){
/* Applying Classic Leibniz Formula V1 */
/*
for(i=0;i< *stop;i++)
{
term = pow(-1, i) / (2*i+1);
sum += term;
}
pi = 4 * sum;
printf("\nPI = %.16lf \n", pi);
*/
mpfr_t sum3, term3, pi3, i3, n3, x3, i2, one3,two3, power3, div3, div33;
mpfr_inits2 (*bt, sum3, term3, pi3, i3, n3, x3, i2, one3, two3, power3, div3, div33, NULL);
mpfr_set_si (i3, 0, MPFR_RNDD);
mpfr_set_si(x3, 0, MPFR_RNDD);
mpfr_set_d (sum3, 0.0, MPFR_RNDD);
mpfr_set_d (term3, 0.0, MPFR_RNDD);
mpfr_set_d (div3, 0.0, MPFR_RNDD);
mpfr_set_si (one3, -1, MPFR_RNDD);
mpfr_set_si (two3, 2, MPFR_RNDD);
mpfr_set_d (pi3, 0.0, MPFR_RNDD);
mpfr_set_str (n3, stop, 10, MPFR_RNDD);
mpfr_sub_si(i2, n3, 1, MPFR_RNDD);
while(mpfr_cmpabs(i3,i2)<0)
{
mpfr_add_si(i3, i3, 1, MPFR_RNDD);
mpfr_pow (power3, one3, x3, MPFR_RNDD);
mpfr_mul(div3, two3, x3, MPFR_RNDD);
mpfr_add_si(div3,div3,1,MPFR_RNDD);
mpfr_div(term3,power3,div3,MPFR_RNDD);
mpfr_add(sum3,sum3,term3,MPFR_RNDD);
mpfr_add_si(x3, x3, 1, MPFR_RNDD);
}
//Print out last - 1 answer
mpfr_mul_si(pi3,sum3,4,MPFR_RNDD);
mpfr_printf ("\n===================\nPI for (n = %.*RZf): ",0,i2);
mpfr_out_str (stdout, 10, *bt, pi3, MPFR_RNDD);
printf ("\n===================\n\n");
// Do one more step
mpfr_add_si(i3, i3, 1, MPFR_RNDD);
mpfr_pow (power3, one3, x3, MPFR_RNDD);
mpfr_mul(div3, two3, x3, MPFR_RNDD);
mpfr_add_si(div3,div3,1,MPFR_RNDD);
mpfr_div(term3,power3,div3,MPFR_RNDD);
mpfr_add(sum3,sum3,term3,MPFR_RNDD);
mpfr_add_si(x3, x3, 1, MPFR_RNDD);
mpfr_mul_si(pi3,sum3,4,MPFR_RNDD);
//Print put last answer
mpfr_printf ("\n===================\nPI for (n = %.*RZf): ",0,i3);
mpfr_out_str (stdout, 10, *bt, pi3, MPFR_RNDD);
printf ("\n===================\n\n");
mpfr_clears (sum3, term3, pi3, i3, n3, x3, i2, one3,two3, power3, div3, div33, NULL);
return 0;
}
int main(int argc, char * argv[]){
unsigned int b;
char * i;
if (argc <= 2){
printf ("Usage: %s \n", argv[0]);
return 1;
}
i = argv[1];
b = atoi(argv[2]);
assert(i != NULL);
assert( b >= 1);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
// Run the main procedure.
cpill1(i,&b);
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
mpfr_free_cache ();
return 0;
}
CPILL algorithm related data.
FFCPI means - Fibonacci Fast Compute Pi formula (This is only an approximation!!!)
FFCPI- C source code
// Calculate the approximation of value of Pi with use large numbers of Fibonacci sequence and/or Golden Phi number
// FFCPI algorithm - Fibonacci Fast Compute Pi
// Assuming that Pi = 6 * (Phi ^ 2) / 5
// we can have three versions of this algorithm
// FFCPI 1) Pi = (6 * (f(n) / f(n-1) ) ^ 2 ) / 5
// FFCPI 2) Pi = 6 * ( ( (sqrt(5) + 1) / 2 ) ^ 2) / 5
// FFCPI 3) Pi = 6 * ( ( (sqrt(5) + 1) / 2 ) + 1) / 5
// Author: Sylwester Bogusiak aka Sylvi91
// This is free code to calculate pi value to an arbitrary degree of precision.
// There is no warranty or guarantee of any kind.
// The mpfr library has further restrictions.
// To Compile:
// gcc -o ffcpi ffcpi.c -lmpfr
// Usage in command line:
// ./ffcpi 1000 1000
#include
#include
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
int ffcpi1(char *stop, long long int decimals){
mpfr_t pi, f_n, f_n_minus_1, temp, n, i;
mpfr_inits2(decimals, pi, f_n, f_n_minus_1, temp, n, i, NULL);
// Initialize Fibonacci numbers
mpfr_set_ui(f_n, 1, MPFR_RNDN);
mpfr_set_ui(f_n_minus_1, 0, MPFR_RNDN);
// Compute consecutive Fibonacci numbers
mpfr_set_si (i, 0, MPFR_RNDD);
mpfr_set_str (n, stop, 10, MPFR_RNDD);
while(mpfr_cmpabs(i,n)<0)
{
mpfr_add_si(i, i, 1, MPFR_RNDD); //// or add_si
mpfr_add(temp, f_n, f_n_minus_1, MPFR_RNDN);
// Swap Fibonacci numbers
mpfr_set(f_n_minus_1, f_n, MPFR_RNDN);
mpfr_set(f_n, temp, MPFR_RNDN);
}
// Compute Pi using Fibonacci numbers and other natural numbers
mpfr_div(pi, f_n, f_n_minus_1, MPFR_RNDN);
mpfr_sqr(pi, pi, MPFR_RNDN);
mpfr_mul_ui(pi, pi, 6, MPFR_RNDN);
mpfr_div_ui(pi, pi, 5, MPFR_RNDN);
// Set the precision for the result
mpfr_prec_round(pi, decimals, MPFR_RNDN);
mpfr_printf("\n Pi = (6 * (f(n) / f(n-1) ) ^ 2 ) / 5 For f(%.0RNf) Pi = \n ", i); // decimal points
// Print the calculated value of Pi
mpfr_printf("%.*RZf", decimals, pi);
printf("\n");
// Clean up
mpfr_clears(pi, f_n, f_n_minus_1, temp, NULL);
}
int ffcpi2(long long int decimals)
{
mpfr_t pi, phi, temp;
mpfr_inits2(decimals, pi, phi, temp, NULL);
// Compute Phi using (sqrt(5) + 1 ) / 2
mpfr_sqrt_ui(temp, 5, MPFR_RNDN);
mpfr_add_ui(temp, temp, 1, MPFR_RNDN);
mpfr_div_ui(phi, temp, 2, MPFR_RNDN);
// Compute Pi using Phi
mpfr_sqr(temp, phi, MPFR_RNDN);
mpfr_mul_ui(temp, temp, 6, MPFR_RNDN);
mpfr_div_ui(pi, temp, 5, MPFR_RNDN);
// Set the precision for the result
mpfr_prec_round(pi, decimals, MPFR_RNDN);
printf ("\n Pi = 6 * ( ( (sqrt(5) + 1) / 2 ) ^ 2) Pi = \n ");
// Print the calculated value of Pi
mpfr_out_str(stdout, 10, decimals, pi, MPFR_RNDN);
printf("\n");
// Clean up
mpfr_clears(pi, phi, temp, NULL);
}
int ffcpi3(long long int decimals)
{
mpfr_t pi, phi, temp;
mpfr_inits2(decimals, pi, phi, temp, NULL);
// Compute Phi using (sqrt(5) + 1 ) / 2
mpfr_sqrt_ui(temp, 5, MPFR_RNDN);
mpfr_add_ui(temp, temp, 1, MPFR_RNDN);
mpfr_div_ui(phi, temp, 2, MPFR_RNDN);
// Compute Pi using Phi
mpfr_add_d(temp, phi, 1.0, MPFR_RNDN);
mpfr_mul_ui(temp, temp, 6, MPFR_RNDN);
mpfr_div_ui(pi, temp, 5, MPFR_RNDN);
// Set the precision for the result
mpfr_prec_round(pi, decimals, MPFR_RNDN);
printf ("\n Pi = 6 * ( ( (sqrt(5) + 1) / 2 ) + 1) Pi = \n ");
// Print the calculated value of Pi
mpfr_out_str(stdout, 10, decimals, pi, MPFR_RNDN);
printf("\n");
// Clean up
mpfr_clears(pi, phi, temp, NULL);
}
int main(int argc, char * argv[]){
long long int b;
char * i;
if (argc <= 2){
printf ("Usage: %s \n", argv[0]);
return 1;
}
i = argv[1];
b = atoi(argv[2]);
assert(i != NULL);
assert( b >= 15);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
// Run the main procedure.
ffcpi1(i, b); // Change the argument to adjust the number of iterations and precision
ffcpi2(b); // Change the argument to adjust precision
ffcpi3(b); // Change the argument to adjust precision
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
mpfr_free_cache ();
return 0;
}
// Bye, bye, My computer with Fibonacci compute Pi ;) FCPI AKA FFCPI - fibonacci fast compute pi
FFCPI algorithm related data.
PTOPOT means - Primes To Power Of Two.
PTOPOT- C source code
// Calculate Primes set at given limit and show it as sum of geometrical set powers of two
// PTOPOT - PRIME TO POWERS OF TWO
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code
// There is no warranty or guarantee of any kind.
// To Compile:
// gcc -o ptopot ptopot.c -lm
// Usage:
// ./ptopot 1000
#include
#include
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
#define Phi 1.618033988749895
#define BITS 8
#define PIECES 7
// Check whether x is a prime or not
unsigned long long ifnotPrime(char prime[], unsigned long long x)
{
return (prime[x/BITS] & (1 << ((x >> 1) & PIECES) ) );
}
// Set the appropriate bit
unsigned long long makeComposite(char prime[], unsigned long long x)
{
prime[x/BITS] |= (1 << ((x >> 1) & PIECES)); /// ok for test fast ok
return 0;
}
// Returns the powers of two
unsigned long long int* primeToPot(unsigned long long int a, int *len){
int arrayLen=0;
unsigned long long i=1;
while (i0){
bits[arrayLen]=a&1;
if(bits[arrayLen] != 0)
{
bits[arrayLen] = pow(2,i);
}
i++;
arrayLen--;
a>>=1;
}
return bits;
}
////////////////////////////////////////////
int calc(unsigned long long int limit)
{
unsigned long long i,j,k;
int len = 32;
unsigned long long int * bits;
int a;
if (limit>=3)
{
printf("________________________________________________________________________\n");
printf("____________THAT APP IS CALCULATING THE PRIME NUMBERS SET_______________\n");
printf("_____UP TO THE GIVEN LIMIT AND DISPLAYS IT AS SET OF POWERS OF TWO______\n");
printf("_____________Author: Sylwester B aka Sylvi91____________________________\n");
printf("________________________________________________________________________\n");
printf("_______________Please wait for primes generator. Warning!_______________\n");
printf("___For argument greater than 1000000000 this may take couple minutes.___\n");
printf("______________Bigger limit consume more and more memory.________________\n");
printf("________________________________________________________________________\n");
char * prime;
prime = (char*) malloc(sizeof(char)*limit/BITS); // memory allocaion
if (prime==NULL) exit (1);
memset(prime, 0, sizeof(prime)); // init
// Eratosthenes Sieve
for (i = 3; i * i <= limit; i += 2) {
if (!ifnotPrime(prime, i))
for ( j = i * i, k = i << 1; j < limit; j += k)
makeComposite(prime, j);
}
// print primes
printf(" 2, ");
for (int i = 3; i <= limit; i += 2)
if (!ifnotPrime(prime, i))
{
printf("%d, ", i);
}
printf("\n");
// Print primes and powers of two
printf(" 2 2\n");
for (i=3;i<=limit;i+=2) /// main loop
{
if (!ifnotPrime(prime, i)) /// prime or not
{
bits = primeToPot(i,&len);
printf(" %llu ", i); /// wypisz jak chcesz
for (a=0;a\n", argv[0]);
return 1;
}
i = atoll(argv[1]);
assert(i >= 1);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
calc(i); // Change the argument i to adjust the limit of integers
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
return 0;
}
PTOPOT app related data.
PTOF means - Primes To Fibonacci. Zeckendorf greedy algorithm.
PTOF - C source code
// Calculate Primes set at given limit and show it as set of fibonacci numbers
// Use Zeckendorf's theorem. It finds representation of n as sum of non-neighbouring Fibonacci Numbers.
// PTOF - PRIMES TO FIBONACCI
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// There is no warranty or guarantee of any kind.
// To Compile:
// gcc -o ptof ptof.c
// Usage:
// ./ptof 1000
#include
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
#define Phi 1.618033988749895
#define BITS 8
#define PIECES 7
// Check whether x is a prime or not
unsigned long long ifnotPrime(char prime[], unsigned long long x)
{
return (prime[x/BITS] & (1 << ((x >> 1) & PIECES) ) );
}
// Set the appropriate bit
unsigned long long makeComposite(char prime[], unsigned long long x)
{
prime[x/BITS] |= (1 << ((x >> 1) & PIECES));
return 0;
}
// Returns the greatest Fibonacci Number smaller than
// or equal to n.
unsigned long long int nearestSmallerEqFib(int n)
{
// Corner cases
if (n == 0 || n == 1)
return n;
// Find the greatest Fibonacci Number smaller
// than n.
unsigned long long int f1 = 0, f2 = 1, f3 = 1;
while (f3 <= n)
{
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
return f2;
}
// Prints Fibonacci Representation of n using
// greedy algorithm
void printFibRepresntation(unsigned long long int n)
{
printf(" %llu ", n);
while (n > 0)
{
// Find the greates Fibonacci Number smaller
// than or equal to n
unsigned long long int f = nearestSmallerEqFib(n);
// Print the found fibonacci number
printf(" %llu ", f);
// Reduce n
n = n - f;
}
printf("\n");
}
// Calculate
int calc(unsigned long long int limit)
{
unsigned long long i,j,k;
if (limit>2)
{
printf("________________________________________________________________________\n");
printf("____________THAT APP IS CALCULATING THE PRIME NUMBERS SET_______________\n");
printf("______UP TO THE GIVEN LIMIT AND DISPLAYS IT AS SET OF FIBONACCI_________\n");
printf("_____________Author: Sylwester B aka Sylvi91____________________________\n");
printf("________________________________________________________________________\n");
printf("_______________Please wait for primes generator. Warning!_______________\n");
printf("___For argument greater than 1000000000 this may take couple minutes.___\n");
printf("______________Bigger limit consume more and more memory.________________\n");
printf("________________________________________________________________________\n");
char * prime;
prime = (char*) malloc(sizeof(char)*limit/BITS); // alokacja pamieci
if (prime==NULL) exit (1);
memset(prime, 0, sizeof(prime)); // init with 0
// Eratosthenes sieve.
for (i = 3; i * i <= limit; i += 2) {
if (!ifnotPrime(prime, i))
for ( j = i * i, k = i << 1; j < limit; j += k)
makeComposite(prime, j);
}
// print
printf("2, ");
for (int i = 3; i <= limit; i += 2)
if (!ifnotPrime(prime, i))
{
printf("%d, ", i);
}
printf("\n");
printf(" 2 2\n");
for (i=3;i<=limit;i+=2) // main loop
{
if (!ifnotPrime(prime, i)) /// prime or not
{
printFibRepresntation(i);
}
}
free(prime); // zwolnij pamiec
}
return 0;
}
// Main function
int main(int argc, char * argv[]) {
unsigned long long int i;
if (argc <= 1)
{
printf ("Usage: %s \n", argv[0]);
return 1;
}
i = atoll(argv[1]);
assert(i >= 2);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
calc(i); // Change the argument i to adjust the limit of integers
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
return 0;
}
PTOF app related data.
PTO1379 aka PTOQR means - Primes To Quadruple Rows of Integers. My own algorithm.
PTO1379 aka PTOQR - C source code
// Calculate Primes set at given limit and show it as a sets of four in a row (n*10)+1,(n*10)+3,(n*10)+7,(n*10)+9
//
// PTOQR - PRIME NUMBERS TO QUADRUPLE ROWS
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate Prime numbers with use std math library
// There is no warranty or guarantee of any kind.
// To Compile:
// gcc -o ptoqr ptoqr.c -lm
// Usage:
// ./ptoqr 1 1000
#include
#include
#include
#include
#include
#ifdef __APPLE__
#include
#else
#include
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// IS THIS PRIME NUMBER CLASSICAL VERSION
int is_this_prime_number(int natural_number)
{
int n, i, flag = 0;
n=natural_number;
for(i=2; i<=n/2; ++i)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
return 0; // 0 - jest prime
else
return 1;
//return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// IS THIS PRIME NUMBER 3-7 FOR 1379 METHOD
int is_this_prime_number_3_7(unsigned long long int natural_number)
{
unsigned long long int i, n, flag = 0;
n=natural_number;
// condition for nonprime number
if((n%3==0) || (n%7==0))
{
flag=1; // 1 - nonprime
return 1;
}
for(i=11; i<=sqrt(n); i=i+2)
{
// condition for nonprime number
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
return 0; // 0 - is prime
else
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// CALCULATE AND DISPLAY PRIMES
int calc_primes_1379(unsigned long long int begin, unsigned long long int end)
{
unsigned long long int i,n;
unsigned long long int a;
unsigned long long int row_sum=0; // sum of prime numbers in a row
unsigned long long int row_qty=0; // qty of prime numbers in a row
unsigned long long int total_qty=0; // total qty of prime numbers
int four[4] = {1,3,7,9};
if ((end>=begin) && (begin>=1))
{
printf("________________________________________________________________________\n");
printf("______________________ PTO1379 aka PTOQR v.0.1 BETA ____________________\n");
printf("____________________ PRIME NUMBERS TO QUADRUPLE ROWS ___________________\n");
printf("____________ THAT APP IS CALCULATING THE PRIME NUMBERS _________________\n");
printf("_____ AND DISPLAYS IT AS 4 COLUMNS OF INTEGERS ENDED WITH 1,3,7,9 ______\n");
printf("____________ Author: MARTE.BEST - Sylwester B aka Sylvi91 ______________\n");
printf("________________________________________________________________________\n");
printf("_______________ Please wait. Warning! _______________\n");
printf("__ For end limit greater than 100000000 this may take couple minutes. __\n");
printf("_______________ Bigger limit consume more and more time. _______________\n");
printf("________________________________________________________________________\n");
//print on screen
printf("* - PRIME, n - natural number\n ");
printf(" integers sum qty\n ");
if (begin == 1)
{
printf(" *2,*3,*5,*7, 17 4,\n ");
total_qty = 4;
}
for (n = begin; n < end; n ++)
{
row_sum=0;
row_qty=0;
for (i = 0; i < 4; i ++)
{
a = (n * 10) + four[i];
//a = ((n<<3) + (n<<1)) + four[i];
if (!is_this_prime_number_3_7(a))
// if (!is_this_prime_number(a))
{
row_sum += a; row_qty++;
printf(" *%llu, ", a);
total_qty++;
//printf("*");
}
else
{
printf(" n%llu, ", a);
//printf(" ");
}
}
printf(" %llu, ", row_sum);
printf(" %llu, ", row_qty);
printf("\n ");
}
printf(" Total primes quantity between %llu and %llu is equal to: %llu ", begin, end * 10, total_qty);
printf("\n ");
}
else
{
printf("Set properly begin and end of rows\n ");
}
return 0;
}
int main(int argc, char * argv[]) {
unsigned long long int b;
unsigned long long int e;
if (argc <= 2)
{
printf ("Usage: %s \n", argv[0]);
return 1;
}
b=atol(argv[1]);
e=atol(argv[2]);
assert(b >= 1);
assert(e >= 2);
// Get system time START
#ifdef __APPLE__
uint64_t start = mach_absolute_time();
#else
clock_t start = clock();
#endif
calc_primes_1379(b,e); // Change the arguments b and e to adjust the begin and end of rows [1,3,7,9]
// Get system time END
#ifdef __APPLE__
uint64_t end = mach_absolute_time();
mach_timebase_info_data_t timebase_info;
mach_timebase_info(&timebase_info);
long double diff = (end - start) * timebase_info.numer / timebase_info.denom; // nano sec
printf("Your calculations took %.3Lf seconds to run.\n", diff / 1e9 );
#else
clock_t end = clock();
printf("Your calculations took %.3Lf seconds to run.\n", (long double)(end - start) / CLOCKS_PER_SEC );
#endif
return 0;
}
PTO1379 aka PTOQR app related data.
Compute Pi - GitHub repository with apps to compute Pi.
Compute Primes - GitHub repository with apps to compute Prime numbers.
ALSO AVAILABLE OTHER SETS OF DATA RELATED TO COMPUTE PI OR PRIMES.
Interested in computing Pi value? Read this thread on MATHFORUMS.COM - Calculating the value of Pi. A new formula using Fibonacci numbers and the golden Phi number..
Interested in computing Primes? Read this thread on MATHFORUMS.COM - Arithmetic mean of subsets of prime numbers. A method that counts the convergence to the golden number Phi..