1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-24 09:41:55 +01:00

kata(5kyu): add some egyptians fractions

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-07-21 12:39:12 +02:00
parent 85b94313f6
commit e13f8cd232
Signed by: mfocko
GPG key ID: 7C47D46246790496
2 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,66 @@
use std::cmp::max;
use std::cmp::min;
fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
let t = b;
b = a % b;
a = t;
}
a
}
fn lcm(a: u32, b: u32) -> u32 {
let c = gcd(a, b);
max(a, b) / c * min(a, b)
}
fn format_fraction(fraction: &(u32, u32)) -> String {
if fraction.1 == 1 {
return format!("{}", fraction.0);
}
format!("{}/{}", fraction.0, fraction.1)
}
fn decompose(mut num: u32, mut denom: u32) -> String {
if num == 0 {
return "".to_string();
}
let mut fractions: Vec<(u32, u32)> = Vec::new();
if num >= denom {
fractions.push((num / denom, 1));
num = num % denom;
}
let mut running_denom = 2;
while num > 1 {
if num * running_denom < denom {
running_denom += 1;
continue;
}
fractions.push((1, running_denom));
let new_denom = lcm(denom, running_denom);
let new_num = (new_denom / denom) * num - new_denom / running_denom;
num = new_num;
denom = new_denom;
running_denom += 1;
}
if num >= 1 {
fractions.push((num, denom));
}
fractions
.iter()
.map(format_fraction)
.reduce(|acc, frac| acc + ", " + &frac)
.unwrap()
}

View file

@ -74,6 +74,7 @@
- [Perimeter of squares in a rectangle](https://www.codewars.com/kata/559a28007caad2ac4e000083) - [solution](5kyu/perimeter_of_squares_in_a_rectangle) - [Perimeter of squares in a rectangle](https://www.codewars.com/kata/559a28007caad2ac4e000083) - [solution](5kyu/perimeter_of_squares_in_a_rectangle)
- [My smallest code interpreter (aka Brainf**k)](https://www.codewars.com/kata/526156943dfe7ce06200063e) - [solution](5kyu/my_smallest_code_interpreter_bf) - [My smallest code interpreter (aka Brainf**k)](https://www.codewars.com/kata/526156943dfe7ce06200063e) - [solution](5kyu/my_smallest_code_interpreter_bf)
- [Count IP Addresses](https://www.codewars.com/kata/526989a41034285187000de4) - [solution](5kyu/count_ip_addresses) - [Count IP Addresses](https://www.codewars.com/kata/526989a41034285187000de4) - [solution](5kyu/count_ip_addresses)
- [Some Egyptian fractions](https://www.codewars.com/kata/54f8693ea58bce689100065f) - [solution](5kyu/some_egyptians_fractions)
### C ### C