This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,24 @@
# syntax: GAWK -f HAMMING_NUMBERS.AWK
BEGIN {
for (i=1; i<=20; i++) {
printf("%d ",hamming(i))
}
printf("\n1691: %d\n",hamming(1691))
exit(0)
}
function hamming(limit, h,i,j,k,n,x2,x3,x5) {
h[0] = 1
x2 = 2
x3 = 3
x5 = 5
for (n=1; n<=limit; n++) {
h[n] = min(x2,min(x3,x5))
if (h[n] == x2) { x2 = 2 * h[++i] }
if (h[n] == x3) { x3 = 3 * h[++j] }
if (h[n] == x5) { x5 = 5 * h[++k] }
}
return(h[limit-1])
}
function min(x,y) {
return((x < y) ? x : y)
}

View file

@ -1,4 +1,4 @@
import std.stdio, std.bigint, std.algorithm, std.range;
import std.stdio, std.bigint, std.algorithm, std.range, core.memory;
auto hamming(in int n) {
BigInt two = 2, three = 3, five = 5;
@ -17,6 +17,7 @@ auto hamming(in int n) {
}
void main() {
GC.disable;
iota(1, 21).map!hamming.writeln;
1_691.hamming.writeln;
1_000_000.hamming.writeln;

View file

@ -1,14 +1,15 @@
import std.stdio,std.bigint,std.container,std.algorithm,std.range;
import std.stdio, std.bigint, std.container, std.algorithm, std.range,
core.memory;
BigInt hamming(int n)
BigInt hamming(in int n)
in {
assert(n > 0);
} body {
auto frontier = redBlackTree(BigInt(2), BigInt(3), BigInt(5));
auto lowest = BigInt(1);
foreach (_; 1 .. n) {
lowest = frontier.front();
frontier.removeFront();
auto frontier = redBlackTree(2.BigInt, 3.BigInt, 5.BigInt);
auto lowest = 1.BigInt;
foreach (immutable _; 1 .. n) {
lowest = frontier.front;
frontier.removeFront;
frontier.insert(lowest * 2);
frontier.insert(lowest * 3);
frontier.insert(lowest * 5);
@ -17,7 +18,8 @@ in {
}
void main() {
writeln("First 20 Hamming numbers: ", map!hamming(iota(1, 21)));
writeln("hamming(1691) = ", hamming(1691));
writeln("hamming(1_000_000) = ", hamming(1_000_000));
GC.disable;
writeln("First 20 Hamming numbers: ", iota(1, 21).map!hamming);
writeln("hamming(1691) = ", 1691.hamming);
writeln("hamming(1_000_000) = ", 1_000_000.hamming);
}

View file

@ -1,8 +1,10 @@
import std.stdio: writefln;
import std.bigint: BigInt, toDecimalString;
import std.bigint: BigInt;
import std.conv: text;
import std.numeric: gcd;
import std.algorithm: copy, map;
import std.math; // log, ^^
import core.stdc.stdlib: calloc;
import std.math: log; // ^^
// Number of factors.
enum NK = 3;
@ -10,44 +12,45 @@ enum NK = 3;
enum MAX_HAM = 10_000_000;
static assert(gcd(NK, MAX_HAM) == 1);
enum int[NK] fac = [2, 3, 5];
enum int[NK] factors = [2, 3, 5];
/// k-smooth numbers (stored as their exponents of each factor).
/// K-smooth numbers (stored as their exponents of each factor).
struct Hamming {
double v; // log of the number, for convenience.
ushort[NK] e; // exponents of each factor.
double v; // Log of the number, for convenience.
ushort[NK] e; // Exponents of each factor.
// Compile-time constant, map!log(fac)
// log can't be used in CTFE yet
public static __gshared const double[fac.length] inc;
// log can't be used in CTFE yet.
//public static __gshared immutable double[factors.length] inc =
// factors[].map!log.array;
public static __gshared immutable double[factors.length] inc;
nothrow pure static this() {
//map!log(fac[]).copy(inc[]); // Not nothrow, not const.
foreach (i, f; fac)
inc[i] = log(f);
//factors[].map!log.copy(inc[]); // Not nothrow, not const.
foreach (immutable i, immutable f; factors)
inc[i] = f.log;
}
bool opEquals(in ref Hamming y) const pure nothrow {
//return this.e == y.e; // too much slow
foreach (size_t i; 0 .. this.e.length)
//return this.e == y.e; // Too much slow.
foreach (immutable i; 0 .. this.e.length)
if (this.e[i] != y.e[i])
return false;
return true;
}
void update() pure nothrow {
//this.v = dotProduct(inc, this.e); // too much slow
//this.v = dotProduct(inc, this.e); // Too much slow.
this.v = 0.0;
foreach (size_t i; 0 .. this.e.length)
foreach (immutable i; 0 .. this.e.length)
this.v += inc[i] * this.e[i];
}
string toString() const {
BigInt result = 1;
foreach (size_t i, f; fac)
result *= BigInt(f) ^^ this.e[i];
return toDecimalString(result);
foreach (immutable i, immutable f; factors)
result *= f.BigInt ^^ this.e[i];
return result.text;
}
}
@ -55,12 +58,16 @@ struct Hamming {
__gshared Hamming[] hams;
__gshared Hamming[NK] values;
nothrow static this() {
// Slower than malloc if you don't use all the MAX_HAM items.
hams = new Hamming[MAX_HAM];
// Slower than calloc if you don't use all the MAX_HAM items.
//hams = new Hamming[MAX_HAM];
foreach (i, ref v; values) {
auto ptr = cast(Hamming*)calloc(MAX_HAM, Hamming.sizeof);
if (!ptr)
throw new Error("Not enough memory.");
hams = ptr[0 .. MAX_HAM];
foreach (immutable i, ref v; values) {
v.e[i] = 1;
v.v = Hamming.inc[i];
}
@ -80,20 +87,20 @@ in {
{
// Find the index of the minimum v.
size_t ni = 0;
foreach (size_t i; 1 .. NK)
foreach (immutable i; 1 .. NK)
if (values[i].v < values[ni].v)
ni = i;
hams[n_hams] = values[ni];
hams[n_hams].update();
hams[n_hams].update;
}
foreach (size_t i; 0 .. NK)
foreach (immutable i; 0 .. NK)
if (values[i] == hams[n_hams]) {
values[i] = hams[idx[i]];
idx[i]++;
values[i].e[i]++;
values[i].update();
values[i].update;
}
}
@ -102,6 +109,6 @@ in {
void main() {
foreach (n; [1691, 10 ^^ 6, MAX_HAM])
writefln("%8d: %s", n, getHam(n));
foreach (immutable n; [1691, 10 ^^ 6, MAX_HAM])
writefln("%8d: %s", n, n.getHam);
}

View file

@ -6,8 +6,7 @@
import Data.List (sortBy)
import Data.Function (on)
main = do { let (r,t) = nthHam 1000000
; sequence_ [print t, print $ trival t] }
main = let (r,t) = nthHam 1000000 in print t >> print (trival t)
lg3 = logBase 2 3; lg5 = logBase 2 5
logval (i,j,k) = fromIntegral i + fromIntegral j*lg3 + fromIntegral k*lg5
@ -20,6 +19,7 @@ rngval n
| n > 1 = (2.2506 , 0.2887 ) -- around (log $ sqrt 30),
| otherwise = (2.2506 , 0.5771 ) -- says WP
nthHam :: Int -> (Double, (Int, Int, Int))
nthHam n -- n: 1-based: 1,2,3...
| w >= 1 = error $ "Breach of contract: (w < 1): " ++ show w
| m < 0 = error $ "Not enough triples generated: " ++ show (c,n)
@ -28,7 +28,7 @@ nthHam n -- n: 1-based: 1,2,3.
where
(d,w) = rngval n -- correction dist, width
hi = estval n - d -- hi > logval > hi-w
(m,nb) = ( fromInteger $ c - n, length b ) -- m 0-based from top, |band|
(m,nb) = ( fromIntegral $ c - n, length b ) -- m 0-based from top, |band|
(s,res) = ( sortBy (flip compare `on` fst) b, s!!m ) -- sorted decreasing, result
(c,b) = f 0 -- total count, the band
[ ( i+1, -- total triples w/ this (j,k)