jzakiya
February 22, 2024, 9:17pm
1
I looked in `The Book`

and `StackOverflow`

, but still can't get this to work.

I want to translate this Crystal method to Rust.
`n`

is an integer of `usize`

, as well as the output, and `log()`

is the natural log.

```
def approx_nthprime(n)
b, logn = (0.5722 * (n**0.0088)), Math.log(n)
a = b * Math.log(logn)
(n * (logn + a) + 3).to_u64
end
fn approx_nthprime(n: usize) -> usize {
let (b, logn) = (0.5722 * (n**0.0088), log(n));
let a = b * log(logn);
(n * (logn + a) + 3) as usize
}
```

Thanks in advance.

jw013
February 22, 2024, 9:26pm
2
Try looking at the std library docs: f64 - Rust

You need to explicitly convert to float
Natural log is called `ln`

`ln`

is a method instead of a free function
power is also a method, `powf`

```
pub fn approx_nthprime(n: usize) -> usize {
let n = n as f64;
let (b, logn) = (0.5722 * n.powf(0.0088), n.ln());
let a = b * logn.ln();
(n * (logn + a) + 3.0) as usize
}
```

1 Like

jzakiya
February 22, 2024, 9:51pm
4
I had everything you had except I didn't first convert `n`

to floating point.
Now I know what that error message meant.

If we every meet, I'll buy you a drink of your choice.

1 Like

system
Closed
May 22, 2024, 9:51pm
5
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.