Harmonic series: Difference between revisions

m
Add space between comment marker and text
(J draft)
m (Add space between comment marker and text)
Line 1,824:
println!("Harmonic number 100 = {}", harmonic_number(100));
 
// In order to avoid recomputing all the terms in the sum for every harmonic number
// we save the value of the harmonic series between loop iterations
// and just add 1/iter to it.
 
let mut target = 1;
Line 1,838:
}
 
// Compute the next term in the harmonic series
iter += 1;
harmonic_number += Ratio::from_integer(iter.into()).recip();
Line 1,845:
 
fn harmonic_number(n: u64) -> Ratio<BigInt> {
// Convert each integer from 1 to n into an arbitrary precision rational number
// and sum their reciprocals
(1..=n).map(|i| Ratio::from_integer(i.into()).recip()).sum()
}
19

edits