Benchmark Julia on Raspberry Pi Zero W
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
#include <stdio.h> | |
#include <math.h> | |
int gcd(int a, int b) | |
{ | |
while (b != 0) | |
{ | |
int r = a % b; | |
a = b; | |
b = r; | |
} | |
return a; | |
} | |
int main() | |
{ | |
int N = 10000; | |
int s = 0; | |
for (int a = 1; a <= N; ++a) | |
{ | |
for (int b = 1; b <= N; ++b) | |
if (gcd(a,b)==1) | |
{ | |
s += 1; | |
} | |
} | |
printf("s=%d\n", s); | |
printf("N=%d\n", N); | |
double cN = (double)(N); | |
double approx_pi = sqrt(6*cN*cN /(double)(s)); | |
printf("%lf\n", approx_pi); | |
} |
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
/* | |
How to install gcc-8.1.0 on your RaspberryPi series | |
https://solarianprogrammer.com/2017/12/08/raspberry-pi-raspbian-install-gcc-compile-cpp-17-programs/ | |
``` | |
$ g++ -std=c++17 -O2 coprime.cpp -o cppexe | |
$ ./cppexe | |
``` | |
*/ | |
#include <iostream> | |
#include <numeric> | |
#include <cmath> | |
int main() | |
{ | |
int N = 10000; | |
int s = 0; | |
for (int a = 1; a <= N; ++a) | |
{ | |
for (int b = 1; b <= N; ++b) | |
//using std::gcd defined in numeric | |
//https://cpprefjp.github.io/reference/numeric/gcd.html | |
if (std::gcd(a,b)==1) | |
{ | |
s += 1; | |
} | |
} | |
printf("s=%d\n", s); | |
printf("N=%d\n", N); | |
double cN = static_cast<double>(N); | |
double approx_pi = std::sqrt(6*cN*cN /static_cast<double>(s)); | |
printf("%lf\n", approx_pi); | |
} |
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
using Printf | |
function calc_pi_by_gcd(N) | |
s = 0 | |
for a in 1:N | |
for b in 1:N | |
s += ifelse(gcd(a,b) == 1, 1, 0) | |
end | |
s | |
end | |
@printf("s=%d\n", s); | |
@printf("N=%d\n", N); | |
sqrt(6N^2/s) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elapsed time
Julia
Run from terminal.
Initialize Julia REPL and then run
calc_pi_by_gcd
C
C++17
By following the procedure
We can get gcc and g++ of version 8.1.0