Ad
Code
Diff
  • fn flip_the_number(x: &u64) -> u64 {
        x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap()
    }
    • def flip_the_number(x):
    • return int(str(x)[::-1])
    • fn flip_the_number(x: &u64) -> u64 {
    • x.to_string().chars().rev().collect::<String>().parse::<u64>().unwrap()
    • }
Numbers
Data Types
Integers
Algorithms
Logic
Code
Diff
  • fn digits(n: u64) -> usize {
        match n {
            0 => 1,
            _ => (n as f64).log10().floor() as usize + 1
        }
    }
    • fn digits(n: u64, base: u64) -> usize {
    • digits_iter(n, base).count()
    • }
    • fn digits_iter(n: u64, base: u64) -> impl Iterator<Item = u64> {
    • let mut n = Some(n);
    • std::iter::from_fn(move || match n {
    • Some(x) => {
    • let digit = x % base;
    • n = Some(x / base);
    • if let Some(0) = n {
    • n = None;
    • }
    • Some(digit)
    • }
    • _ => None,
    • })
    • }
    • fn digits(n: u64) -> usize {
    • match n {
    • 0 => 1,
    • _ => (n as f64).log10().floor() as usize + 1
    • }
    • }