Last active
December 20, 2015 12:59
-
-
Save lovasoa/6135335 to your computer and use it in GitHub Desktop.
sqrt(2) ≃ mean(2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 1 2 1 1 2 1 2 1 1 2 …
This file contains hidden or 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> | |
//See http://oeis.org/A001030 | |
unsigned int maxdepth, nbrtermes; | |
unsigned int recur(char num, unsigned int depth) { | |
if (depth>=maxdepth) { | |
nbrtermes++; | |
return (unsigned int)num; | |
} | |
depth++; | |
if (num==2){ | |
return recur(2, depth) + recur(1, depth) + recur(1, depth); | |
} else {//num==1 | |
return recur(2, depth) + recur(1, depth); | |
} | |
} | |
int main(int argc, char *argv[]){ | |
nbrtermes=0; | |
printf("Profondeur de récursion?"); | |
scanf("%d", &maxdepth); | |
printf("sqrt(2) ~ %d/%d\n", recur(2,1), nbrtermes); | |
} |
This file contains hidden or 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
#!/usr/bin/env python3 | |
def seq(n, repl): | |
a=[2] | |
while len(a)<n: a=[l for i in a for l in (repl[0] if i==1 else repl[1])] | |
return a[:n] | |
def mean(iterbl): | |
ssum=0 | |
for i in iterbl: ssum+=i | |
return ssum/len(iterbl) | |
seqlen = int(1e6) #the larger seqlen, the better the approximation, the slower this program | |
print(""" | |
φ ≃ %f | |
√2 ≃ %f | |
""" % (mean(seq(seqlen, ((2,), (2,1)))), mean(seq(seqlen, ((2,1), (2,1,1)))))) |
This file contains hidden or 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> | |
/* Ophir LOJKINE | |
Compute a sequence which mean converges to sqrt(2) | |
The computation is done using only integer arithmetics | |
*/ | |
int abs (int x) { | |
return (x>0)?x:-x; | |
} | |
int main (void){ | |
long int sum=0, iter=0; | |
long int step=0; | |
long int invepsilon=1e5; //The precision of the approximation will be at least 1/invepsilon | |
//Thanks to Ulysse LOJKINE for the formula: | |
//5*abs(sum^2-2*iter^2)*1/epsilon-14*iter^2 > 0 ==> | sqrt(2) - sum/iter | < epsilon | |
while (5*abs(sum*sum-2*iter*iter)*invepsilon-14*iter*iter>=0){ | |
if(sum*sum > 2 * iter*iter) step=1; | |
else step=2; | |
printf("%ld ", step); | |
sum+=step; | |
iter++; | |
} | |
printf("\nsqrt(2) ~ %ld/%ld\n", sum, iter); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment