Calculate all the prime numbers between two given numbers I've made an application that calculates all the prime numbers between two given numbers and prints then into a .txt document... anything I can improve?
use std::io;
use std::fs::{OpenOptions};
use std::io::{Write, BufWriter};

fn main() {

    loop{
        
        let mut format = 1;

        let mut input = String::new();
        println!(&quot;Say a start for the prime loop! &quot;);
        
        io::stdin().read_line(&amp;mut input).unwrap();
        let start: u128 = input.trim().parse().unwrap();
        

        let mut input = String::new();
        println!(&quot;Say an end for the prime loop! &quot;);       

        io::stdin().read_line(&amp;mut input).unwrap();
        let end: u128 = input.trim().parse().unwrap();

        
        let path = &quot;path/to/file.txt&quot;;

        let f = OpenOptions::new()
            .write(true)
            .open(path)
            .expect(&quot;Could not open file&quot;);

        let mut f = BufWriter::new(f);

        for i in start..end{    
            if prime(i) == true{
                f.write_all(i.to_string().as_bytes()).expect(&quot;unable to write to file&quot;);
                f.write_all(b&quot;\t&quot;).expect(&quot;unable to write to file&quot;);
                format += 1;
            }
            
            if format == 10{
                f.write_all(b&quot;\n&quot;).expect(&quot;unable to write to file&quot;);
                format = 0;
            }
            
        }
    }
}


fn prime(x: u128) -&gt; bool {

    if x == 4 || x == 6 || x == 8 || x == 9{ //The loop doesnt quite work for numbers below 10 so this is for those numbers
        return false; 
    }
    for i in 2..((x as f64).sqrt() as u128){   
        if x % i == 0 { return false; }  //modulo to see if the number is dividable by variable i
    }
    true
}
```
The efficiency of the prime function is probably the most interesting aspect of this exercise. You're limiting the search space to up to the square root of the number, which is good, but the fact that you're looking for all primes in a range would make many other prime searching algorithms usable, such as the Sieve of Eratosthenes, which fairly efficiently gets you the primes from 2 through N. The issue is that which algorithm to use in your case depends on runtime inputs: if the user asks for prime numbers between 2 and a large N there are heaps of efficient algorithms. If they instead ask for primes between a large N and N+1 it probably makes much more sense to just look at those two numbers. I'm not a mathematician, but choosing which algorithm to use depending on the range seems like a subject worth studying.
I don't see why the algorithm would not work for numbers 4, 6, 8 and 9 - that seems like either a subtle bug in the loop or just a misunderstanding. I would add some test cases to prime and verify it with lots of different inputs.
prime takes a u128, but then drops precision when converting it to f64 before taking the square root. Since Integer -&gt; Float -&gt; Integer conversion can succeed while changing what the value is, I suspect there are corner cases where the range is miscalculated. See also this discussion.