The invariant is checked by a debug_assert!, and follows from the previous
commit, as `dec` and `factors` only ever contains coprime numbers:
- true at the start: factor = ∅ and dec = { n¹ } ;
- on every loop iteration, we pull out an element `f` from `dec` and either:
- discover it is prime, and add it to `factors` ;
- split it into a number of coprime factors, that get reinserted into `dec`;
the invariant is maintained, as all divisors of `f` are coprime with all
numbers in `dec` and `factors` (as `f` itself is coprime with them.
As we only add elements to `Decomposition` objects that are coprime with the
existing ones, they are distinct.
- change `const`=>`static` and remove unneeded help/version (supplied by default by `clap`)
- update of the ABOUT description
- move to alphabetical order (where reasonable)
- rename OPT_FILES => ARG_FILES
- change the order of the declarations
- add a trailing "." to ABOUT for consistency
- rename OPT_FILES => ARG_FILES
- move to alphabetical order for OPTIONs (where reasonable)
Co-authored-by: Roy Ivy III <rivy.dev@gmail.com>
If the output of sort is piped to another program that closes the file
descriptor, sort currently panics. The GNU coreutils is able to handle
this case.
Replacing panic with crash_if_err reports the closed pipe and exits
with a return code, which seems like the correct behavior. Tested on
my Mac and the panic disappears.
Add a test which pipes data to sort - it won't protect against this
specific regression, but it increases the test coverage, at least.
Fixes#1608.
* factor: Introduce a type alias for exponents
This way, we can easily replace u8 with a larger type when moving to support
larger integers.
* factor::Factors: Split off a Decomposition type
The new type can be used to represent in-progress factorisations,
which contain non-prime factors.
* factor::Decomposition: Use a flat vector representation
~18% faster than BTreeMap, and ~5% faster than “master”.
* factor::Factors: Use a RefCell rather than copy data when printing
~2.9% faster than the previous commit, ~11% faster than “master” overall.
* CI: Only run rustfmt in one environment
- This displays clippy warnings even when rustfmt fails.
- This avoids displaying 3 copies of the same rustfmt warning as Github
annotations.
- Avoids duplicated work.
* CI: Suppress warnings when building for the oldest toolchain version
We had cases of warnings emitted due to `rustc` bugs that were fixed
in non-obsolete versions.
* factor: Remove a workaround for warnings on obsolete rustc
* factor::numeric::gcd: Switch variable names to be more consistent
* factor::numeric::gcd: Improve comments
* factor::numeric::gcd: Extend loop invariant to v
* factor::Factors: Derive implementations of Eq and PartialEq
* factors::Factors: Implement quickcheck::Arbitrary
This generates uniformly-distributed integers with known factorisations,
and will enable further testing.
* factor: Test against random numbers with known factorisations
* factor::Factors::arbitrary: Simplify method signature
* factor::numeric::gcd: Rename test against the Euclidean algorithm
* factor::numeric::gcd: Add various property-based tests
* factor::numeric::modular_inverse: Rename test
* factor::numeric::modular_inverse: Add test on random values
* factor::numeric: Start refactoring into multiple submodules
No change to the module's interface, but it should make it much easier to
keep the tests right next to the code they are related to.
Moreover, build.rs' dependency is now limited to numeric::{modular_inverse,
traits}, meaning that the rest of it can use build-time generated tables etc.
* factor::numeric: Move gcd (and its test) to a submodule
* factor::numeric: Move Montgomery arithmetic to its own module
Finally hollowed-out numeric.rs
* factor: Move numeric.rs to numeric/mod.rs
* factor::numeric: Fix an erroneous lint on obsolete Rust versions
Ignoring a value of that type is a bug: they are only produced by
`miller_rabbin::test`, which is a pure, but expensive, function.
As such, an ignored value is always either a mistake, or an easy
optimisation opportunity (just remove the useless call to `test`).
Also add a property-based test against the Euclidean implementation.
numeric::gcd got ~50-65% faster, according to criterion. The effect on the
overall system is small, but later PRs will use a lot more GCD computations.
- `DoubleInt::Double` renamed to `DoubleWidth`
- `{as,from}_double()` renamed to `{as,from}_double_width()`.
This should hopefully clarify that this is not a “double precision”
floating-point type, but an integer type with a larger range (used
for storing intermediate results, typ. from a multiplication)
Montgomery only works for odd n, so attempting to construct an instance
for an even number results in a panic!
The most obvious solution is to special-case even numbers.
test() takes a modulus that is known to not be even or <2 (otherwise the
Montgomery value could not be constructed), so those checks can be hoisted
into is_prime() and out of the critical path.