bin/csolve in congruence_solver-0.5.0 vs bin/csolve in congruence_solver-0.5.1
- old
+ new
@@ -3,27 +3,30 @@
require "polynomial_interpreter"
SOLVE_CONGRUENCE_BENCH_FILE = "../bench/solve_congruence_bm.rb"
-
-if ARGV.pop == "bench"
+if ARGV[0] == "bench"
require_relative SOLVE_CONGRUENCE_BENCH_FILE
exit(0)
+elsif ARGV[0] == "-c"
+ congruence = ARGV[1]
+else
+ puts "Congruence to solve:"
+ congruence = STDIN.gets
end
CONGRUENCE_FORMAT = "(lhs polynomial) = (rhs polynomial) mod (modulus)"
CONGRUENCE_INVALID_MSG = "Congruence invalid: congruences must be of form:\n#{CONGRUENCE_FORMAT}"
POLYNOMIAL_FORMAT = "ax^b+cx^d...\n(integer coefficients, positive integer exponents, order irrelevant)"
LHS_INVALID_MSG = "Left hand polynomial invalid: polynomials must be of form: #{POLYNOMIAL_FORMAT}"
RHS_INVALID_MSG = "Right hand polynomial invalid: polynomials must be of form: #{POLYNOMIAL_FORMAT}"
MOD_INVALID_MSG = "Mod invalid: modulus must be an integer greater than 2"
-puts "Congruence to solve:"
begin
- coeffs, mod = PolynomialInterpreter.read_congruence(STDIN.gets)
+ coeffs, mod = PolynomialInterpreter.read_congruence(congruence)
rescue ArgumentError => e
if(e == PolynomialInterpreter::Errors::CONGRUENCE_INVALID)
STDERR.puts CONGRUENCE_INVALID_MSG
exit(1)
@@ -44,11 +47,11 @@
end
end
solutions = CongruenceSolver.lift(coeffs, mod).sort
-if solutions.empty?
+if solutions.empty?
puts "No solution."
else
puts "Solutions:"
solutions.each_with_index {|sol, i| puts "(#{i}) #{sol}"}
-end
\ No newline at end of file
+end