Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168695
Image: ubuntu2004
# A Brief Introduction to Sage
# In the tried and true methodology of computer science, # we always begin learning a new language in the same way: print("Hello, world!")
Hello, world!
# Now, not to tax our engine too much, let's try some arithmetic: 2+2
4
# So far so good. Well, let's step it up a bit. Can you write # out the factorial of 20? No? How about the factorial of 1000? factorial(1000)
402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
# Or, from the ridiculous to the sublime, # how about some pie. That is, some Pi ... 100 decimal places worth numerical_approx(pi, prec=335)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
# Expanding polynomials is always a challenge. Let's try (1+x)^3 expand((1+x)^3)
x^3 + 3*x^2 + 3*x + 1
# OK. Suppose the exponent is 100 expand((1+x)^100)
x^100 + 100*x^99 + 4950*x^98 + 161700*x^97 + 3921225*x^96 + 75287520*x^95 + 1192052400*x^94 + 16007560800*x^93 + 186087894300*x^92 + 1902231808400*x^91 + 17310309456440*x^90 + 141629804643600*x^89 + 1050421051106700*x^88 + 7110542499799200*x^87 + 44186942677323600*x^86 + 253338471349988640*x^85 + 1345860629046814650*x^84 + 6650134872937201800*x^83 + 30664510802988208300*x^82 + 132341572939212267400*x^81 + 535983370403809682970*x^80 + 2041841411062132125600*x^79 + 7332066885177656269200*x^78 + 24865270306254660391200*x^77 + 79776075565900368755100*x^76 + 242519269720337121015504*x^75 + 699574816500972464467800*x^74 + 1917353200780443050763600*x^73 + 4998813702034726525205100*x^72 + 12410847811948286545336800*x^71 + 29372339821610944823963760*x^70 + 66324638306863423796047200*x^69 + 143012501349174257560226775*x^68 + 294692427022540894366527900*x^67 + 580717429720889409486981450*x^66 + 1095067153187962886461165020*x^65 + 1977204582144932989443770175*x^64 + 3420029547493938143902737600*x^63 + 5670048986634686922786117600*x^62 + 9013924030034630492634340800*x^61 + 13746234145802811501267369720*x^60 + 20116440213369968050635175200*x^59 + 28258808871162574166368460400*x^58 + 38116532895986727945334202400*x^57 + 49378235797073715747364762200*x^56 + 61448471214136179596720592960*x^55 + 73470998190814997343905056800*x^54 + 84413487283064039501507937600*x^53 + 93206558875049876949581681100*x^52 + 98913082887808032681188722800*x^51 + 100891344545564193334812497256*x^50 + 98913082887808032681188722800*x^49 + 93206558875049876949581681100*x^48 + 84413487283064039501507937600*x^47 + 73470998190814997343905056800*x^46 + 61448471214136179596720592960*x^45 + 49378235797073715747364762200*x^44 + 38116532895986727945334202400*x^43 + 28258808871162574166368460400*x^42 + 20116440213369968050635175200*x^41 + 13746234145802811501267369720*x^40 + 9013924030034630492634340800*x^39 + 5670048986634686922786117600*x^38 + 3420029547493938143902737600*x^37 + 1977204582144932989443770175*x^36 + 1095067153187962886461165020*x^35 + 580717429720889409486981450*x^34 + 294692427022540894366527900*x^33 + 143012501349174257560226775*x^32 + 66324638306863423796047200*x^31 + 29372339821610944823963760*x^30 + 12410847811948286545336800*x^29 + 4998813702034726525205100*x^28 + 1917353200780443050763600*x^27 + 699574816500972464467800*x^26 + 242519269720337121015504*x^25 + 79776075565900368755100*x^24 + 24865270306254660391200*x^23 + 7332066885177656269200*x^22 + 2041841411062132125600*x^21 + 535983370403809682970*x^20 + 132341572939212267400*x^19 + 30664510802988208300*x^18 + 6650134872937201800*x^17 + 1345860629046814650*x^16 + 253338471349988640*x^15 + 44186942677323600*x^14 + 7110542499799200*x^13 + 1050421051106700*x^12 + 141629804643600*x^11 + 17310309456440*x^10 + 1902231808400*x^9 + 186087894300*x^8 + 16007560800*x^7 + 1192052400*x^6 + 75287520*x^5 + 3921225*x^4 + 161700*x^3 + 4950*x^2 + 100*x + 1
# I like that one just for the pretty picture. Even fancier if the # exponent is 300 expand((1+x)^300)
x^300 + 300*x^299 + 44850*x^298 + 4455100*x^297 + 330791175*x^296 + 19582837560*x^295 + 962822846700*x^294 + 40438559561400*x^293 + 1481062243936275*x^292 + 48052241692154700*x^291 + 1398320233241701770*x^290 + 36864806149099410300*x^289 + 887827414757477464725*x^288 + 19668791957704116141600*x^287 + 403210235132934380902800*x^286 + 7687875149867948862546720*x^285 + 136940276107022839114113450*x^284 + 2287708142023205076965189400*x^283 + 35967855788475946487841588900*x^282 + 533838701702642995240596214200*x^281 + 7500433758922134083130376809510*x^280 + 100005783452295121108405024126800*x^279 + 1268255162872288126783863715062600*x^278 + 15329345012108526054170178816843600*x^277 + 176926190348085904875214147177736550*x^276 + 1953265141442868389822364184842211512*x^275 + 20659535149876492584659621185831083300*x^274 + 209656023372820702525805044626582104600*x^273 + 2044146227885001849626599185109175519850*x^272 + 19172681861542086313739137184472266944800*x^271 + 173193226149263513034110205899732811401360*x^270 + 1508457130977456403845475986868640615431200*x^269 + 12680467757279242894826032514614510173468525*x^268 + 102980768453055669570102324664142082620895900*x^267 + 808701916969584228682862373097821648817035450*x^266 + 6146134568968840137989754035543444531009469420*x^265 + 45242379466020628793535689428305911131041927675*x^264 + 322810491325120162202524919164128663205272132600*x^263 + 2234188400487015859454317203688574695341751865100*x^262 + 15009163100707645004539259163241194107167666375800*x^261 + 97934789232117383654618666040148791549269023102095*x^260 + 621049882935378530492703735864358190312437707476700*x^259 + 3829807611434834271371673037830208840260032529439650*x^258 + 22978845668609005628230038226981253041560195176637900*x^257 + 134217348564375328328525450553049591629112958190816825*x^256 + 763547582944001867824500340924015454601175939929980160*x^255 + 4232709427189575571635817107296172628767388362655324800*x^254 + 22874642436301110536074415856451656334189715832222393600*x^253 + 120568427841337103450558900243380605261458293865672199600*x^252 + 620066200326876532031445772680243112773214082737742740800*x^251 + 3112732325640920190797857778854820426121534695343468558816*x^250 + 15258491792357451915675773425758923657458503408546414504000*x^249 + 73064701082634721673139761211807153667445525937078023298000*x^248 + 341887657896102093866767184538267436028801706271610373168000*x^247 + 1563819472228466984909101751499112161094704100909032632824000*x^246 + 6994538003058234150684346015796028938714494705884036866812800*x^245 + 30601103763379774409244013819107626606875914338242661292306000*x^244 + 130994198566046753611500690734425629685574089447915953602152000*x^243 + 548820521578437260820597721525266000234387995445578909057292000*x^242 + 2251094342745454527433638112018887661978337201658137220201096000*x^241 + 9041895610027575685191779749942532108946321093326851167807735600*x^240 + 35574671252567510892557821966987011576182246924564660332358304000*x^239 + 137134619828445727472924507259837028495283177660821835797316688000*x^238 + 518064119351906081564381471870495440982180893385326935234307488000*x^237 + 1918456191975027208293100138020428429887138620817538807039544916500*x^236 + 6965471712401637248571871270351093991590226377122140899405116927600*x^235 + 24801300794157344748702874977765258909450048463995501687275795121000*x^234 + 86619468445265950316365264847717471415094199113058916340634866542000*x^233 + 296799060996278917995780981022326041760543358725628345696587116239500*x^232 + 997933074654155202536538950683762923020957380062982263791423347356000*x^231 + 3293179146358712168370578537256417645969159354207841470511697046274800*x^230 + 10668045122007095756693423430548958571449389457293007580530849586524000*x^229 + 33930310179717012892816582855495993234193197023890260221410618823805500*x^228 + 105974119465417519720029875219905293936932177006123004527145494408598000*x^227 + 325082771873645634816848401012412185455183840275539486860297665280429000*x^226 + 979582752579252179581436515050735385504953972030292320405696964711692720*x^225 + 2900080517504365005339779156400203443929140048773891738043181803422774500*x^224 + 8436597869103607288261175727709682745975680141887685056125619791775344000*x^223 + 24120016984744928529259515221528964773750982969755817532256579661101304000*x^222 + 67780300893840178905007751635182660503452129358047993571657730186892272000*x^221 + 187243081219233494225083913892192099640786507351607582241704479641289901400*x^220 + 508561455163350231228622976003484715073741131078440346829320808902268868000*x^219 + 1358231203424069519988639411521501860989625703733883365312454355482888806000*x^218 + 3567402437909001871777390261586595249346245824264898477567651198738189876000*x^217 + 9215789631264921502091591509098704394144468379350987733716432263406990513000*x^216 + 23418947768861447581785691364297884107473001999291921770385286692893058244800*x^215 + 58547369422153618954464228410744710268682504998229804425963216732232645612000*x^214 + 144013069613113499497187872182751356293081104248519289047771590582733174264000*x^213 + 348577088949922447646602463351432260118480400056075097354265327205933705889000*x^212 + 830318459071725380911008114949479091518178031594246299315778082782673546612000*x^211 + 1946635498490378393024696802826000981225950718515399657284768616301601314834800*x^210 + 4492235765747027060826223391136925341290655504266306901426389114542156880388000*x^209 + 10205187772186181040355224877691493438366815221648458069544731792818595521751000*x^208 + 22824505985104576950471900801718608980433307162396551381347357128024385683056000*x^207 + 50262475945921781156890249637827149563294623219320065275945775803202636557368000*x^206 + 108990210998525125455993593951498871684617814559788773124682419110102559271766400*x^205 + 232739513069767194984152987083929882243194291507882275943332249141364840111584500*x^204 + 489472790373531008007909374898161814202181808944412209200410090977715746214054000*x^203 + 1013907922916599945159240848003335186561662318527711004772278045596696902871969000*x^202 + 2068781822516698878001683346431047552378341296389874979434345103136694690708462000*x^201 + 4158251463258564744783383526326405580280466005743648708663033657304756328324008620*x^200 + 8234161313383296524323531735299813030258348526225046947847591400603477877869324000*x^199 + 16064687268267411846474341326712380323739327026654748457075202830589138212705838000*x^198 + 30881631836086869374775918278534478680586279138617865966028059810258731709861708000*x^197 + 58496937227972242950296691354531656731495163368343457647187767140586251411949581500*x^196 + 109194282825548186840553823861792425898790971620907787608083831995761002635639218800*x^195 + 200876275009263173904792411821221915568530561000726590411097615463899957678770261000*x^194 + 364205582727075287266632970965579921684999334898513631212644274766323287754032062000*x^193 + 650848865428940096689445957373675230418563626253825285407780972499077727190075814500*x^192 + 1146449377636298151966730493722437103122607488447105089892605015778191959821050976000*x^191 + 1990653010259390427505868402736231697240163911758155201540795981942133312052915785600*x^190 + 3407424071615172803838873842521477679960640930036481876511272401522570534144630624000*x^189 + 5750028120850604106478099609254993584933581569436563166612772177569337776369064178000*x^188 + 9566418466547907716972413509202998176703657832336936949762842206929517716437027128000*x^187 + 15692282923197006518191590580885619816171789602166729908821504321893156254155474324000*x^186 + 25380561945344723585944659548215002485286546660895754461224346120627191854547114993600*x^185 + 40477620343868740201722086348446340170500095967807884270056069244103711147338071326000*x^184 + 63657112335656822197580033231744671721128356051937185518720655905257118385557308752000*x^183 + 98722470825637275103026661706858262075987196250038177541744746022559768343703283912000*x^182 + 150987308321562891334040776728136165527980417794176036240315493916856116290369728336000*x^181 + 227739190051690694428844838231605383004703796839548854662475869991257975404641006906800*x^180 + 338785571977721694191670007286685693726005648191064411894592203292780459279631249944000*x^179 + 497070634295181830002532223805874911286516483821315817451901675323013952549622899508000*x^178 + 719337991093840371873583218190615725276422228619465166719012180548751898811649399288000*x^177 + 1026796971158143111464711529191443414305860761819720439590848031912331339432757610274000*x^176 + 1445730135390665500942313833101552327342651952642166378943914028932562525921322715265792*x^175 + 2007958521375924306864324768196600454642572156447453304088769484628559063779614882313600*x^174 + 2751061281255203381058208737529200622896122482061865156783038506498970685808291256083200*x^173 + 3718231262946485819711485246816810216883040542161739625964575481440015067537768650799950*x^172 + 4957641683928647759615313662422413622510720722882319501286100641920020090050358201066600*x^171 + 6521205599629221283801681817494097918841024950868281805537870844371718733835471172172220*x^170 + 8462633220892882582032716862396920963381482760668762648407923996512917440855191597475400*x^169 + 10834734957052251184572190528371815475844474140553188542279842086444568541700964999798050*x^168 + 13685980998381790969985924877943345864224598914382975000774537372351033947411745262902800*x^167 + 17056409154699694716325742198630886263623194169417588247233938367034497531475831782871400*x^166 + 20973066071704809799333875592390571257492223941654219622524694584649826594259170933012240*x^165 + 25445263984053629535956540240767972481516301105683428218504225047553098441564435323139850*x^164 + 30460024039305074772969872988948521802691046579066293633829875239406628791361805788284200*x^163 + 35978144336280631797058618095642094593033627481071056973291809159588989079651987996306700*x^162 + 41931362463866635619593497348877836863823364402399361364555921466571339790673539966918600*x^161 + 48221066833446630962532521951209512393396869062759265569239309686557040759274570961956390*x^160 + 54718941087599013858192932710592354488960986170506968021831840779071819301304477687326400*x^159 + 61269800231889036644032931697071720871442230993736675461065230168115628654277548959752800*x^158 + 67696702354115159368931490965995327955859248230841921138799345220715170121509459689796800*x^157 + 73808210205528333478626695011536572840763208140570705686052063886474178535256841467347900*x^156 + 79407453738361517397694927046894519745924554965027793703614634250275667941379774268319120*x^155 + 84302433763329008196182970494990757264508945339584301534659371978032387198040171312256600*x^154 + 88316835371106580015048826232847459991390323689088315893452675405557738969375417565221200*x^153 + 91300512241752072583124800092065279585694050840746704943907157682772527448070533023505700*x^152 + 93138777588901443172046775932845117429701313609352343298482469582425665584608865903173600*x^151 + 93759702772827452793193754439064084879232655700081358920472352712975170021839591675861424*x^150 + 93138777588901443172046775932845117429701313609352343298482469582425665584608865903173600*x^149 + 91300512241752072583124800092065279585694050840746704943907157682772527448070533023505700*x^148 + 88316835371106580015048826232847459991390323689088315893452675405557738969375417565221200*x^147 + 84302433763329008196182970494990757264508945339584301534659371978032387198040171312256600*x^146 + 79407453738361517397694927046894519745924554965027793703614634250275667941379774268319120*x^145 + 73808210205528333478626695011536572840763208140570705686052063886474178535256841467347900*x^144 + 67696702354115159368931490965995327955859248230841921138799345220715170121509459689796800*x^143 + 61269800231889036644032931697071720871442230993736675461065230168115628654277548959752800*x^142 + 54718941087599013858192932710592354488960986170506968021831840779071819301304477687326400*x^141 + 48221066833446630962532521951209512393396869062759265569239309686557040759274570961956390*x^140 + 41931362463866635619593497348877836863823364402399361364555921466571339790673539966918600*x^139 + 35978144336280631797058618095642094593033627481071056973291809159588989079651987996306700*x^138 + 30460024039305074772969872988948521802691046579066293633829875239406628791361805788284200*x^137 + 25445263984053629535956540240767972481516301105683428218504225047553098441564435323139850*x^136 + 20973066071704809799333875592390571257492223941654219622524694584649826594259170933012240*x^135 + 17056409154699694716325742198630886263623194169417588247233938367034497531475831782871400*x^134 + 13685980998381790969985924877943345864224598914382975000774537372351033947411745262902800*x^133 + 10834734957052251184572190528371815475844474140553188542279842086444568541700964999798050*x^132 + 8462633220892882582032716862396920963381482760668762648407923996512917440855191597475400*x^131 + 6521205599629221283801681817494097918841024950868281805537870844371718733835471172172220*x^130 + 4957641683928647759615313662422413622510720722882319501286100641920020090050358201066600*x^129 + 3718231262946485819711485246816810216883040542161739625964575481440015067537768650799950*x^128 + 2751061281255203381058208737529200622896122482061865156783038506498970685808291256083200*x^127 + 2007958521375924306864324768196600454642572156447453304088769484628559063779614882313600*x^126 + 1445730135390665500942313833101552327342651952642166378943914028932562525921322715265792*x^125 + 1026796971158143111464711529191443414305860761819720439590848031912331339432757610274000*x^124 + 719337991093840371873583218190615725276422228619465166719012180548751898811649399288000*x^123 + 497070634295181830002532223805874911286516483821315817451901675323013952549622899508000*x^122 + 338785571977721694191670007286685693726005648191064411894592203292780459279631249944000*x^121 + 227739190051690694428844838231605383004703796839548854662475869991257975404641006906800*x^120 + 150987308321562891334040776728136165527980417794176036240315493916856116290369728336000*x^119 + 98722470825637275103026661706858262075987196250038177541744746022559768343703283912000*x^118 + 63657112335656822197580033231744671721128356051937185518720655905257118385557308752000*x^117 + 40477620343868740201722086348446340170500095967807884270056069244103711147338071326000*x^116 + 25380561945344723585944659548215002485286546660895754461224346120627191854547114993600*x^115 + 15692282923197006518191590580885619816171789602166729908821504321893156254155474324000*x^114 + 9566418466547907716972413509202998176703657832336936949762842206929517716437027128000*x^113 + 5750028120850604106478099609254993584933581569436563166612772177569337776369064178000*x^112 + 3407424071615172803838873842521477679960640930036481876511272401522570534144630624000*x^111 + 1990653010259390427505868402736231697240163911758155201540795981942133312052915785600*x^110 + 1146449377636298151966730493722437103122607488447105089892605015778191959821050976000*x^109 + 650848865428940096689445957373675230418563626253825285407780972499077727190075814500*x^108 + 364205582727075287266632970965579921684999334898513631212644274766323287754032062000*x^107 + 200876275009263173904792411821221915568530561000726590411097615463899957678770261000*x^106 + 109194282825548186840553823861792425898790971620907787608083831995761002635639218800*x^105 + 58496937227972242950296691354531656731495163368343457647187767140586251411949581500*x^104 + 30881631836086869374775918278534478680586279138617865966028059810258731709861708000*x^103 + 16064687268267411846474341326712380323739327026654748457075202830589138212705838000*x^102 + 8234161313383296524323531735299813030258348526225046947847591400603477877869324000*x^101 + 4158251463258564744783383526326405580280466005743648708663033657304756328324008620*x^100 + 2068781822516698878001683346431047552378341296389874979434345103136694690708462000*x^99 + 1013907922916599945159240848003335186561662318527711004772278045596696902871969000*x^98 + 489472790373531008007909374898161814202181808944412209200410090977715746214054000*x^97 + 232739513069767194984152987083929882243194291507882275943332249141364840111584500*x^96 + 108990210998525125455993593951498871684617814559788773124682419110102559271766400*x^95 + 50262475945921781156890249637827149563294623219320065275945775803202636557368000*x^94 + 22824505985104576950471900801718608980433307162396551381347357128024385683056000*x^93 + 10205187772186181040355224877691493438366815221648458069544731792818595521751000*x^92 + 4492235765747027060826223391136925341290655504266306901426389114542156880388000*x^91 + 1946635498490378393024696802826000981225950718515399657284768616301601314834800*x^90 + 830318459071725380911008114949479091518178031594246299315778082782673546612000*x^89 + 348577088949922447646602463351432260118480400056075097354265327205933705889000*x^88 + 144013069613113499497187872182751356293081104248519289047771590582733174264000*x^87 + 58547369422153618954464228410744710268682504998229804425963216732232645612000*x^86 + 23418947768861447581785691364297884107473001999291921770385286692893058244800*x^85 + 9215789631264921502091591509098704394144468379350987733716432263406990513000*x^84 + 3567402437909001871777390261586595249346245824264898477567651198738189876000*x^83 + 1358231203424069519988639411521501860989625703733883365312454355482888806000*x^82 + 508561455163350231228622976003484715073741131078440346829320808902268868000*x^81 + 187243081219233494225083913892192099640786507351607582241704479641289901400*x^80 + 67780300893840178905007751635182660503452129358047993571657730186892272000*x^79 + 24120016984744928529259515221528964773750982969755817532256579661101304000*x^78 + 8436597869103607288261175727709682745975680141887685056125619791775344000*x^77 + 2900080517504365005339779156400203443929140048773891738043181803422774500*x^76 + 979582752579252179581436515050735385504953972030292320405696964711692720*x^75 + 325082771873645634816848401012412185455183840275539486860297665280429000*x^74 + 105974119465417519720029875219905293936932177006123004527145494408598000*x^73 + 33930310179717012892816582855495993234193197023890260221410618823805500*x^72 + 10668045122007095756693423430548958571449389457293007580530849586524000*x^71 + 3293179146358712168370578537256417645969159354207841470511697046274800*x^70 + 997933074654155202536538950683762923020957380062982263791423347356000*x^69 + 296799060996278917995780981022326041760543358725628345696587116239500*x^68 + 86619468445265950316365264847717471415094199113058916340634866542000*x^67 + 24801300794157344748702874977765258909450048463995501687275795121000*x^66 + 6965471712401637248571871270351093991590226377122140899405116927600*x^65 + 1918456191975027208293100138020428429887138620817538807039544916500*x^64 + 518064119351906081564381471870495440982180893385326935234307488000*x^63 + 137134619828445727472924507259837028495283177660821835797316688000*x^62 + 35574671252567510892557821966987011576182246924564660332358304000*x^61 + 9041895610027575685191779749942532108946321093326851167807735600*x^60 + 2251094342745454527433638112018887661978337201658137220201096000*x^59 + 548820521578437260820597721525266000234387995445578909057292000*x^58 + 130994198566046753611500690734425629685574089447915953602152000*x^57 + 30601103763379774409244013819107626606875914338242661292306000*x^56 + 6994538003058234150684346015796028938714494705884036866812800*x^55 + 1563819472228466984909101751499112161094704100909032632824000*x^54 + 341887657896102093866767184538267436028801706271610373168000*x^53 + 73064701082634721673139761211807153667445525937078023298000*x^52 + 15258491792357451915675773425758923657458503408546414504000*x^51 + 3112732325640920190797857778854820426121534695343468558816*x^50 + 620066200326876532031445772680243112773214082737742740800*x^49 + 120568427841337103450558900243380605261458293865672199600*x^48 + 22874642436301110536074415856451656334189715832222393600*x^47 + 4232709427189575571635817107296172628767388362655324800*x^46 + 763547582944001867824500340924015454601175939929980160*x^45 + 134217348564375328328525450553049591629112958190816825*x^44 + 22978845668609005628230038226981253041560195176637900*x^43 + 3829807611434834271371673037830208840260032529439650*x^42 + 621049882935378530492703735864358190312437707476700*x^41 + 97934789232117383654618666040148791549269023102095*x^40 + 15009163100707645004539259163241194107167666375800*x^39 + 2234188400487015859454317203688574695341751865100*x^38 + 322810491325120162202524919164128663205272132600*x^37 + 45242379466020628793535689428305911131041927675*x^36 + 6146134568968840137989754035543444531009469420*x^35 + 808701916969584228682862373097821648817035450*x^34 + 102980768453055669570102324664142082620895900*x^33 + 12680467757279242894826032514614510173468525*x^32 + 1508457130977456403845475986868640615431200*x^31 + 173193226149263513034110205899732811401360*x^30 + 19172681861542086313739137184472266944800*x^29 + 2044146227885001849626599185109175519850*x^28 + 209656023372820702525805044626582104600*x^27 + 20659535149876492584659621185831083300*x^26 + 1953265141442868389822364184842211512*x^25 + 176926190348085904875214147177736550*x^24 + 15329345012108526054170178816843600*x^23 + 1268255162872288126783863715062600*x^22 + 100005783452295121108405024126800*x^21 + 7500433758922134083130376809510*x^20 + 533838701702642995240596214200*x^19 + 35967855788475946487841588900*x^18 + 2287708142023205076965189400*x^17 + 136940276107022839114113450*x^16 + 7687875149867948862546720*x^15 + 403210235132934380902800*x^14 + 19668791957704116141600*x^13 + 887827414757477464725*x^12 + 36864806149099410300*x^11 + 1398320233241701770*x^10 + 48052241692154700*x^9 + 1481062243936275*x^8 + 40438559561400*x^7 + 962822846700*x^6 + 19582837560*x^5 + 330791175*x^4 + 4455100*x^3 + 44850*x^2 + 300*x + 1
# An issue of considerable importance both in pure mathematics and # in computer security is the difficulty in factoring natural numbers. # Now, if we were to look at 400-digit integers, # Sage wouldn't be of much help. But let's take a garden-variety # 50-digit integer and try factoring it factor(24982379423749287492874297492384729742974234871023)
# Solving equations. For equations in one variable # linear and quadratic are too easy, so var('x') solve(x^3-3*x^2+3*x+9==0,x)
[x == -1/2*(I*sqrt(3) + 1)*(-1)^(1/3)*10^(1/3) + 1, x == -1/2*(-I*sqrt(3) + 1)*(-1)^(1/3)*10^(1/3) + 1, x == (-1)^(1/3)*10^(1/3) + 1]
# Let's write out a sum. Say, the odd numbers up to 101. # Using range, the odd numbers beginning at 1 and ending just # before 103. M = range(1,103,2); M
# Now add them P = sum(M); P
# By the way sqrt(P)
# Or, a symbolic sum Q = [x^i for i in range(1,103,2)];R = sum(Q); R
# Let's move on to differential calculus diff(sin(x),x)
cos(x)
diff(sin(x)*(1/x),x)
cos(x)/x - sin(x)/x^2
# Or a fifth derivative diff(sin(x)*(1/x),x,5)
cos(x)/x - 5*sin(x)/x^2 - 20*cos(x)/x^3 + 60*sin(x)/x^4 + 120*cos(x)/x^5 - 120*sin(x)/x^6
# Or take the derivative of a previous sum diff(R,x)
# If derivatives, why not indefinite integrals? integral(1/x,x)
log(x)
# Well, we omitted the constant, but who cares? # # And, of course, the definite integral integral(1/x,x,1,10)
log(10)
# Note that the definite integral solution is described # by the log function. # However,we can compute log(10) with 100 bits of precision. numerical_approx(log(10), prec=100)
# Of course, we can take limits limit(sin(x)/x,x=0)
# Let's go back to the integers, # and calculate an integer division, or modular arithmetic. mod(238472368426,1241246186)
# Alternatively, 238472368426 % 1241246186
# Finding the modular inverse is also possible inverse_mod(45,97)
mod(45*69,97)
# We next illustrate the extended Euclidean algorithm, # the Mobius function, # and the Chinese remainder theorem d,u,v = xgcd(12,15) d == u*12 + v*15
d, u, v
prime_divisors(1001)
phi = 1001*prod([1-1/p for p in prime_divisors(1001)]); phi
euler_phi(1001)
# Better still to calculate a^b (mod c), in this example # the inverse of 23 mod 9973 mod(23^(-1),9973)
# Check mod(23*7805,9973)
# Series # For the terms up to x^20 of the Taylor series for the cosine g=cos(x) g.taylor(x,0,20)
1/2432902008176640000*x^20 - 1/6402373705728000*x^18 + 1/20922789888000*x^16 - 1/87178291200*x^14 + 1/479001600*x^12 - 1/3628800*x^10 + 1/40320*x^8 - 1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1
# Matrices # Here are two 3 x 3 matrices over the integers A = matrix(ZZ,3,[1,2,3,4,5,6,7,8,10]); A
B = matrix(ZZ,3,[9,6,3,8,5,2,7,4,-1]); B
# Let's multiply them A * B
# A few other matrix computations A.determinant()
B.determinant()
C=A*B C.determinant()
A.inverse()
A.eigenvalues()
A^10
A*A*A*A*A*A*A*A*A*A
# From the matrix A above, solve AX=D where D = [10,11,12] D = vector([10, 11, 12]); D
A.solve_right(D)
G=PermutationGroup([(1,2,3,4,5)]) G.order()
# Abstract algebra. List all the elements of S5 # the symmetric group on 5 elements, # That is, all permutations permutations([1,2,3,4,5])
# Displaying a graph d = {0: [1,4,5], 1: [2,6], 2: [3,7], 3: [4,8], 4: [9], 5: [7, 8], 6: [8,9], 7: [9]} G = Graph(d); G G.plot().show()
# Un hommage a Pascal # for j in range(1,20): R = [binomial(j,i) for i in range (0,j+1)];R
# Signore Fibonacci Q = [fibonacci(i) for i in range(1,101)]; Q
# Let's graph some functions plot(cos, (-5,5))
plot(sin(x),(x,-10,10))
plot(log(x),(x,1,10))
y = var('y') plot3d(x^2 + y^2, (x,-2,2), (y,-2,2))
# Typesetting # Let's write Sage output into Latex form k = 1/(sqrt(3)*I + 3/4 + sqrt(73)*5/9); k
latex(k)
# Writing programs in Sage # # Sage has a fairly standard syntax. It shouldn't be too # unfamiliar to persons who write in any of many procedural languages # print("Hello, World!") name = "Wayne" print "Hello,",name,"!" print "Count to ten:" for i in range(1,11): print(i)