Skip to content

Instantly share code, notes, and snippets.

@gagern
Last active October 7, 2017 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagern/e904b8f5c28cf2eb83d157f21a3a6eea to your computer and use it in GitHub Desktop.
Save gagern/e904b8f5c28cf2eb83d157f21a3a6eea to your computer and use it in GitHub Desktop.
Numeric experiment on averge distance between points in Sierpinski triangle
// Numeric experiment for https://math.stackexchange.com/q/2434673/35416
#include <cmath>
#include <random>
#include <iostream>
#include <iomanip>
// Barycentric coordinates
struct vec {
double c[3];
double norm() {
return std::sqrt((c[0]*c[0]+c[1]*c[1]+c[2]*c[2])*0.5);
}
vec operator-(const vec& v) const {
return {c[0]-v.c[0], c[1]-v.c[1], c[2]-v.c[2]};
}
void step(unsigned direction) {
c[0] *= 0.5;
c[1] *= 0.5;
c[2] *= 0.5;
c[direction] += 0.5;
}
};
int main() {
vec p{1/3., 1/3., 1/3.}, q{1/3., 1/3., 1/3.};
std::default_random_engine re{42};
std::uniform_int_distribution<unsigned> rd{0, 2};
for (unsigned i = 0; i < 1000; ++i) {
p.step(rd(re));
q.step(rd(re));
}
constexpr unsigned n = 1024;
double a[n] = {};
unsigned best = 0;
while (true) {
p.step(rd(re));
q.step(rd(re));
double d = (p-q).norm();
unsigned i;
for (i = 0; i < n && a[i]; ++i) {
d = (d + a[i]) * 0.5;
a[i] = 0;
}
if (i > best) {
best = i;
std::cout << std::setprecision(20) << d
<< " (2^" << i << ")"
<< std::endl;
}
if (i == n)
return 0;
a[i] = d;
}
}
0.27874298799388647074 (2^1)
0.29863202053101534084 (2^2)
0.26942331191508167576 (2^3)
0.30743663539814958252 (2^4)
0.4150281403504998412 (2^5)
0.3990468336834953611 (2^6)
0.41543420756388382831 (2^7)
0.40666831829231159245 (2^8)
0.40765916853199124503 (2^9)
0.41471113115986213415 (2^10)
0.41398097431637187471 (2^11)
0.41763425331837955579 (2^12)
0.42227062942489013153 (2^13)
0.42355815642533845011 (2^14)
0.42356762435416328572 (2^15)
0.42243238021504458946 (2^16)
0.42349961977696420901 (2^17)
0.42330028212455939052 (2^18)
0.42321647219919833471 (2^19)
0.4229136036583323599 (2^20)
0.42267934893346825742 (2^21)
0.42274431965991604576 (2^22)
0.42275384588027709043 (2^23)
0.42271527906342648562 (2^24)
0.42267879502409710923 (2^25)
0.42265859948474032715 (2^26)
0.42267895557936996376 (2^27)
0.42268198775969928471 (2^28)
0.4226762563841552911 (2^29)
0.4226765933802798525 (2^30)
0.42267910113364504099 (2^31)
0.42267910104283756834 (2^32)
0.42267910131311675626 (2^33)
0.42267910132256103495 (2^34)
0.42267910136691044798 (2^35)
0.42267910139755948684 (2^36)
0.42267910141297981852 (2^37)
0.42267910141086351139 (2^38)
0.42267910142121578598 (2^39)
0.4226791014128896129 (2^40)
0.42267910143024967073 (2^41)
0.42267910143016740321 (2^42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment