Parallel Prime k-tuple finder

I wrote a prime k-tuple finder using the rug crate for bignum arithmetic. I am new to Rust and I am coming from a C++ background. I would like to get some feedback on my code, what I could do to make it faster and/or cleaner. The main work is done in the sieving and the primality testing functions. Any comment is welcome!

1 Like

One thing that immediately sticks out is that the formatting is not idiomatic (including the positioning of braces and a lot of unnecessary vertical whitespace). Run your code through rustfmt to get a good first approximation on Rust style.

While you are at it, use the official Rust linter, Clippy. It will discover automatically-detectible stylistic problems without another human needing to go through all of the code.

Another thing is that wheel_factorization has 12 separate parameters. I'm pretty sure that's not acceptable in C++, either. It should probably be a struct. The same applies to get_eliminated_factors.


Thanks. I applied your suggestions. What about now?

1 Like

A kindly reminder: iterators are preferred over index-based loop. The later can have a performance overhead due to Rust's bounds checking. You can find more about it here.

For example, you code here can be replaced by

for (b, mut sieve_word) in factors_table[..sieve_words].iter().copied().enumerate() {
  // ...

And the loop here can be replaced by utilizing slice::windows.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.