Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Farey-sequence/D/farey-sequence-1.d
Normal file
9
Task/Farey-sequence/D/farey-sequence-1.d
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
string toString() const /*pure nothrow*/ {
|
||||
if (den != 0)
|
||||
//return num.text ~ (den == 1 ? "" : "/" ~ den.text);
|
||||
return num.text ~ "/" ~ den.text;
|
||||
if (num == 0)
|
||||
return "NaRat";
|
||||
else
|
||||
return ((num < 0) ? "-" : "+") ~ "infRat";
|
||||
}
|
||||
15
Task/Farey-sequence/D/farey-sequence-2.d
Normal file
15
Task/Farey-sequence/D/farey-sequence-2.d
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import std.stdio, std.algorithm, std.range, arithmetic_rational2;
|
||||
|
||||
auto farey(in int n) pure nothrow @safe {
|
||||
return rational(0, 1).only.chain(
|
||||
iota(1, n + 1)
|
||||
.map!(k => iota(1, k + 1).map!(m => rational(m, k)))
|
||||
.join.sort().uniq);
|
||||
}
|
||||
|
||||
void main() @safe {
|
||||
writefln("Farey sequence for order 1 through 11:\n%(%s\n%)",
|
||||
iota(1, 12).map!farey);
|
||||
writeln("\nFarey sequence fractions, 100 to 1000 by hundreds:\n",
|
||||
iota(100, 1_001, 100).map!(i => i.farey.walkLength));
|
||||
}
|
||||
53
Task/Farey-sequence/D/farey-sequence-3.d
Normal file
53
Task/Farey-sequence/D/farey-sequence-3.d
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import core.stdc.stdio: printf, putchar;
|
||||
|
||||
void farey(in uint n) nothrow @nogc {
|
||||
static struct Frac { uint d, n; }
|
||||
|
||||
Frac f1 = { 0, 1 }, f2 = { 1, n };
|
||||
|
||||
printf("%u/%u %u/%u", 0, 1, 1, n);
|
||||
while (f2.n > 1) {
|
||||
immutable k = (n + f1.n) / f2.n;
|
||||
immutable aux = f1;
|
||||
f1 = f2;
|
||||
f2 = Frac(f2.d * k - aux.d, f2.n * k - aux.n);
|
||||
printf(" %u/%u", f2.d, f2.n);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
ulong fareyLength(in uint n, ref ulong[] cache) pure nothrow @safe {
|
||||
if (n >= cache.length) {
|
||||
auto newLen = cache.length;
|
||||
if (newLen == 0)
|
||||
newLen = 16;
|
||||
while (newLen <= n)
|
||||
newLen *= 2;
|
||||
cache.length = newLen;
|
||||
} else if (cache[n])
|
||||
return cache[n];
|
||||
|
||||
ulong len = ulong(n) * (n + 3) / 2;
|
||||
for (uint p = 2, q = 0; p <= n; p = q) {
|
||||
q = n / (n / p) + 1;
|
||||
len -= fareyLength(n / p, cache) * (q - p);
|
||||
}
|
||||
|
||||
cache[n] = len;
|
||||
return len;
|
||||
}
|
||||
|
||||
void main() nothrow {
|
||||
foreach (immutable uint n; 1 .. 12) {
|
||||
printf("%u: ", n);
|
||||
n.farey;
|
||||
}
|
||||
|
||||
ulong[] cache;
|
||||
for (uint n = 100; n <= 1_000; n += 100)
|
||||
printf("%u: %llu items\n", n, fareyLength(n, cache));
|
||||
|
||||
immutable uint n = 10_000_000;
|
||||
printf("\n%u: %llu items\n", n, fareyLength(n, cache));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue