From 6fb0c2480ffb0d2fc69866af19b4b39a1b1424ae Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 19 Nov 2023 12:46:53 +0100 Subject: [PATCH] fix(maths::solver): simplify_sqrt Last prime factor now always in array, the bug was that if the last prime factor is bigger that sqrt(n) it wasn't pushed since the loop escaped --- src/maths/solver.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/maths/solver.rs b/src/maths/solver.rs index 3860e25..81e5017 100644 --- a/src/maths/solver.rs +++ b/src/maths/solver.rs @@ -63,8 +63,12 @@ fn simplify_sqrt(mut n: i128) -> (i128, i128) { } } } + if n != 1 { + prime_factors.push(n); + } let (mut natural, mut irrational) = (1, 1); prime_factors = prime_factors.into_iter().rev().collect(); + dbg!(&prime_factors); while prime_factors.len() > 1 { let pop = prime_factors.pop().unwrap(); if *prime_factors.last().unwrap() == pop { @@ -307,7 +311,11 @@ fn degree_two_complex(a: GaussianRational, b: GaussianRational, c: GaussianRatio fn sqrt(n: Rational, a: Rational) -> MySqrt { let numerator_irrational = n.numerator * n.denominator; + dbg!(numerator_irrational); let (mut numerator_natural, numerator_irrational) = simplify_sqrt(numerator_irrational); + dbg!(numerator_natural); + dbg!(numerator_irrational); + numerator_natural *= a.denominator; let mut denominator = n.denominator * 2 * a.numerator; let gcd = gcd(numerator_natural, denominator); @@ -401,6 +409,9 @@ fn degree_two_real(a: Rational, b: Rational, c: Rational) { let mut sqrt_delta = sqrt(delta, a); let mut left_part = (-1 * b) / (2 * a); + println!("sqrt delta {:?}", sqrt_delta); + println!("left part {:?}", left_part); + let tmp = left_part.denominator; left_part.denominator *= sqrt_delta.denominator; @@ -408,7 +419,7 @@ fn degree_two_real(a: Rational, b: Rational, c: Rational) { sqrt_delta.numerator_natural *= tmp; sqrt_delta.denominator *= tmp; - +/* let gcd = gcd(gcd(sqrt_delta.numerator_natural, left_part.numerator), left_part.denominator); left_part.denominator /= gcd; @@ -416,7 +427,7 @@ fn degree_two_real(a: Rational, b: Rational, c: Rational) { sqrt_delta.numerator_natural /= gcd; sqrt_delta.denominator /= gcd; - +*/ print_degree_two_real(left_part, sqrt_delta, imaginary); }