This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

41
Task/Pi/C++/pi.cpp Normal file
View file

@ -0,0 +1,41 @@
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (300000);
mpf_t x0, y0, resA, resB, Z, var;
mpf_init_set_ui (x0, 1);
mpf_init_set_d (y0, 0.5);
mpf_sqrt (y0, y0);
mpf_init (resA);
mpf_init (resB);
mpf_init_set_d (Z, 0.25);
mpf_init (var);
int n = 1;
for(int i=0; i<8; i++){
agm(x0, y0, resA, resB);
mpf_sub(var, resA, x0);
mpf_mul(var, var, var);
mpf_mul_ui(var, var, n);
mpf_sub(Z, Z, var);
n += n;
agm(resA, resB, x0, y0);
mpf_sub(var, x0, resA);
mpf_mul(var, var, var);
mpf_mul_ui(var, var, n);
mpf_sub(Z, Z, var);
n += n;
}
mpf_mul(x0, x0, x0);
mpf_div(x0, x0, Z);
gmp_printf ("%.100000Ff\n", x0);
return 0;
}

38
Task/Pi/D/pi-1.d Normal file
View file

@ -0,0 +1,38 @@
import std.stdio, std.conv, std.string;
struct PiDigits {
immutable uint nDigits;
int opApply(int delegate(ref string /*chunk of pi digit*/) dg){
// Maximum width for correct output, for type ulong.
enum size_t width = 9;
enum ulong scale = 10UL ^^ width;
enum ulong initDigit = 2UL * 10UL ^^ (width - 1);
enum string formatString = "%0" ~ text(width) ~ "d";
immutable size_t len = 10 * nDigits / 3;
auto arr = new ulong[len];
arr[] = initDigit;
ulong carry;
foreach (i; 0 .. nDigits / width) {
ulong sum;
foreach_reverse (j; 0 .. len) {
auto quo = sum * (j + 1) + scale * arr[j];
arr[j] = quo % (j*2 + 1);
sum = quo / (j*2 + 1);
}
auto yield = format(formatString, carry + sum/scale);
if (dg(yield))
break;
carry = sum % scale;
}
return 0;
}
}
void main() {
foreach (d; PiDigits(100))
writeln(d);
}

33
Task/Pi/D/pi-2.d Normal file
View file

@ -0,0 +1,33 @@
import std.stdio, std.bigint;
void main() {
int ndigits = 0;
auto q = BigInt(1);
auto r = BigInt(0);
auto t = q;
auto k = q;
auto n = BigInt(3);
auto l = n;
bool first = true;
while (ndigits < 1_000) {
if (4 * q + r - t < n * t) {
write(n); ndigits++;
if (ndigits % 70 == 0) writeln();
if (first) { first = false; write('.'); }
auto nr = 10 * (r - n * t);
n = ((10 * (3 * q + r)) / t) - 10 * n;
q *= 10;
r = nr;
} else {
auto nr = (2 * q + r) * l;
auto nn = (q * (7 * k + 2) + r * l) / (t * l);
q *= k;
t *= l;
l += 2;
k++;
n = nn;
r = nr;
}
}
}