First, the best time for visibility is calculated:
Best Time=Sunset Time+4/9*(Moonset Time - Sunset Time)
Then at that time the following is calculated:
The Elongation (ARCL) i.e., angle between sun-earth-moon is calculated.
The Arc of Vision (ARCV) i.e., altitude difference between moon and sun is calculated.
The Width of Crescent (WOC) = SD*(1 Cos(ARCL)), Where Moon's SD 0.2725*
Then a factor q is calculated as follows:
q = (ARCV-(11.8371-6.3226*WOC+0.7319*WOC^2-0.1018*WOC^3))/10
IF q>.27 then it is easily visible with bare eyes
IF .27>=q AND q>-.024 then it is visible if perfect conditions
IF -.024>=q AND q>-.212 then optical aid is needed to find moon
IF -.212>=q AND q>-.48 then visible with optical aid only
IF -.48>=q AND q>-.523 then not visible even with telescope Else "Not Visible "