aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-066/steven-wilson/perl/ch-1.pl2
-rw-r--r--challenge-066/steven-wilson/rust/ch-1.rs22
2 files changed, 23 insertions, 1 deletions
diff --git a/challenge-066/steven-wilson/perl/ch-1.pl b/challenge-066/steven-wilson/perl/ch-1.pl
index c6610b3adc..abed01885e 100644
--- a/challenge-066/steven-wilson/perl/ch-1.pl
+++ b/challenge-066/steven-wilson/perl/ch-1.pl
@@ -21,7 +21,7 @@ sub divide {
}
if ( ( ( $M < 0 ) && ( $N > 0 ) ) || ( ( $N < 0 ) && ( $M > 0 ) ) ) {
$q = -$q;
- if ($a != 0) { --$q }; # if neg and has remainder, floor number
+ $q-- if $a != 0; # if neg and has remainder, floor number
}
$q;
}
diff --git a/challenge-066/steven-wilson/rust/ch-1.rs b/challenge-066/steven-wilson/rust/ch-1.rs
new file mode 100644
index 0000000000..2303f09f9d
--- /dev/null
+++ b/challenge-066/steven-wilson/rust/ch-1.rs
@@ -0,0 +1,22 @@
+fn main() {
+ println!("5 / 2 = {}", divide( 5, 2 ));
+ println!("-5 / 2 = {}", divide( -5, 2 ));
+ println!("-5 / -2 = {}", divide( -5, -2 ));
+}
+
+fn divide ( m: i32, n: i32) -> i32 {
+ let mut a = m.abs();
+ let b = n.abs();
+ let mut q = 0;
+ while a >= b {
+ q += 1;
+ a -= b;
+ }
+ if ( m < 0 && n > 0 ) || ( n < 0 && m > 0 ) {
+ q *= -1;
+ if a != 0 {
+ q -= 1;
+ }
+ }
+ q
+}