The math.lcm function (that was introduced in 3.9+), has an issue with integers > 9859.
Let’s say I am calculating the LCM of first n natural numbers. Using math.lcm, that reduces to the following snippet (Taking n as 10 for example).
>>> from math import lcm
>>> lcm(*range(1, 10))
2520
However, for integers > 9859, the snippet starts throwing the following error (which is documented in the standard docs over here: Built-in Types — Python 3.13.2 documentation.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Exceeds the limit (4300) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit
My knowledge of C is very limited, however, looking at the PR that introduced this change (https://github.com/python/cpython/pull/18547), I don’t see any integer to string conversion in the algorithm ?
Anyone has any ideas why this error is thrown ?
System Information
Python 3.11.1
OS: Windows 10 Build 10.0.26100
Architecture: 64bit
Processor: Intel64 Family 6 Model 126 Stepping 5, GenuineIntel
Is it possible that it’s the actual printing in the interpreter instead of the lcm call itself?
Try saving the value as an int instead of printing it. The print is implicitly converting to a string to print. (And the print is implicitly done because it’s not assigning to something and it’s in the REPL) .
That’s coming from new “security feature”, added to several minor releases. In your case this definitely coming from repr(). You can workaround this as suggested above, but usually, working with bigints, it does make sense to disable this feature with sys.set_int_max_str_digits(0).
It is not clear if you are only asking out of curiosity but if you do intend to do calculations with such large integers then you may want to use something like gmpy2:
In [3]: gmpy2.lcm(*range(1, 10000))
Out[3]: mpz(57933396702876429686922708791662400986348602979985188253931383511489793001457731823088325981761829221665744176794023407056559491402467891577328326763021299466843118474637852656831938521549472347971073068161679301705472685236926463387338495220571064420250677315000599457941340849496227227628926493771018264821842230370349640102573492881424317306189569467101495834601991270039918780924506495405797923762205360790652073159333382795670426041033566699342449050309786673681670483369155689567554239898879039744147333971988258061042090970476729293484513072443614795766878726325795854855394491290821167148355514749149683707585283381546153703014210442470318180511906691108325146494219343498899382918018246586609827667470329166012110874981104800415741527586280026737848182673635645872230905234515169611121042867043956727839314198728626274066655467846183343599194761590368608472578398169740111485924046986870714883894285841394964627408094161019230662749101230783008668676907211199488107523306410531772045452853957706873238466829988649822157557103503283563398281775464911904789159515900987401574678885942493907604740891878907698622679570965569483682456042918236444719794534411171907606336090534029349351300276141892529795448751826394399153216183270385737795748770508612096374765333578237973395907265484337502903901947799663388329849198045756207969590055686607678195206367273600632909417024224754750428711236917913663419215925830944035539848749163178489614227546656090790164108195741048033614368495827231281392190063051315248070192263400801315095608512139510731469732311313898995746040563433121427776071482655904346538281010668476731132415829844984600414136781404774213539507859790229205890271721600309169926806121871750008163738773911610009508609149665332579632767397078877996926581337419351834754370411008686136818501030862345505385357198060894463821342298717851567836562984344806469613768024764967372979655179066074398198246805104576134474823016488842818077041661676098399378809713894284994865370648616800689225595431967181072865363430005250840767890912164530705704936837915584856606960687347372391339254432119085932175541392954343684716695162629271229789289404752104218596977036941910521266321726821940533986384237994403780618301379099347975260122724194454275088825587044488208965690373706904056926509324696308810974331790119456438147168585552011926921912167450509941646104076818762060881903969616431646384985895944231218505620547093874241169759205450145478746112796898626711966320965057212219958567338851356631739947125096250452942497473309299907612330435197454392788637359253116308685007014249605492659524429134513344137517101872279428202285951652856354827230765931502805341696470148698002737700823078904634554776750169178259216255903968865588749827789888950172452455448248833712309835657561369233157977405579365293671943131412034109901944892819245001657496671822581274180596255340507054499934060282320458240722454209933569735940032859109934686878274110864394924463573852015338428881961843292083566034669814619612606638283615766521897504566616272305253931938372830446073384019299355320864342734019517633662346790422915951954822645137091494126100390104510987373366328615363056042137440808225973600809566845718073791616927784260557845021823094999326904373592319407516660896764388092262510369182153559285446074990941863516247226532653142198551840063631989428776799533286215464660644129411503287306838551341184739976807097763115368031748646043780549055143428297230678053738453010234949008253769355207208167999203353157524666017029803679612131824740794652592875662818479980117505768541194835524231818203552256426752730455115752280837099763237606348192867936457993970866446264015812819179994138642295108872381709181937092290392544335464025324661284746003660247161196698209062164637264114930766444473471083408200329662059064201896721165015687487728300854501780810155844837489798144309942999091774466406270065305461848242329380636274754660519867343112275861821293501112101434868225378041813836808745417606289159904294165941408692922250601127804971962342807927743390030395048263275616935165347620718001157478088456439083590834464409622781693790883289597024043982584220069224170235863458745344365684082114430362867446193601075569803650773018026700003812298460527976219100308016537538008597751565631582745643139434508332515569645426771932483266712323523039014220800000)
I was only asking out of curiosity. I was trying to see how fast math.lcm calculates as the size of n increases. When I substituted n = 10000, that’s when I got that “error” (which was my misunderstanding of why its thrown, rather than an actual fault in the algorithm).