use num::rational::Ratio;
use num::{BigInt, FromPrimitive};

fn main() {
    for n in 1..=20 {
        println!("Harmonic number {} = {}", n, harmonic_number(n.into()));
    }

    println!("Harmonic number 100 = {}", harmonic_number(100.into()));

    let max = 5;
    let mut target = 1;
    let mut i = 1;
    while target <= max {
        if harmonic_number(i.into()) > Ratio::from_integer(FromPrimitive::from_u64(target).unwrap())
        {
            println!("Position of first term > {} is {}", target, i);
            target += 1;
        }
        i += 1;
    }
}

fn harmonic_number(n: BigInt) -> Ratio<BigInt> {
    let mut result: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u8(0).unwrap());
    let mut i: BigInt = FromPrimitive::from_u8(1).unwrap();
    let one: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u8(1).unwrap());

    while i <= n {
        result = &result + &one / &i;
        i += 1;
    }

    result
}