#![cfg_attr(not(feature = "std"), no_std)]
#[macro_export]
macro_rules! swap {
(mut $a:expr, mut $b:expr) => {
let temp = $a;
$a = $b;
$b = temp;
};
}
pub trait Luhn {
fn luhn(&self) -> bool;
}
impl Luhn for u128 {
fn luhn(&self) -> bool {
luhn_str(itoa::Buffer::new().format(*self))
}
}
impl Luhn for u64 {
fn luhn(&self) -> bool {
luhn_str(itoa::Buffer::new().format(*self))
}
}
fn luhn_str(string: &str) -> bool {
let mut w = 1;
let mut t = 0;
for c in string.chars().rev() {
let digit = c.to_digit(10).unwrap();
let mut d = w * digit;
if d > 9 {
d -= 9;
}
t += d;
w = 3 - w;
}
t - (t / 10) * 10 == 0
}
pub fn crypt(sentence: &str, key: u32) -> heapless::String<128_usize> {
let mut result = heapless::String::<128_usize>::new();
for c in sentence.chars() {
let c_num: u32 = c as u32;
let c_num_convert: u32 = c_num ^ key;
let c_convert: char = char::from_u32(c_num_convert).unwrap();
let _ = result.push(c_convert);
}
result
}
pub fn egyptian_fractions(numerator: usize, denominator: usize) -> heapless::String<256_usize> {
let mut result = heapless::String::<256_usize>::new();
let mut m: usize = numerator;
let mut n: usize = denominator;
let mut q: usize;
let _ = result.push_str(itoa::Buffer::new().format(m));
let _ = result.push_str("/");
let _ = result.push_str(itoa::Buffer::new().format(n));
let _ = result.push_str(" = ");
while n % m != 0_usize {
q = n / m + 1;
let _ = result.push_str("1/");
let _ = result.push_str(itoa::Buffer::new().format(q));
let _ = result.push_str(" + ");
m = m * q - n;
n *= q;
}
let _ = result.push_str("1/");
let _ = result.push_str(itoa::Buffer::new().format(n / m));
result
}
pub fn radix_conversion(
d_1: usize,
x_1: &heapless::Vec<usize, 16>,
d_2: usize,
) -> (heapless::String<128_usize>, heapless::String<128_usize>) {
let mut result_1 = heapless::String::<128_usize>::new();
let mut result_2 = heapless::String::<128_usize>::new();
let mut x_1_10: usize = 0_usize;
for i in 0_usize..x_1.len() {
x_1_10 += x_1[i] * d_1.pow(i as u32);
}
let _ = result_1.push_str("(");
let _ = result_1.push_str(itoa::Buffer::new().format(x_1_10));
let _ = result_1.push_str(")_{");
let _ = result_1.push_str(itoa::Buffer::new().format(d_1));
let _ = result_1.push_str("} = ");
for i in 0_usize..x_1.len() {
let _ = result_1.push_str(" +");
let _ = result_1.push_str(itoa::Buffer::new().format(x_1[i]));
let _ = result_1.push_str("x");
let _ = result_1.push_str(itoa::Buffer::new().format(d_1));
let _ = result_1.push_str("^");
let _ = result_1.push_str(itoa::Buffer::new().format(i));
}
let mut x_2: heapless::Vec<usize, 16> = heapless::Vec::<usize, 16>::new();
let mut max: usize = 1_usize;
while max <= x_1_10 {
max *= d_2;
let _ = x_2.push(0_usize);
}
for i in (0_usize..x_2.len()).rev() {
let pow = d_2.pow(i as u32);
x_2[i] = x_1_10 % pow;
x_1_10 -= x_1_10 / pow;
}
let _ = result_2.push_str("(");
let _ = result_2.push_str(itoa::Buffer::new().format(x_1_10));
let _ = result_2.push_str(")_{");
let _ = result_2.push_str(itoa::Buffer::new().format(d_2));
let _ = result_2.push_str("} = ");
for i in 0_usize..x_2.len() {
let _ = result_2.push_str(" +");
let _ = result_2.push_str(itoa::Buffer::new().format(x_2[i]));
let _ = result_2.push_str("x");
let _ = result_2.push_str(itoa::Buffer::new().format(d_2));
let _ = result_2.push_str("^");
let _ = result_2.push_str(itoa::Buffer::new().format(i));
}
(result_1, result_2)
}
pub fn inverse_mapping_sort(a: &heapless::Vec<i8, 256_usize>) -> heapless::Vec<i8, 256_usize> {
let mut max: i8 = core::i8::MIN;
let mut min: i8 = core::i8::MAX;
for value in a {
if max < *value {
max = *value;
}
if min > *value {
min = *value;
}
}
let mut b: heapless::Vec<i8, 256_usize> = heapless::Vec::<i8, 256_usize>::new();
for _ in 0_usize..a.len() {
let _ = b.push(0_i8);
}
let mut index: heapless::Vec<i16, 256_usize> = heapless::Vec::<i16, 256_usize>::new();
for _ in 0_usize..((max - min + 1_i8) as usize) {
let _ = index.push(-1_i16);
}
let mut next: heapless::Vec<i16, 256_usize> = heapless::Vec::<i16, 256_usize>::new();
for _ in 0_usize..a.len() {
let _ = next.push(0_i16);
}
for i in (0_usize..a.len()).rev() {
let x: i8 = a[i] - min;
next[i] = index[x as usize];
index[x as usize] = i as i16;
}
let mut j: usize = 0;
for x in 0_usize..=((max - min) as usize) {
let mut i: i16 = index[x];
while i >= 0 && j < a.len() {
b[j] = a[i as usize];
j += 1_usize;
i = next[i as usize];
}
}
b
}