%mathpiper,title="FactorsBinomials"

//Retract("FactorsBinomials",*);

10 # FactorsBinomials( _x + y_IsFreeOf(x) ) <-- {x+y,1};

10 # FactorsBinomials( _x - y_IsFreeOf(x) ) <-- {x-y,1};

10 # FactorsBinomials( c_IsConstant * _x + y_IsFreeOf(x) ) <-- {c*x+y,1};

10 # FactorsBinomials( c_IsConstant * _x - y_IsFreeOf(x) ) <-- {c*x-y,1};

10 # FactorsBinomials( _x^m_IsOdd + _y ) <--
[
    If(InVerboseMode(),Tell("FactorsBinomialssum",{x,m,y}));
    Local(nn,qq);
    nn := (m-1)/2;
    qq := (y^(1/m));
    If(InVerboseMode(),Tell("  FBinsum1",{nn,qq}));
    r := {{x+qq,1},{Sum(k,0,m-1,(-1)^k*qq^k*x^(m-1-k)),1}};
];


12 # FactorsBinomials( c_IsConstant * _x^m_IsOdd + _y ) <--
[
    If(InVerboseMode(),Tell("FactorsBinomialssum",{c,x,m,y}));
    Local(nn,qq);
    nn := (m-1)/2;
    qq := ((y/c)^(1/m));
    If(InVerboseMode(),Tell("  FBinsum.1b",{nn,qq}));
    If( c=1,
          r := {{x+qq,1},{Sum(k,0,m-1,(-1)^k*qq^k*x^(m-1-k)),1}},
          r := {{c,1},{x+qq,1},{Sum(k,0,m-1,(-1)^k*qq^k*x^(m-1-k)),1}}
    );  
];


10 # FactorsBinomials( _x^m_IsInteger - _y ) <--
[
    If(InVerboseMode(),Tell("FactorsBinomialsdif",{x,m,y}));
    Local(pp,qq,r,L);
    pp := m-1;
    qq := (y^(1/m));
    If(IsNumber(y),qq:=GuessRational(N(qq)));
    If(InVerboseMode(),Tell("  FBindif.1",{pp,qq}));
    
    if      (m = 2)
        [
            L := FunctionToList(y);
            If(And(L[1]=ToAtom("^"),L[3]=2),qq:=L[2]);
            r := {{x+qq,1},{x-qq,1}};
        ]
    else if (m = 4)
        [r := {{x+qq,1},{x-qq,1},{x^2+qq^2,1}};]
    else if (m = 6)
        [r := {{x+qq,1},{x-qq,1},{x^2+x*qq+qq^2,1},{x^2-x*qq+qq^2,1}};]
    else
        [r := {{x-qq,1},{Sum(k,0,pp,qq^k*x^(pp-k)),1}};];
    r;
];


12 # xFactorsBinomials( c_IsConstant * _x^m_IsInteger - _y ) <--
[
    If(InVerboseMode(),Tell("FactorsBinomialsdif",{c,x,m,y}));
    Local(aa,bb,c0,r);
    aa := c^(1/m);
    bb := ((y)^(1/m));
    If(IsNumber(y),bb:=GuessRational(N(bb)));
    If(InVerboseMode(),Tell("  FBindif.1b",{aa,bb}));
    r  := FactorsBinomials( (aa*x)^m - bb^m );
];

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output


