Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Parallel-calculations/D/parallel-calculations-1.d
Normal file
27
Task/Parallel-calculations/D/parallel-calculations-1.d
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
ulong[] decompose(ulong n) pure nothrow {
|
||||
typeof(return) result;
|
||||
for (ulong i = 2; n >= i * i; i++)
|
||||
for (; n % i == 0; n /= i)
|
||||
result ~= i;
|
||||
if (n != 1)
|
||||
result ~= n;
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.algorithm, std.parallelism, std.typecons;
|
||||
|
||||
immutable ulong[] data = [
|
||||
2UL^^59-1, 2UL^^59-1, 2UL^^59-1, 112_272_537_195_293UL,
|
||||
115_284_584_522_153, 115_280_098_190_773,
|
||||
115_797_840_077_099, 112_582_718_962_171,
|
||||
112_272_537_095_293, 1_099_726_829_285_419];
|
||||
|
||||
//auto factors = taskPool.amap!(n => tuple(decompose(n), n))(data);
|
||||
//static enum genPair = (ulong n) pure => tuple(decompose(n), n);
|
||||
static genPair(ulong n) pure { return tuple(decompose(n), n); }
|
||||
auto factors = taskPool.amap!genPair(data);
|
||||
|
||||
auto pairs = factors.map!(p => tuple(p[0].reduce!min, p[1]));
|
||||
writeln("N. with largest min factor: ", pairs.reduce!max[1]);
|
||||
}
|
||||
85
Task/Parallel-calculations/D/parallel-calculations-2.d
Normal file
85
Task/Parallel-calculations/D/parallel-calculations-2.d
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import std.stdio, std.math, std.algorithm, std.typecons,
|
||||
core.thread, core.stdc.time;
|
||||
|
||||
final class MinFactor: Thread {
|
||||
private immutable ulong num;
|
||||
private ulong[] fac;
|
||||
private ulong minFac;
|
||||
|
||||
this(in ulong n) /*pure nothrow*/ {
|
||||
super(&run);
|
||||
num = n;
|
||||
fac = new ulong[0];
|
||||
}
|
||||
|
||||
@property ulong number() const pure nothrow {
|
||||
return num;
|
||||
}
|
||||
|
||||
@property const(ulong[]) factors() const pure nothrow {
|
||||
return fac;
|
||||
}
|
||||
|
||||
@property ulong minFactor() const pure nothrow {
|
||||
return minFac;
|
||||
}
|
||||
|
||||
private void run() {
|
||||
immutable clock_t begin = clock;
|
||||
switch (num) {
|
||||
case 0: fac = [];
|
||||
break;
|
||||
|
||||
case 1: fac = [1];
|
||||
break;
|
||||
|
||||
default:
|
||||
uint limit = cast(uint)(1 + double(num).sqrt);
|
||||
ulong n = num;
|
||||
for (ulong divi = 3; divi < limit; divi += 2) {
|
||||
if (n == 1)
|
||||
break;
|
||||
if ((n % divi) == 0) {
|
||||
while ((n > 1) && ((n % divi) == 0)) {
|
||||
fac ~= divi;
|
||||
n /= divi;
|
||||
}
|
||||
limit = cast(uint)(1 + double(n).sqrt);
|
||||
}
|
||||
}
|
||||
if (n > 1)
|
||||
fac ~= n;
|
||||
}
|
||||
minFac = fac.reduce!min;
|
||||
immutable clock_t end = clock;
|
||||
writefln("num: %20d --> min. factor: %20d ticks(%7d -> %7d)",
|
||||
num, minFac, begin, end);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable ulong[] numbers = [
|
||||
2UL^^59-1, 2UL^^59-1, 2UL^^59-1, 112_272_537_195_293UL,
|
||||
115_284_584_522_153, 115_280_098_190_773,
|
||||
115_797_840_077_099, 112_582_718_962_171,
|
||||
112_272_537_095_293, 1_099_726_829_285_419];
|
||||
|
||||
auto tGroup = new ThreadGroup;
|
||||
foreach (const n; numbers)
|
||||
tGroup.add(new MinFactor(n));
|
||||
|
||||
writeln("Minimum factors for respective numbers are:");
|
||||
foreach (t; tGroup)
|
||||
t.start;
|
||||
tGroup.joinAll;
|
||||
|
||||
auto maxMin = tuple(0UL, [0UL], 0UL);
|
||||
foreach (thread; tGroup) {
|
||||
auto s = cast(MinFactor)thread;
|
||||
if (s !is null && maxMin[2] < s.minFactor)
|
||||
maxMin = tuple(s.number, s.factors.dup, s.minFactor);
|
||||
}
|
||||
|
||||
writefln("Number with largest min. factor is %16d," ~
|
||||
" with factors:\n\t%s", maxMin.tupleof);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue