Skip to content

Commit 52bfa65

Browse files
committed
refactor: largest_prime_factor refactored
1 parent 23cf27a commit 52bfa65

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

largest_prime_factor/src/main.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,25 @@ fn main() {
55
fn largest_prime_factor(x: i64) -> f64 {
66
let mut largest: f64 = -1.0;
77
let mut x_copy:f64 = x.clone() as f64;
8-
let x_start_value:f64 = x.clone() as f64;
8+
let upper_limit:i64 = (x as f64).sqrt() as i64;
99

1010
while x_copy % 2.0 == 0.0 {
1111
largest = 2.0;
1212
x_copy /= 2.0;
1313
}
14-
1514
while x_copy % 3.0 == 0.0 {
1615
largest = 3.0;
1716
x_copy /= 3.0;
1817
}
19-
let mut i: f64 = 5.0;
20-
while i <= x_start_value.sqrt()
21-
{
22-
while x_copy % i == 0.0 {
23-
largest = i;
24-
x_copy = x_copy / i;
18+
for i in (5..upper_limit).step_by(6) {
19+
while x_copy % i as f64 == 0.0 {
20+
largest = i as f64;
21+
x_copy = x_copy / i as f64;
2522
}
26-
while x_copy % (i + 2.0) == 0.0 {
27-
largest = i + 2.0;
28-
x_copy = x_copy / (i + 2.0);
23+
while x_copy % (i + 2) as f64 == 0.0 {
24+
largest = (i + 2) as f64;
25+
x_copy = x_copy / (i + 2) as f64;
2926
}
30-
i += 6.0;
3127
}
3228
largest
3329
}

0 commit comments

Comments
 (0)