function [n] = returnnforgsigmag(sigmagsq)  % Remember you have to specify Pt as well, and make sure sigmagsq > Pt/Pshannon

global Pt;
global log10pe;
global sigmagsq2

sigmagsq2 = sigmagsq;

Pt;
%log10pe

R = 1/3;

sigmaPsq = 1;

Pshannon = 2^(2*R)-1;

Cg = 0.5*log2(  1  +  Pt/sigmagsq  );

deltag = 1 - Cg/R;

if deltag<0
    
    disp('delta(G) negative');
    deltag
end

hbinvdeltag = hbinv(deltag);

epsilon =  (3 + 4*log(2) + 4*log(1/hbinvdeltag) )*sigmagsq;  


c = log10pe*log(10) - log(hbinvdeltag) + 1;

%c = log10pe*log2(10) - log2(deltag/2) - log2(hbinv(deltag/2)/2) +1 ;
%            c = - log2(1/pe(t))  - log2(hbinv(deltag)) +1 ;


    div = 0.5*( sigmagsq  -1  - log(sigmagsq)   );

    a = div;

    b = 0.5 * (   1 - 1/sigmagsq  )   * epsilon;

    if (b^2 - 4*a*c >0)
        sqrtn = ( (- b + sqrt(b^2 - 4*a*c) )/ ( 2*a)    );

    else
        sqrtn = 1;
    end

ntemp =  max(sqrtn^2,1);

ntemp3 = min(28, (2/(sigmagsq-1))^(5/9) );

n = -max(ntemp,ntemp3);