sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
12.64k stars 4.36k forks source link

Cannot find roots of big polynomial equation #15355

Open lvenuto opened 5 years ago

lvenuto commented 5 years ago

I'm trying to use the Sympy solveset to find only Integer solutions of a big polynomial like I'm doing in Sagemath

from sympy import solveset, Symbol
from sympy.abc import x

eq = -14463275119469747701761787627192388*x**10 - 90192166282097116621887010901765928821458332584681741818535680729946342*x**9 + 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897*x**8 + 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*x**7 + 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*x**6 - 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057*x**5 - 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*x**4 + 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*x**3 - 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365*x**2 + 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*x + 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632

x = Symbol('x', Reals = True)
solveset(eq,x)

Result is EmptySet()

But with Sagemath I get:

[(37598862050464270193369012399545432, 1)]

How do I get a similar behaviour as Sagemath roots() method? With simpler polynomials Sympy works just fine.

llucifer97 commented 5 years ago

hi @lvenuto i taking up this issue.

jksuom commented 5 years ago

Note that x = Symbol('x', Reals = True) will redefine the symbol x. It is different from the symbol x of eq that was imported from sympy.abc. The result of solveset(eq, x) is empty since eq does not depend on the new x.

lvenuto commented 5 years ago

Note that x = Symbol('x', Reals = True) will redefine the symbol x. It is different from the symbol x of eq that was imported from sympy.abc. The result of solveset(eq, x) is empty since eq does not depend on the new x.

Since we know 2 of the solutions are integers, I tried with:

solveset(eq,x,domain=S.Integers)

Which takes half an hour and yields:

Intersection(Integers, {CRootOf(14463275119469747701761787627192388*_r**10 + 90192166282097116621887010901765928821458332584681741818535680729946342*_r**9 - 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897*_r**8 - 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*_r**7 - 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*_r**6 + 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057*_r**5 + 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*_r**4 - 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*_r**3 + 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365*_r**2 - 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*_r - 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632, 0), CRootOf(14463275119469747701761787627192388*_r**10 + 90192166282097116621887010901765928821458332584681741818535680729946342*_r**9 - 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897*_r**8 - 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*_r**7 - 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*_r**6 + 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057*_r**5 + 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*_r**4 - 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*_r**3 + 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365*_r**2 - 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*_r - 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632, 1), CRootOf(14463275119469747701761787627192388*_r**10 + 90192166282097116621887010901765928821458332584681741818535680729946342*_r**9 - 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897*_r**8 - 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*_r**7 - 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*_r**6 + 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057*_r**5 + 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*_r**4 - 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*_r**3 + 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365*_r**2 - 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*_r - 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632, 2), CRootOf(14463275119469747701761787627192388*_r**10 + 90192166282097116621887010901765928821458332584681741818535680729946342*_r**9 - 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897*_r**8 - 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*_r**7 - 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*_r**6 + 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057*_r**5 + 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*_r**4 - 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*_r**3 + 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365*_r**2 - 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*_r - 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632, 3)})
jksuom commented 5 years ago

What do you get when you substitute the integers to eq (eq.subs(x, ...))?

lvenuto commented 5 years ago

What do you get when you substitute the integers to eq (eq.subs(x, ...))?

>>> eq.subs(x,37598862050464270193369012399545432)
59998831204039462476367477552203614273058266729213294534546302274324454207015232537458642703690180355254785648687111183574648143042735412092442436745296449180410171669481969810512768594535595156687225877562187810063933776495062806042031543510745261821546916693953063729413790214450297140121977609613655927416826099608215664946077046722992292556198478409458154511997315084066685976576
>>> eq.subs(x,1)
3214703246576214010537607278931775436263385693843783373639760355860627286107846517913157224233861078913729464320004711290819624272291985413577120091533793129999877982482058482652729468097016599891796363111567317238931767193573155401878292903433227123119689051946334911461054762146945003440637145688717008418538856874721926222005543387286499020690920405171341441295744238047567602292

Not 0

jksuom commented 5 years ago

It seems that eq has one positive root, but that is not an integer:

>>> count_roots(eq, 0)
1
>>> nsolve(eq, (0, 10**40), solver='bisect', prec=40, verify=False)
57492866286035965257845680043236867.85689
lvenuto commented 5 years ago

