diff --git a/examples/demo/triples.js b/examples/demo/triples.js new file mode 100644 index 0000000..087dea0 --- /dev/null +++ b/examples/demo/triples.js @@ -0,0 +1,50 @@ +const tripleCache = new Map(); +const gcdCache = new Map(); + +function gcd(a, b) { + const key = (a << 16) | b; + if (gcdCache.has(key)) return gcdCache.get(key); + const result = b === 0 ? a : gcd(b, a % b); + gcdCache.set(key, result); + return result; +} + +function pushMultiples(a, b, c, k, limit, acc) { + if (k * c > limit) return acc; + acc.push([k * a, k * b, k * c]); + return pushMultiples(a, b, c, k + 1, limit, acc); +} + +function generate(m, n, limit, acc) { + if (m * m + 1 > limit) return acc; + if (n >= m) return generate(m + 1, 1, limit, acc); + + const c = m * m + n * n; + if (c > limit) return generate(m + 1, 1, limit, acc); + + if (((m ^ n) & 1) === 1 && gcd(m, n) === 1) { + const p = m * m - n * n; + const q = 2 * m * n; + const [a, b] = p < q ? [p, q] : [q, p]; + pushMultiples(a, b, c, 1, limit, acc); + } + + return generate(m, n + 1, limit, acc); +} + +function pythagoreanTriples(limit) { + if (tripleCache.has(limit)) return tripleCache.get(limit); + const triples = generate(2, 1, limit, []); + triples.sort((x, y) => x[2] - y[2] || x[0] - y[0]); + tripleCache.set(limit, triples); + return triples; +} + +const start = performance.now(); +const result = pythagoreanTriples(200); +const end = performance.now(); + +console.log(`Found ${result.length} Pythagorean triples with c ≤ 200`); +console.log(result); + +console.log(`Time: ${(end - start).toFixed(4)} ms (${((end - start) * 1000).toFixed(2)} µs)`); diff --git a/vendor/mir.wrap b/vendor/mir.wrap index 54dc1e6..f1ab2a1 100644 --- a/vendor/mir.wrap +++ b/vendor/mir.wrap @@ -1,7 +1,6 @@ [wrap-git] -url = https://github.com/vnmakarov/mir.git +url = https://github.com/themackabu/mir.git revision = head -patch_directory = mir depth = 1 [provide] diff --git a/vendor/packagefiles/mir/meson.build b/vendor/packagefiles/mir/meson.build deleted file mode 100644 index 90299da..0000000 --- a/vendor/packagefiles/mir/meson.build +++ /dev/null @@ -1,34 +0,0 @@ -project('mir', 'c') - -cc = meson.get_compiler('c') -cpu = host_machine.cpu_family() - -# mir-gen-.c files are #include'd directly inside mir-gen.c — they are -# NOT standalone compilation units and must not be listed as sources here. -mir_sources = files('mir.c', 'mir-gen.c') - -mir_warn_args = cc.get_supported_arguments([ - '-Wno-unused-function', - '-Wno-unused-variable', - '-Wno-unused-parameter', - '-Wno-sign-compare', - '-Wno-missing-field-initializers', - '-Wno-implicit-fallthrough', - '-Wno-pedantic', - '-Wno-format-nonliteral', - '-Wno-cast-function-type', - '-Wno-double-promotion', - '-Wno-conversion', -]) - -mir_lib = static_library( - 'mir', - mir_sources, - c_args: mir_warn_args, - install: false, -) - -mir_dep = declare_dependency( - link_with: mir_lib, - include_directories: include_directories('.'), -)