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.
Graphics mode app - use Allegro 4.2 by Shawn Hargreaves
C source code (old not best but just cleaned)
/** PRIME NUMBERS ON FIBONACCI SPIRAL by MARTE.BET - Sylwester Bogusiak. 2018 AD v 0.9 beta **/
// Each subset of Primes resonate with Golden Number Phi= 1.618033... The Arithmetic Mean aka Average divided by upper Fibonacci from border = F(n) is near to, but below Phi / 2... = 0.89... and closer with bigger values of Primes
// More info in my publication available on Researchgate.net: Arithmetic mean of subsets of prime numbers. A method that counts the convergence to the golden number Phi.
// Link: https://www.researchgate.net/publication/385384830_Arithmetic_mean_of_subsets_of_prime_numbers_A_method_that_counts_the_convergence_to_the_golden_number_Phi
// See also this thread on MATHFORUMS.COM: https://mathforums.com/t/arithmetic-mean-of-subsets-of-prime-numbers-a-method-that-counts-the-convergence-to-the-golden-number-phi.370710/
// SHOW AND CALCULATE PRIMES ON FIBONACCI SPIRAL
// POFSpiral algorithm - Primes On Fibonacci Spiral
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate Fibonacci numbers and Prime Numbers and distribute it in between Fibonacci on Fibonacci Spiral
// There is no warranty or guarantee of any kind.
// The Allegro library has further restrictions.
// Tested on version Allegro 4.4
// To Compile:
// gcc -o pofspiral pofspiral.c -lm -lalleg
// Usage in command line:
// ./pofspiral
#include
#include
#include
#define BLACK makecol(0,0,0)
#define CLEAR makecol(255,0,255)
#define GREY makecol(127,127,127)
#define WHITE makecol(255,255,255)
#define RED makecol(255,0,0)
#define GREEN makecol(0,255,0)
#define BLUE makecol(0,0,255)
const int Screen_w = 1920; // size of the screen
const int Screen_h = 1080;
int scroll_x=0;
int scroll_y=0;
double scale=1.0;
int keypres=0;
enum keypres {Key_0,Key_1,Key_2,Key_6,Key_U};
BITMAP * back_screen=NULL;
static BITMAP * mouse_cursor = NULL;
static const double PI = M_PI; // PI= 3.1415926535;
double angle;
double x=0.0,y=0.0,r=0.0;
int i,j;
int natural_number;
#define MAX_NUMBERS 12000
#define MAX_PRIMES MAX_NUMBERS / 10
int prime_position=0;
int prime_table[MAX_NUMBERS][MAX_PRIMES];
int sum_primes[MAX_NUMBERS]; /// OK
int result=0;
#define CURSORSIZE 15 /// pixels
int numeration=10; // decimal
#define PHI 1.618033988749894848204586834
#define EULER 2.7182818284590452353602874713527
#define ANGLE 180
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// IS THIS FIBONACCI NUMBER
// A utility function that returns true if x is perfect square
int isPerfectSquare(int x)
{
int s;
s = sqrt(x);
if (s*s == x) {return 0;}
else
return 1;
}
int is_this_fibonacci_number(int natural_number)
{
int n, i, flag1 = -1, flag2 = -1;
n=natural_number;
flag1 = isPerfectSquare(5*n*n + 4);
flag2 = isPerfectSquare(5*n*n - 4);
if ((flag1==0) || (flag2==0))
{ return 0; // 0 - is fibonacci
}
else
return 1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// IS THIS PRIME NUMBER
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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// CENTER PICTURE
int center_picture()
{
scroll_x=Screen_w/2;
scroll_y=Screen_h/2;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SAVE PRIME TO LIST
void save_prime_to_list(int prime_number, int index_prime, int index_fibo)
{
prime_table[index_fibo][index_prime]=prime_number;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// FIND PRIMES BETWEEN FIBO
int find_primes_between_fibo(int fibo_previous, int fibo_present, int index_fibo)
{
int j;
int index_primes=0;
index_primes=0;
int natural_number;
sum_primes[index_fibo]=0;
for(j = fibo_previous+1; j <= fibo_present; j++)
{
natural_number=j;
if (((is_this_prime_number(natural_number)==0) && natural_number>1))
{
index_primes++;
sum_primes[index_fibo]+=natural_number; /// sum of primes
save_prime_to_list(natural_number,index_primes,index_fibo); /// save from 1
}
}
return index_primes; /// max_primes
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// CHECK MOUSE POSITION AND HOVER
int check_mouse_position_and_hover(int xx,int yy, int scroll_x, int scroll_y,int cursor_size)
{
int ix,iy;
int mx,my;
mx=scroll_x+mouse_x;
my=scroll_y+mouse_y;
for (ix=0;ixxx-(2*ix)+scroll_x && my < yy+iy+scroll_y && my > yy-(2*iy)+scroll_y ) return 1;
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// DRAW FIBONACCI SPIRAL AND DISTRIBUTE PRIMES
int draw_primes_on_fibonacci_spiral()
{
int direction=0;
int direction_p=0;
int index_primes=0;
int max_primes[MAX_NUMBERS];
double cx1[MAX_NUMBERS],cy1[MAX_NUMBERS];
natural_number=-1;
int queue=0;
int previous_number=0;
double x1[MAX_NUMBERS],x2[MAX_NUMBERS],y1[MAX_NUMBERS],y2[MAX_NUMBERS];
double px1,py1; //prime posxy
int position=0;
int index_fibo;
index_fibo=-1;
index_primes=0;
prime_position=1; /// for table
position=0;
px1=py1=0.0;
for(j = 0; j <= MAX_NUMBERS; j++)
{
x1[j]=0.0;
x2[j]=0.0;
y1[j]=0.0;
y2[j]=0.0;
//////////////////////////////////////////
natural_number++;
if ((is_this_fibonacci_number(natural_number) == 0) && (natural_number >=2))
{
position=0; /// needed
index_fibo++;
double temp_natural,temp_prev;
temp_natural=natural_number;
temp_prev=previous_number;
temp_natural*=scale;
temp_prev*=scale;
switch(direction)
{
case 0: /// up just once
break;
case 1: /// bottom
x1[index_fibo]=x1[index_fibo-1];
y1[index_fibo]=y1[index_fibo-1]+temp_prev;
x2[index_fibo]=x1[index_fibo]+temp_natural;
y2[index_fibo]=y1[index_fibo]+temp_natural;
break;
case 2: /// right
x1[index_fibo]=x1[index_fibo-1]+temp_prev;
y1[index_fibo]=y1[index_fibo-1]-(temp_natural-temp_prev);
x2[index_fibo]=x1[index_fibo]+temp_natural;
y2[index_fibo]=y1[index_fibo]+temp_natural;
break;
case 3: /// up
x1[index_fibo]=x1[index_fibo-1]-(temp_natural-temp_prev);
y1[index_fibo]=y1[index_fibo-1]-temp_natural;
x2[index_fibo]=x1[index_fibo]+temp_natural;
y2[index_fibo]=y1[index_fibo]+temp_natural;
break;
case 4: /// left
x1[index_fibo]=x1[index_fibo-1]-temp_natural;
y1[index_fibo]=y1[index_fibo-1];
x2[index_fibo]=x1[index_fibo]+temp_natural;
y2[index_fibo]=y1[index_fibo]+temp_natural;
break;
}
int ai,bi,max_natural;
int fix_x=0,fix_y=0;
int old=0; /// temp
if (natural_number>=2)
{
double avg, phi_f1, phi_f2, sum_f;
max_natural=natural_number-previous_number; /// actual difference between Fibonacci
max_primes[index_fibo]=find_primes_between_fibo(previous_number, natural_number, index_fibo); // actual max primes (quantiny in the subset between Fibonacci)
avg = sum_primes[index_fibo] / max_primes[index_fibo];
sum_f = sum_primes[index_fibo] / natural_number;
phi_f2 = avg / natural_number;
phi_f1 = avg / previous_number;
if (check_mouse_position_and_hover(scroll_x+x+x1[index_fibo],scroll_y+y+y1[index_fibo],scroll_x+x+x1[index_fibo]+temp_natural,scroll_y+y+y1[index_fibo]+temp_natural,CURSORSIZE))
{
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+temp_natural/2,scroll_y+y+y1[index_fibo]+temp_natural/2, RED, -1, "QTY = %d", max_primes[index_fibo]); ///show qty of primes
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+temp_natural/2,scroll_y+y+y1[index_fibo]+temp_natural/2+10, RED, -1, "SUM = %d", sum_primes[index_fibo]); ///show sum primes
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+temp_natural/2,scroll_y+y+y1[index_fibo]+temp_natural/2+20, BLACK, -1, "AVG = %10.2f", avg);
///show average aka arithmetic mean
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+temp_natural/2,scroll_y+y+y1[index_fibo]+temp_natural/2+30, BLACK, -1, "SUM/F(n) = %.2f", sum_f); ///show SUM / F(n)
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+temp_natural/2,scroll_y+y+y1[index_fibo]+temp_natural/2+40, BLACK, -1, "AVG/F(n) = %.9f", phi_f2); ///show that AVG / F(n) close to Phi / 2
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+temp_natural/2,scroll_y+y+y1[index_fibo]+temp_natural/2+50, BLACK, -1, "AVG/F(n-1) = %.9f", phi_f1); ///show that AVG / F(n-1) close to phi / 2
}
if (direction==1) /// bottom
{
rect(back_screen,scroll_x+x+x1[index_fibo],scroll_y+y+y1[index_fibo],scroll_x+x+x2[index_fibo]-fix_x,scroll_y+y+y2[index_fibo]-fix_y, WHITE);
/* Draw a white arc */
arc(back_screen, scroll_x+x+x1[index_fibo]+temp_natural, scroll_y+y+y1[index_fibo], itofix(128), itofix(192), temp_natural, WHITE);
bi=0;
for (ai=previous_number+1;ai<=natural_number;ai++)
{
bi++;
angle= (double) 90/max_natural*bi-(90*(direction))-90;
r= (double) temp_natural;
cx1[index_fibo] = r * cos(angle * PI / 180);
cy1[index_fibo] = -r * sin(angle * PI / 180);
if (((is_this_prime_number(ai)==0) && ai>1))
{
circlefill(back_screen,scroll_x+x+x1[index_fibo]+cx1[index_fibo]+temp_natural,scroll_y+y+y1[index_fibo]+cy1[index_fibo],2, WHITE);
textprintf_centre_ex(back_screen, font,scroll_x+x+x1[index_fibo]+cx1[index_fibo]+temp_natural-10,scroll_y+y+y1[index_fibo]+cy1[index_fibo]+10, WHITE, -1, "%d", ai); ///show sum primes
}
}
}
if (direction==2) /// right
{
rect(back_screen,scroll_x+x+x1[index_fibo],y+scroll_y+y1[index_fibo],scroll_x+x+x2[index_fibo]-fix_x,scroll_y+y+y2[index_fibo]-fix_y, BLACK);
arc(back_screen, scroll_x+x+x1[index_fibo], scroll_y+y+y1[index_fibo], itofix(192), itofix(256), temp_natural, BLACK);
bi=0;
for (ai=previous_number+1;ai<=natural_number;ai++)
{
bi++;
angle= (double) 90/max_natural*bi-(90*(direction))+90;
r= (double) temp_natural;
cx1[index_fibo] = r * cos(angle * PI / 180);
cy1[index_fibo] = -r * sin(angle * PI / 180);
if (((is_this_prime_number(ai)==0) && ai>1))
{
circlefill(back_screen,scroll_x+x+x1[index_fibo]+cx1[index_fibo],scroll_y+y+y1[index_fibo]+cy1[index_fibo],2, BLACK);
textprintf_centre_ex(back_screen, font,scroll_x+x+x1[index_fibo]+cx1[index_fibo]+10,scroll_y+y+y1[index_fibo]+cy1[index_fibo]+10, BLACK, -1, "%d", ai); ///show sum primes
if (ai==natural_number)
line(back_screen,scroll_x+x+3*scale,scroll_y+y+1*scale,scroll_x+x+x1[index_fibo]+cx1[index_fibo],scroll_y+y+y1[index_fibo]+cy1[index_fibo],BLACK);
}
}
}
if (direction==3) ///up
{
rect(back_screen,scroll_x+x+x1[index_fibo],y+scroll_y+y1[index_fibo],scroll_x+x+x2[index_fibo]-fix_x,scroll_y+y+y2[index_fibo]-fix_y, BLUE);
arc(back_screen, scroll_x+x+x1[index_fibo], scroll_y+y+y1[index_fibo]+temp_natural, itofix(0), itofix(64), temp_natural, BLUE);
bi=0;
for (ai=previous_number+1;ai<=natural_number;ai++)
{
bi++;
angle= (double) 90/max_natural*bi-(90*(direction))-90;
r= (double) temp_natural;
cx1[index_fibo] = r * cos(angle * PI / 180);
cy1[index_fibo] = -r * sin(angle * PI / 180);
if (((is_this_prime_number(ai)==0) && ai>1))
{
circlefill(back_screen,scroll_x+x+x1[index_fibo]+cx1[index_fibo],scroll_y+y+y1[index_fibo]+cy1[index_fibo]+temp_natural,2, BLUE);
textprintf_centre_ex(back_screen, font, scroll_x+x+x1[index_fibo]+cx1[index_fibo]+10,scroll_y+y+y1[index_fibo]+cy1[index_fibo]+temp_natural-10, BLUE, -1, "%d", ai); ///show sum primes
}
}
}
if (direction==4) ///left
{
rect(back_screen,scroll_x+x+x1[index_fibo],y+scroll_y+y1[index_fibo],scroll_x+x+x2[index_fibo]-fix_x,scroll_y+y+y2[index_fibo]-fix_x, GREEN);
arc(back_screen, scroll_x+x+x1[index_fibo]+temp_natural, scroll_y+y+y1[index_fibo]+temp_natural, itofix(64), itofix(128), temp_natural, GREEN);
bi=0;
for (ai=previous_number+1;ai<=natural_number;ai++)
{
bi++;
angle= (double) 90/max_natural*bi-(90*(direction))+90;
r= (double) temp_natural;
cx1[index_fibo] = r * cos(angle * PI / 180);
cy1[index_fibo] = -r * sin(angle * PI / 180);
if (((is_this_prime_number(ai)==0) && ai>1))
{
circlefill(back_screen,scroll_x+x+x1[index_fibo]+cx1[index_fibo]+temp_natural,scroll_y+y+y1[index_fibo]+cy1[index_fibo]+temp_natural,2, GREEN);
textprintf_centre_ex(back_screen, font,scroll_x+x+x1[index_fibo]+cx1[index_fibo]+temp_natural-10,scroll_y+y+y1[index_fibo]+cy1[index_fibo]+temp_natural-10, GREEN, -1, "%d", ai); ///show sum primes
if (ai==natural_number)
line(back_screen,scroll_x+x+fix_x+3*scale,scroll_y+y+fix_y+1*scale,scroll_x+x+x1[index_fibo]+cx1[index_fibo]+temp_natural,scroll_y+y+y1[index_fibo]+cy1[index_fibo]+temp_natural,GREEN);
}
}
}
if (numeration == 10) textprintf_centre_ex(back_screen, font, x+x1[index_fibo]+20+scroll_x, y+y1[index_fibo]+10+scroll_y, RED, -1, "%d", natural_number);
}
previous_number=natural_number;
//////////////////////////////////////
direction++;
if (direction>4) direction=1;
} /// fibo
}
/////////////////////////////////////////////// fibo
if(key[KEY_W])
{
if (scale>0.05) scale-=0.01;
}
if(key[KEY_S])
{
if (scale<5) scale+=0.01;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SHOW INFO
int show_info()
{
if(!(key[KEY_I]))
{
textout_ex(back_screen, font,"Press I for more info.", 830, 30, BLACK, -1);
};
if((key[KEY_I]))
{
textout_ex(back_screen, font,"Keyboard control:", 10, 0, BLACK, -1);
textout_ex(back_screen, font,"Arrows (Left,Right,Up,Down) - move diagram.", 10, 10, BLACK, -1);
textout_ex(back_screen, font,"C - center the picture.", 10, 20, BLACK, -1);
textout_ex(back_screen, font,"W and S - scale picture.", 10, 30, BLACK, -1);
textout_ex(back_screen, font,"ESC - exit the program.", 10, 50, BLACK, -1);
};
return 0;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SHOW WELCOME MESSAGE
int show_welcome_message()
{
while(!(key[KEY_SPACE]))
{
textout_ex(screen, font," Prime Numbers In 2D Geometry on Fibonacci spiral. ", 700, 240, makecol(0,0,0), -1);
textout_ex(screen, font," POFSPIRAL Version 0.9 (beta) ", 700, 260, makecol(0,0,0), -1);
textout_ex(screen, font,"This program is presenting prime numbers distributed in 2D space. ", 700, 280, makecol(0,0,0), -1);
textout_ex(screen, font," On next screen please use keyboard and mouse to manipulate. ", 700, 450, makecol(0,0,0), -1);
textout_ex(screen, font," Please press SPACE ", 700, 500, makecol(0,0,0), -1);
textout_ex(screen, font," Thank You. MARTE.BEST - Sylwester Bogusiak aka Sylvi91 AD 2018 ", 700, 520, makecol(0,0,0), -1);
textout_ex(screen, font," This program is using Allegro 4.2 library by Shawn Hargreaves ", 700, 560, makecol(0,0,0), -1);
};
return 0;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// KEYBOARD AND MOUSE
int keyboard_mouse_and_drawings()
{
if(key[KEY_LEFT]) scroll_x=scroll_x-10;
if(key[KEY_RIGHT]) scroll_x=scroll_x+10;
if(key[KEY_UP]) scroll_y=scroll_y-10;
if(key[KEY_DOWN]) scroll_y=scroll_y+10;
if(key[KEY_C]) //center spiral
{
center_picture();
}
draw_primes_on_fibonacci_spiral();
// draw mouse cursor
draw_sprite(back_screen,mouse_cursor,mouse_x,mouse_y);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// MAIN
int main(void)
{
keypres==Key_0; ///
/* you should always do this at the start of Allegro programs */
if (allegro_init() != 0)
return 1;
/* set up the keyboard handler */
install_timer(); //before install_mod
install_keyboard();
install_mouse();
/* set a graphics mode sized 320x200 */
set_color_depth(32);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, Screen_w, Screen_h, 0, 0) != 0) {
if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
return 1;
}
}
/* set the color palette */
set_palette(desktop_palette);
if (back_screen==NULL)
{
back_screen=create_bitmap(Screen_w,Screen_h);
clear_to_color(back_screen,CLEAR); //wyczysc i ustaw przezroczyste tlo
};
if ( mouse_cursor==NULL)
{
mouse_cursor=create_bitmap(25,25);
clear_to_color( mouse_cursor,CLEAR);
circle( mouse_cursor,12,12,12,BLACK);
circle( mouse_cursor,12,12,10,BLACK);
line( mouse_cursor,0,12,25,12,BLACK);
line( mouse_cursor,12,0,12,25,BLACK);
};
clear_to_color(screen,GREY);
show_welcome_message();
while(!(key[KEY_ESC]))
{
clear_to_color(back_screen,GREY);
keyboard_mouse_and_drawings();
show_info();
blit(back_screen,screen,0,0,0,0,Screen_w,Screen_h);
}
return 0;
};
END_OF_MAIN();
POFSPIRAL app related data on images.
Console mode app.
C source code
/** PRIME NUMBERS AND COMPOSITE NUMBERS SUBSETS BETWEEN FIBONACCI WORDS by MARTE.BEST - Sylwester Bogusiak. 2019 AD v 0.9 beta **/
// Check how each subset of Primes resonate with Golden Number Phi= 1.618033... The Arithmetic Mean aka Average divided by upper Fibonacci from border = F(n) is near to, but lesser than Phi / 2... = 0.89... and closer with bigger values of Primes
// But Composite Numbers Average divided by Fib(n+1) is greater than Phi /2
// More info in my publication available on Researchgate.net: Arithmetic mean of subsets of prime numbers. A method that counts the convergence to the golden number Phi.
// See also this thread on MATHFORUMS.COM: https://mathforums.com/t/arithmetic-mean-of-subsets-of-prime-numbers-a-method-that-counts-the-convergence-to-the-golden-number-phi.370710/
// PRIMES AND COMPOSITE SUBSETS
// PACS algorithm - Primes And Composite Subsets
// Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91
// This is free code to calculate Fibonacci numbers and Prime Numbers and Composite Numbers
// There is no warranty or guarantee of any kind.
// To Compile:
// gcc -o pacs pacs.c -lm
// Usage in command line:
// ./pacs 10 30
#include
#include
#include
#include
#include
#include
#define Phi 1.618033988749895
unsigned long long fib(int n) {
if ((n == 1) || (n == 2))
return 1;
else
return fib(n - 1) + fib(n - 2);
}
int main( int argc, const char* argv[] )
{
time_t start;
time_t end; // Get the system time
bool print = false;
int x;
int y;
unsigned long long i,j,k;
unsigned long long sum_p=0; /// sum of prime numbers
unsigned long long qty_p=0; /// qty of primes numbers in subset
unsigned long long sum_c=0; /// sum of composite numbers
unsigned long long qty_c=0; /// qty of composite numbers in subset
unsigned long long limit; /// limit of primes from fib
unsigned long long subset_start=0; ///
unsigned long long subset_end=1;
unsigned long long tempNumber=0;
double approx_p_phi;
double approx_c_phi;
double avg_p=0;
double avg_c=0;
qty_p=0;
qty_c=0;
if ( argc >= 3 )
{
x = atoi( argv[1] );
y = atoi( argv[2] );
if ( argc == 4 )
{
if (!(strcmp(argv[3],"print")))
{
print = true;
}
}
time(&start); // Get the system time
if ((y>x) && (x>=4))
{
printf("________________________________________________________________________\n");
printf("____________THAT APP IS CALCULATING THE PRIME NUMBERS SUBSETS___________\n");
printf("_______________________AND COMPOSITE NUMBERS SUBSETS____________________\n");
printf("____________BETWEEN FIBONACCI AND FINDING APPROXIMATION_________________\n");
printf("______________________TO THE GOLDEN NUMBER_PHI._________________________\n");
printf("___________Author: MARTE.BEST - Sylwester Bogusiak aka Sylvi91__________\n");
printf("________________________________________________________________________\n");
printf(" Please wait for prime numbers generator. Warning! For second argument f2 greater than 45 this may take couple minutes. Bigger f2 consume more and more time and memory.\n");
limit = fib(y); /// only once
// Allocate memory for prime array and initialize all
// elements as true
bool* prime = malloc((limit + 1) * sizeof(bool));
for (int i = 0; i <= limit; i++)
prime[i] = true;
// 0 and 1 are not prime numbers
prime[0] = prime[1] = false;
// For each number from 2 to sqrt(n)
for (int p = 2; p <= sqrt(limit); p++) {
// If p is prime
if (prime[p]) {
// Mark all multiples of p as non-prime
for (int i = p * p; i <= limit; i += p)
prime[i] = false;
}
}
if (print)
{
// Print all prime numbers up to n
printf("Prime numbers up to %lld:\n", limit);
for (int p = 2; p <= limit; p++) {
if (prime[p])
printf("%d ", p);
}
printf("\n");
}
subset_start=fib(x-1); /// For start
subset_end=fib(x); ///
for (i=x;i=4)
{
avg_p = sum_p/qty_p;
avg_c = sum_c/qty_c;
approx_p_phi = ((avg_p/subset_end) *2);
approx_c_phi = ((avg_c/subset_end) *2);
printf("\n");
printf(" Fib(%llu) = %llu - Fib(%llu) = %llu SUM P = %llu QTY P = %llu AVG P = %.2f Phi P = %f\n",i,subset_start,i+1, subset_end, sum_p, qty_p, avg_p, approx_p_phi);
printf(" Fib(%llu) = %llu - Fib(%llu) = %llu SUM C = %llu QTY C = %llu AVG C = %.2f Phi C = %f\n",i,subset_start,i+1, subset_end, sum_c, qty_c, avg_c, approx_c_phi);
printf("\n");
if (approx_p_phi > Phi)
{
printf(" Phi P = %f > Phi\n", approx_p_phi);
}
else
if (approx_p_phi < Phi)
{
printf(" Phi P = %f < Phi\n", approx_p_phi);
}
else
if (approx_p_phi == Phi)
{
printf(" Phi P = %f = Phi\n", approx_p_phi);
}
if (approx_c_phi > Phi)
{
printf(" Phi C = %f > Phi\n", approx_c_phi);
}
else
if (approx_c_phi < Phi)
{
printf(" Phi C = %f < Phi\n", approx_c_phi);
}
else
if (approx_c_phi == Phi)
{
printf(" Phi C = %f = Phi\n", approx_c_phi);
}
printf("\n");
}
}
// Free allocated memory
free(prime);
time(&end); // End time
double dif;
dif = difftime (end,start); // time difference
printf ("Your calculations took %.2lf seconds to run.\n", dif ); //
return 0;
}
} // if
printf(" Simple usage: pacs f1 f2 \n where f1 and f2 are natural numbers and Fibonacci words in sequence Fib(n) Fib(n+1) and f1 >= 4 and f2 > f1\n Warning! f2 should not exceed 50 for 8 GB RAM otherwise you will reach memory limit.\n");
printf(" Additionally to print all natural numbers use: pacs f1 f2 print\n");
return 0;
}
Free publication to download
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..