I'm sorry, 1 is not a solution, is the multiplicity. The only solution sagemath finds is the integer solution:

37598862050464270193369012399545432

Anyway Sagemath thinks his solution is right :

sage: new_pol.substitute(x==37598862050464270193369012399545432)
0

I'm sure Sagemath sol is correct, because it's used to factorize a big number into 2 primes with the coppersmith attack (https://github.com/mimoo/RSA-and-LLL-attacks/blob/master/coppersmith.sage) The attack is based upon finding an integer solution. When i replaced the code with the _findroots method of sympy things stopped working because sympy doesn't believe there is an integer solution!

jksuom commented 5 years ago

It looks like eq and new_pol are different.

lvenuto commented 5 years ago

You are right! I'm sorry, eq and new_pol are:

eq = -14463275119469747701761787627192388*x**10 - 90192166282097116621887010901765928821458332584681741818535680729946342*x**9 + 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897*x**8 + 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*x**7 - 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*x**6 - 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057*x**5 - 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*x**4 + 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*x**3 - 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365*x**2 + 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*x + 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632

Now in Sympy, if you use subs:

>>> eq.subs(x,37598862050464270193369012399545432)
0

Like we expect, so at least that is the correct solution. I checked again with nsolve but it finds an approximation. I'm waiting on solveset termination, but Sagemath find_roots() takes a couple of seconds, so I don't have any high hopes.

Edit:

Solveset finds the correct solution, but takes 5 minutes while Sage is alot faster... Do you know how I can make sympy find it faster?

>>> solveset(eq,x,domain=S.Integers)
Union({37598862050464270193369012399545432}, Intersection(Integers, {CRootOf(14463275119469747701761787627192388*_r**9 + 90735968968111971802410322342402411774544424083913938818615174340517958*_r**8 + 2540535349398881772634563441203463735298686034130541177028685787084921156452245214403709517969853160953959*_r**7 + 27927344786271935577196428551986310496696682770157419247041797692383266660757081101548575672793647889142068886759221269939033003912064418244*_r**6 + 11668571039185376638090475184550597826140983147514577862232645239733839902363007406971500464507767082348770660824853448106047100346332354420512876427513786643913702075159374745*_r**5 + 547793134768694814854315404988520592213446472756869060799047799287133186497427247268712498010724800674261730446899931678791971954161787130366284275719906960764285339151627702822770719424210569978329499744100897*_r**4 + 22121629238441827603621449975588767541169842260460029207845742504524189885312127698884286009723616371973495729836257384226000952526156041235316225113356404872123325806543334437545679325408190377855729595135166686056956081190319363449890224395209*_r**3 + 676007072387497212435491717436623784451926247447569585964394934661439420588807287082746009528693664187065250766977000420931574505902030089782877050862610383448589784538564373356734415367921987537150977271755807317309455735941258819260201693302065436195215639869590995971111371064*_r**2 + 26083315377197446046728148337776132540306325644401365213135014813572427773443381890229662074114667844300068128679118522710715280331253876766506048133646409157162522954012057945749735600704053358100506315319512475493142406014421178416617753456864295201449290804135364593937358738952566416861533481199503494184120013*_r + 85500014395688842458493168045289317594595779015780770406546319857581885637290610987650388603373118203906633072764851490376133178169200381945449987525535091868679348040548455295992679942011625211034732334440392421018528399022879179643509402415677495358786246825585637695535541564750826846815072204886207630987930448458156885870600822880862894227601, 0), CRootOf(14463275119469747701761787627192388*_r**9 + 90735968968111971802410322342402411774544424083913938818615174340517958*_r**8 + 2540535349398881772634563441203463735298686034130541177028685787084921156452245214403709517969853160953959*_r**7 + 27927344786271935577196428551986310496696682770157419247041797692383266660757081101548575672793647889142068886759221269939033003912064418244*_r**6 + 11668571039185376638090475184550597826140983147514577862232645239733839902363007406971500464507767082348770660824853448106047100346332354420512876427513786643913702075159374745*_r**5 + 547793134768694814854315404988520592213446472756869060799047799287133186497427247268712498010724800674261730446899931678791971954161787130366284275719906960764285339151627702822770719424210569978329499744100897*_r**4 + 22121629238441827603621449975588767541169842260460029207845742504524189885312127698884286009723616371973495729836257384226000952526156041235316225113356404872123325806543334437545679325408190377855729595135166686056956081190319363449890224395209*_r**3 + 676007072387497212435491717436623784451926247447569585964394934661439420588807287082746009528693664187065250766977000420931574505902030089782877050862610383448589784538564373356734415367921987537150977271755807317309455735941258819260201693302065436195215639869590995971111371064*_r**2 + 26083315377197446046728148337776132540306325644401365213135014813572427773443381890229662074114667844300068128679118522710715280331253876766506048133646409157162522954012057945749735600704053358100506315319512475493142406014421178416617753456864295201449290804135364593937358738952566416861533481199503494184120013*_r + 85500014395688842458493168045289317594595779015780770406546319857581885637290610987650388603373118203906633072764851490376133178169200381945449987525535091868679348040548455295992679942011625211034732334440392421018528399022879179643509402415677495358786246825585637695535541564750826846815072204886207630987930448458156885870600822880862894227601, 1), CRootOf(14463275119469747701761787627192388*_r**9 + 90735968968111971802410322342402411774544424083913938818615174340517958*_r**8 + 2540535349398881772634563441203463735298686034130541177028685787084921156452245214403709517969853160953959*_r**7 + 27927344786271935577196428551986310496696682770157419247041797692383266660757081101548575672793647889142068886759221269939033003912064418244*_r**6 + 11668571039185376638090475184550597826140983147514577862232645239733839902363007406971500464507767082348770660824853448106047100346332354420512876427513786643913702075159374745*_r**5 + 547793134768694814854315404988520592213446472756869060799047799287133186497427247268712498010724800674261730446899931678791971954161787130366284275719906960764285339151627702822770719424210569978329499744100897*_r**4 + 22121629238441827603621449975588767541169842260460029207845742504524189885312127698884286009723616371973495729836257384226000952526156041235316225113356404872123325806543334437545679325408190377855729595135166686056956081190319363449890224395209*_r**3 + 676007072387497212435491717436623784451926247447569585964394934661439420588807287082746009528693664187065250766977000420931574505902030089782877050862610383448589784538564373356734415367921987537150977271755807317309455735941258819260201693302065436195215639869590995971111371064*_r**2 + 26083315377197446046728148337776132540306325644401365213135014813572427773443381890229662074114667844300068128679118522710715280331253876766506048133646409157162522954012057945749735600704053358100506315319512475493142406014421178416617753456864295201449290804135364593937358738952566416861533481199503494184120013*_r + 85500014395688842458493168045289317594595779015780770406546319857581885637290610987650388603373118203906633072764851490376133178169200381945449987525535091868679348040548455295992679942011625211034732334440392421018528399022879179643509402415677495358786246825585637695535541564750826846815072204886207630987930448458156885870600822880862894227601, 2)}))
jksuom commented 5 years ago

nsolve only gives floating point values. The exact roots can be found from linear factors, if they exist:

>>> factors = factor(eq).args
>>> factors[0]
-1
>>> factors[1]
x - 37598862050464270193369012399545432
>>> degree(factors[2])
9
lvenuto commented 5 years ago

Thanks jksuom, that works and is very fast. Do you know if I can use solveset/solve and get a similar behaviour? Maybe it's faster as I don't need the factorization but just the roots.

jksuom commented 5 years ago

I believe that factorization is the best way for finding integer (or rational) solutions of polynomials with integer coefficients.

lvenuto commented 5 years ago

While factors works in that example, I don't think it fits my use case. I need a list of integer roots, if they exists, or empty if there are no integer solutions. I'd rather have solveset work as fast as sage _findroot method. The problem with factor is:

>>> factors = factor(x**2-43*x+2).args
>>> factors
(2, x**2, -43*x)

I understand Sympy is trying to tell me that there are no integer solutions, but how can I know that?

jksuom commented 5 years ago

factor(eq).args did work above as factor(eq) was of type Mul, but it is not a universal solution. factor(x**2-43*x+2) is of type Add as the polynomial is irreducible and its arguments are not factors but the terms of the sum. In order to get the arguments of a product, one can use Mul.make_args(factor(eq)) (or factor_list).

lvenuto commented 5 years ago

Wow, works perfectly. I can cycle through all the factors. Do you know of a better way to print only the factors I'm interested in? This works but it's crude:

>>> for elem in Mul.make_args(factor(x**2-1)):
            if degree(elem) == 1: #to avoid the irreducible part
                print solve(elem) #Is there a faster way of extracting the constant term?

[-1]
[1]
jksuom commented 5 years ago

Rational numbers can also appear as roots of linear factors. If p = a*x + b is a linear factor, then the root x = - b/a is an integer only if a == 1. solve is a relatively expensive way of finding the root. It is probably more efficient to directly compute the coefficient list by Poly(p, x).coeffs(), for example.

lvenuto commented 5 years ago

By default the symbol x seems to be considered in the ZZ field. Mul.make_args(factor(2*x-1)) which is an example of the counterexample, returns: (2*x - 1,) which makes sense if we are in ZZ, as there are no integer solutions. So I shouldn't worry about that I think.

I was thinking something along the lines of:

for elem in Mul.make_args(factor(2*x-1)):
    if degree(elem) == 1:
        Poly(elem, x,domain='ZZ').coeffs()[1]
-1

But of course this doesn't work because the result from Mul.make_args(factor(2*x-1)) is 2*x-1 because the polynomial is irreducible in ZZ. So I can't use degree()...

Even if I use solve

>>> solve(elem, x, Integer=True)
[1/2]

I get 1/2, even if I specify Integer=True, then again as you said solve is probably slower, so I'd prefer to use your method by creating a Poly, If I can get it working 100%.

alatiki commented 5 years ago

@lvenuto hey im taking this issue

alatiki commented 5 years ago

@lvenuto I have found a way to find the degrees using count_root, factors, and degree libraries. This is the solution I get, which provides the right degree but not the correct eq.

This is what my code looks like for your reference: https://paste.ofcode.org/Ryb7bJxuYutFTRfsaJGHX

alatiki commented 5 years ago

Here is the code that finds the roots of big polynomial equation:

, , , from sympy import solveset, Symbol, degree from sympy.abc import x

eq = -14463275119469747701761787627192388*x10 - 90192166282097116621887010901765928821458332584681741818535680729946342*x*9 + 871033830848367109976102179672875863060447884429535325269250414873913705111183364495829062385205471913897x8 + 67593893350104665454276217655772913203825188922801812844551135795545650452595155749663558701490993817855525866322316553511740484511016347044*x7 + 10618534655130585564772773989878359915269295618279215937337433136248515093402435863316661855786385487867331249361899919319811801871124417584048708556608058117413179148031713337*x*6 - 109068141940321324528756200332674286359963467493717844800304054600388429690813370482178278617039850027615719143100912097423830176377170572528774942830992040976522146410384412052826595127101116661078708403186057x5 - 1525230732082288576758474805069477392183860044730925598679997320216013277844117271104498227571861632574972087025567297512988692654492710650359010067066565704124692166938418739516042697199910875909179858407014395663063290811195468645443880942705*x4 + 155741013680194033667122246424452018241881364975547237123316996891598563409256934185235167144489400803774489123982707601484623954151171476696142495374777020634565763652394975499471404005763636986279935717234491472188209450035725023963307632044556309190606529871670926097607264224*x*3 - 666218717361724277390387436463154617772122209201111752973907775718705500215417181881576322456712355402029771540911186457115492382621406510675754880417285862178016513308561309414865865012299437868001670983774110086258311271498070677026711683343916427562997250939163067452052896014871919535705688000482454305940365x2 + 895202962290311352938632728029353752353401555386834773557643334284240101867602111808605594456859791248288077381327175567360684023121068087326077320720624552937301654292777478213535259867196398550404123015957057605573134709127721123927644134501833560736808312552660032412933924257440863117297909519576758111323515306319955651815443949660011239703015*x + 3214703246576214010537607278931774541060423403532430435007032326506875598925008492802661056977963257828089627826421909763311019073852003814283303548646848685273469381947252124189694709525935738374232943827114309607499243918330414162043768634407808570444611012904209265386225895833890833282009997391510529631765965598845032661861016495357169566029077373148451758011558107149147868632 x = Symbol('x', Reals = True) solveset(eq,x)

eq.subs(x,1) eq.subs(x,37598862050464270193369012399545432) from sympy import count_roots count_roots(eq, 0) from sympy import factor factors = factor(eq).args factors[1] degree(factors[1]) , , ,