diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-06-27 16:47:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-27 16:47:44 +0100 |
| commit | 52c25f574cf0f382c6095cd86af562df763f7ee0 (patch) | |
| tree | e917ec40e48c044b3dd7abb7e21f49b66c14a7a1 | |
| parent | 8d17cadee501fce53279c074d70c0a9aed353985 (diff) | |
| parent | ea242cfbc9597f63d533bdfc39c698f314ca2c01 (diff) | |
| download | perlweeklychallenge-club-52c25f574cf0f382c6095cd86af562df763f7ee0.tar.gz perlweeklychallenge-club-52c25f574cf0f382c6095cd86af562df763f7ee0.tar.bz2 perlweeklychallenge-club-52c25f574cf0f382c6095cd86af562df763f7ee0.zip | |
Merge pull request #1870 from oWnOIzRi/week066
add rust solution for task 1
| -rw-r--r-- | challenge-066/steven-wilson/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-066/steven-wilson/rust/ch-1.rs | 22 |
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 +} |
