Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
29
Task/Stern-Brocot-sequence/D/stern-brocot-sequence-1.d
Normal file
29
Task/Stern-Brocot-sequence/D/stern-brocot-sequence-1.d
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import std.stdio, std.numeric, std.range, std.algorithm;
|
||||
|
||||
/// Generates members of the stern-brocot series, in order,
|
||||
/// returning them when the predicate becomes false.
|
||||
uint[] sternBrocot(bool delegate(in uint[]) pure nothrow @safe @nogc pred=seq => seq.length < 20)
|
||||
pure nothrow @safe {
|
||||
typeof(return) sb = [1, 1];
|
||||
size_t i = 0;
|
||||
while (pred(sb)) {
|
||||
sb ~= [sb[i .. i + 2].sum, sb[i + 1]];
|
||||
i++;
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum nFirst = 15;
|
||||
writefln("The first %d values:\n%s\n", nFirst,
|
||||
sternBrocot(seq => seq.length < nFirst).take(nFirst));
|
||||
|
||||
foreach (immutable nOccur; iota(1, 10 + 1).chain(100.only))
|
||||
writefln("1-based index of the first occurrence of %3d in the series: %d",
|
||||
nOccur, sternBrocot(seq => nOccur != seq[$ - 2]).length - 1);
|
||||
|
||||
enum nGcd = 1_000;
|
||||
auto s = sternBrocot(seq => seq.length < nGcd).take(nGcd);
|
||||
assert(zip(s, s.dropOne).all!(ss => ss[].gcd == 1),
|
||||
"A fraction from adjacent terms is reducible.");
|
||||
}
|
||||
18
Task/Stern-Brocot-sequence/D/stern-brocot-sequence-2.d
Normal file
18
Task/Stern-Brocot-sequence/D/stern-brocot-sequence-2.d
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import std.stdio, std.algorithm, std.range, std.numeric, queue_usage2;
|
||||
|
||||
struct SternBrocot {
|
||||
private auto sb = GrowableCircularQueue!uint(1, 1);
|
||||
enum empty = false;
|
||||
@property uint front() pure nothrow @safe @nogc {
|
||||
return sb.front;
|
||||
}
|
||||
uint popFront() pure nothrow @safe {
|
||||
sb.push(sb.front + sb[1]);
|
||||
sb.push(sb[1]);
|
||||
return sb.pop;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
SternBrocot().drop(50_000_000).front.writeln;
|
||||
}
|
||||
28
Task/Stern-Brocot-sequence/D/stern-brocot-sequence-3.d
Normal file
28
Task/Stern-Brocot-sequence/D/stern-brocot-sequence-3.d
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
void main() {
|
||||
import std.stdio, std.numeric, std.range, std.algorithm, std.bigint, std.conv;
|
||||
|
||||
/// Stern-Brocot sequence, 0-th member is 0.
|
||||
T sternBrocot(T)(T n) pure nothrow /*safe*/ {
|
||||
T a = 1, b = 0;
|
||||
while (n) {
|
||||
if (n & 1) b += a;
|
||||
else a += b;
|
||||
n >>= 1;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
alias sb = sternBrocot!uint;
|
||||
|
||||
enum nFirst = 15;
|
||||
writefln("The first %d values:\n%s\n", nFirst, iota(1, nFirst + 1).map!sb);
|
||||
|
||||
foreach (immutable nOccur; iota(1, 10 + 1).chain(100.only))
|
||||
writefln("1-based index of the first occurrence of %3d in the series: %d",
|
||||
nOccur, sequence!q{n}.until!(n => sb(n) == nOccur).walkLength);
|
||||
|
||||
auto s = iota(1, 1_001).map!sb;
|
||||
assert(s.zip(s.dropOne).all!(ss => ss[].gcd == 1),
|
||||
"A fraction from adjacent terms is reducible.");
|
||||
|
||||
sternBrocot(10.BigInt ^^ 20_000).text.length.writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue