diff --git a/src/maths/solver.rs b/src/maths/solver.rs index 3b57bf5..a5787dd 100644 --- a/src/maths/solver.rs +++ b/src/maths/solver.rs @@ -117,13 +117,11 @@ fn comp_sqrt(c: GaussianRational) -> MyCompSqrt { } // Here we multiply by 4 to avoid dividing by 2 later and ease ascii art - if sqrt.denominator != rational.denominator { - let mem = sqrt.denominator; - sqrt.denominator *= rational.denominator * 4; - sqrt.numerator_natural *= rational.denominator; - rational.numerator *= mem; - rational.denominator *= mem * 4; - } + let mem = sqrt.denominator; + sqrt.denominator *= rational.denominator * 4; + sqrt.numerator_natural *= rational.denominator; + rational.numerator *= mem; + rational.denominator *= mem * 4; let gcd = gcd(gcd(sqrt.denominator, sqrt.numerator_natural), rational.numerator); @@ -155,18 +153,22 @@ fn get_strings(left_part: Rational, sqrt_delta: MySqrt) -> (String, String, Stri let mut string = String::from(""); if b != 0 || c != 0 { if b != 1 && c != 1 { - string = format!("+ {b} * sqrt({c})"); + string = format!("{b} * sqrt({c})"); } else if b != 1 { - string = format!("+ {b}"); + string = format!("{b}"); } else if c != 1 { - string = format!("+ sqrt({c})"); + string = format!("sqrt({c})"); } else { - string = format!("+ 1"); + string = format!("1"); } } - let string_a = format!("{a} {string}"); - let string_b = format!("{} {string}", -a); + let mut string_a = format!("{string}"); + let mut string_b = format!("{string}"); + if a != 0 { + string_a = format!("{a} + {string}"); + string_b = format!("{} + {string}", -a); + } let len = std::cmp::max(string_a.len(), format!("{d}").len()) + 2; @@ -226,12 +228,14 @@ fn degree_two_complex(a: GaussianRational, b: GaussianRational, c: GaussianRatio let sqrt_delta = comp_sqrt(delta); println!("{:?}", sqrt_delta); - let b = b / GaussianRational::new(Rational::new(2, 1), Rational::new(0, 1)); + println!("b {:?}", b); + let b = b / GaussianRational::new(Rational::new(-2, 1), Rational::new(0, 1)); println!("BEWARE IM GOING TO PRINT THE SOLUTIONS MDR"); /* + 2362 / 473 + sqrt(7896511271) \ 49 / -473 + sqrt(7896511271) \ ---- + sqrt | ------------------------ | + -- i sign sqrt | ------------------------- | i 32 \ 45150 / 13 \ 45150 / @@ -239,8 +243,6 @@ fn degree_two_complex(a: GaussianRational, b: GaussianRational, c: GaussianRatio */ - let b_real_len = std::cmp::max(format!("{}", b.real.denominator).len(), format!("{}", b.real.numerator).len()) + 2; - let b_imag_len = std::cmp::max(format!("{}", b.imaginary.denominator).len(), format!("{}", b.imaginary.numerator).len()) + 2; let mut sign = '+'; if sqrt_delta.sign < 0 { @@ -248,13 +250,56 @@ fn degree_two_complex(a: GaussianRational, b: GaussianRational, c: GaussianRatio } let (s1, s2, s3, s4, s5, s6) = get_strings(sqrt_delta.rational, sqrt_delta.sqrt); - let space1 = b_real_len - format!("{}", b.real.numerator).len(); - let space2 = b_imag_len - format!("{}", b.imaginary.numerator).len(); - let space3 = b_real_len - format!("{}", b.real.denominator).len(); - let space4 = b_imag_len - format!("{}", b.imaginary.denominator).len(); + let mut string1 = String::from(""); + let mut string2 = String::from(""); + let mut string3 = String::from(""); + + if b.real.numerator != 0 { + if b.real.denominator != 1 { + let b_real_len = std::cmp::max(format!("{}", b.real.denominator).len(), format!("{}", b.real.numerator).len()) + 2; + let space_num = b_real_len - format!("{}", b.real.numerator).len(); + let space_dem = b_real_len - format!("{}", b.real.denominator).len(); + string1 = format!("{}{}{}", " ".repeat(space_num / 2), b.real.numerator, " ".repeat(space_num / 2 + space_num % 2)); + string2 = format!("{}", "-".repeat(b_real_len)); + string3 = format!("{}{}{}", " ".repeat(space_dem / 2), b.real.denominator, " ".repeat(space_dem / 2 + space_dem % 2)); + } else { + let string = format!("{}", b.real.numerator); + string1 = format!("{}", " ".repeat(string.len())); + string2 = format!("{string}"); + string3 = format!("{}", " ".repeat(string.len())); + } + } + + string1 = format!("{string1} /{s1}\\ "); + string2 = format!("{string2} + sqrt | {s2} | "); + string3 = format!("{string3} \\{s3}/ "); + + if b.imaginary.numerator != 0 { + if b.imaginary.denominator != 1 { + let b_imag_len = std::cmp::max(format!("{}", b.imaginary.denominator).len(), format!("{}", b.imaginary.numerator).len()) + 2; + let space_num = b_imag_len - format!("{}", b.imaginary.numerator).len(); + let space_dem = b_imag_len - format!("{}", b.imaginary.denominator).len(); + string1 = format!("{string1} {}{}{} ", " ".repeat(space_num / 2), b.imaginary.numerator, " ".repeat(space_num / 2 + space_num % 2)); + string2 = format!("{string2}+ {} i ", "-".repeat(b_imag_len)); + string3 = format!("{string3} {}{}{} ", " ".repeat(space_dem / 2), b.imaginary.denominator, " ".repeat(space_dem / 2 + space_dem % 2)); + } else { + let string = format!("{}", b.imaginary.numerator); + string1 = format!("{string1} {} ", " ".repeat(string.len())); + string2 = format!("{string2}+ {string}i "); + string3 = format!("{string3} {} ", " ".repeat(string.len())); + } + } + + string1 = format!("{string1} /{s4}\\ "); + string2 = format!("{string2}{sign} sqrt | {s5} | i"); + string3 = format!("{string3} \\{s6}/ "); + +/* let string1 = format!("{}{}{} / {s1} \\ {}{}{} / {s4} \\ ", " ".repeat(space1 / 2), b.real.numerator, " ".repeat(space1 / 2 + space1 % 2), " ".repeat(space2 / 2), b.imaginary.numerator, " ".repeat(space2 / 2 + space2 % 2)); let string2 = format!("{} + sqrt | {s2} | + {} i {sign} sqrt | {s5} | i", "-".repeat(b_real_len), "-".repeat(b_imag_len)); let string3 = format!("{}{}{} \\ {s3} / {}{}{} \\ {s6} / ", " ".repeat(space3 / 2), b.real.denominator, " ".repeat(space3 / 2 + space3 % 2), " ".repeat(space4 / 2), b.imaginary.denominator, " ".repeat(space4 / 2 + space4 % 2)); + */ + println!("{string1}"); println!("{string2}"); println!("{string3}");