On Jul 26, 3:32 am, "Jano Svitok" <jan.svi... / gmail.com> wrote:
> For this I meant n.to_f/d that was there three times. I'm not sure if
> ruby reuses the value or computes it three times.

In other words, if the exact same calculation is needed in more than
one place, store it instead of recalculating? Gotta remember that
one...

> require 'mathn'
> MAX_D = 1000
> red_prop_fract = 0
> for n in 2...(MAX_D/2).ceil do
>  lower = n*2+1
>  upper = [n*3-1,MAX_D].min
>  for d in lower..upper do
>    if n.gcd2(d) == 1
>      red_prop_fract += 1
>    end
>  end
> end

...I can't believe I didn't even think to narrow the loop down to d's
where d is less than three but more than two times larger than n.
*smacks head*

> Inlining the function also helps a bit.
> This is my version:
> t = Time.now
> MAX_D = 1000
> red_prop_fract = 0
> for n in 2...(MAX_D/2).ceil do
>  lower = n*2+1
>  upper = [n*3-1,MAX_D].min
>  for d in lower..upper do
>    next if (d|n) & 1 == 0
>    min = n
>    max = d
>    while min > 1
>      tmp = min
>      min = max % min
>      max = tmp
>    end
>    if min == 1
>      red_prop_fract += 1
>    end
>  end
> end
> p red_prop_fract
> p Time.now-t

That's a good-looking solution.

I'm still getting much longer times than the two of you have cited,
but I'm chalking that up to the fact that my work machine is a low-
cost, out-of-the-box Dell and has worse specs.

Thanks again!
Andrew