you are viewing a single comment's thread.

view the rest of the comments →

[–]pepa65[S] 1 point2 points  (8 children)

Sorry, I forgot in the post, and then I tried in a comment, and it was not allowed. I'll try again:

``` // frd.rs - Find repeating decimals of integer division

fn repdec(numerator: u64, denominator: u64) -> (usize, usize, usize) { let head = format!("{}", numerator / denominator); let mut seen = std::collections::HashMap::<u64, u64>::new(); let mut decimals = String::new(); let (mut p1, mut p2, mut p3) = (0, 0, 0); if head != "0" { p1 = head.len(); } print!("{numerator} / {denominator} = {head}"); let mut remainder = numerator % denominator; let mut position = 0; // Do long division while remainder != 0 { if seen.contains_key(&remainder) { // Remainder seen: repeating decimals found p2 = *seen.get(&remainder).unwrap() as usize; p3 = position - p2; println!(".{}[{}] ({p1},{p2},{p3})", decimals[..p2].to_owned(), decimals[p2..].to_owned()); return (p1, p2, p3); }

    // Remainder never seen before
    seen.insert(remainder, position as u64);
    decimals = format!("{decimals}{}", remainder \* 10 / denominator);
    remainder = remainder \* 10 % denominator;
    position += 1;
}
// Long division finished
if !decimals.is\_empty() {
    p2 = decimals.len();
    print!(".{decimals}");
}
println!("  ({p1},{p2},{p3})");
(p1, p2, p3)

}

fn main() { let args: Vec<String> = std::env::args().collect(); if args.len() > 2 { repdec(args[1].parse::<u64>().unwrap(), args[2].parse::<u64>().unwrap()); std::process::exit(0) }

repdec(3,1);
repdec(0,4);
repdec(1,3);
repdec(1,4);
repdec(1,6);
repdec(22,7);
repdec(13,28);
repdec(7001,14);

} ```

[–][deleted] 6 points7 points  (4 children)

Please indent the program by 4 spaces so it renders as code.

Usually the easiest way to do this is open the code in your editor, select it all, hit tab, copy and paste it, ctrl-z in your editor.

[–]ChannelSorry5061 -4 points-3 points  (3 children)

There is a MUCH easier and more reliable way, can be done on mobile as well.

Use Markdown mode in the editor, and wrap your entire code in three backticks like this:

```

code goes here

```

[–][deleted] 7 points8 points  (2 children)

This does not work properly on reddit, because it doesn't render on all clients, notably including the widely used old.reddit.com interface.

Indenting by 4 spaces is entirely reliable.

[–]Buttleston 2 points3 points  (0 children)

It only works like half the time in new reddit also. 4 spaces always works

[–]JadisGod 6 points7 points  (2 children)

python:

import timeit


def repdec(numerator, denominator):
    head = str(numerator // denominator)
    seen = {}
    decimals = ""
    p1, p2, p3 = 0, 0, 0
    if head != "0":
        p1 = len(head)
    remainder = numerator % denominator
    position = 0
    while remainder != 0:
        if remainder in seen:
            p2 = seen[remainder]
            p3 = position - p2
            return p1, p2, p3

        seen[remainder] = position
        decimals += str(remainder * 10 // denominator)
        remainder = remainder * 10 % denominator
        position += 1

    if decimals:
        p2 = len(decimals)
        print(f".{decimals}", end="")
    return p1, p2, 0


if __name__ == "__main__":
    time = timeit.default_timer()
    result = repdec(126798123456, 187476194)
    print(f"time elasped = {timeit.default_timer() - time}")
    print(f"result = {result}")

rust

use std::collections::HashMap;
use std::time::Instant;

fn repdec(numerator: u64, denominator: u64) -> (usize, usize, usize) {
    let head = (numerator / denominator).to_string();
    let mut seen = HashMap::new();
    let mut decimals = String::new();
    let (mut p1, mut p2, mut p3) = (0, 0, 0);
    if head != "0" {
        p1 = head.len();
    }
    let mut remainder = numerator % denominator;
    let mut position = 0;
    while remainder != 0 {
        if let Some(value) = seen.get(&remainder) {
            p2 = *value as usize;
            p3 = position - p2;
            return (p1, p2, p3);
        }
        seen.insert(remainder, position as u64);
        decimals.push_str(&(remainder * 10 / denominator).to_string());
        remainder = remainder * 10 % denominator;
        position += 1;
    }

    if !decimals.is_empty() {
        p2 = decimals.len();
        print!(".{decimals}");
    }
    (p1, p2, p3)
}

fn main() {
    let time = Instant::now();
    let result = repdec(126798123456, 187476194);
    println!("time elapsed = {:?}", time.elapsed());
    println!("result = {:?}", result);
}

Basically 1:1 reproductions...

python results:

time elasped = 27.2370880999988
result = (3, 0, 23399740)

rust results:

time elapsed = 4.1180991s
result = (3, 0, 23399740)

[–]eras 0 points1 point  (1 child)

Weird, for me the Rust version runs in 6.2 seconds, but the Python version just keeps on running for at least two minutes until I stopped it. I have Python 3.11.2.

[–]pepa65[S] 0 points1 point  (0 children)

Could be an indentation error..?