LM386 with Gain of 40
From the circuit diagram and the paragraph Gain control I derive the following two formula's:


  With pins 1 and 8 open the 1.35 kΩ resistor sets the gain at 20 (26 dB).


\$G = \dfrac{x}{150Ω + 1350Ω} = 20 \Rightarrow x = 20 \cdot (150Ω+1350Ω) = \boxed{30000}\$


  If a capacitor is put from pin 1 to 8, bypassing the 1.35 kΩ resistor, the
  gain will go up to 200 (46 dB).


For AC the capacitor can be neglected in our formula:

\$G = \dfrac{x}{150Ω} = 200 \Rightarrow x = 200 × 150Ω = \boxed{30000}\$

The formula derived from the circuit appears to be correct because for both situations we get the same result.

Now for a Gain of 40:

\$G = \dfrac{30000}{R_p} = 40 \Rightarrow R_p = \dfrac{30000}{40} = \boxed{750Ω}\$

Where Rp is the internal 1350Ω resistor in parallel with the external resistor you need to apply. Again we neglected the series capacitor:

\$R_p = 1350Ω||R_x\$

\$\dfrac{1}{R_p} = \dfrac{1}{R_x} + \dfrac{1}{R_i}\$

\$\dfrac{1}{R_x} = \dfrac{1}{R_p} - \dfrac{1}{R_i}\$

\$\dfrac{1}{R_x} = \dfrac{1}{750Ω} - \dfrac{1}{1350}\$

\$R_x \approx \boxed{ 1688Ω }\$

The nearest E12 value would be 1k5 or 1k8.

Don't forget to include the 10μF series capacitor when you actually build the circuit. Notice that the capacitor makes the circuit's response frequency dependent. A large cap (like the one proposed) will have a fairly low cut off frequency at the low frequency end.