Skip to content

Instantly share code, notes, and snippets.

@ordonezf
Last active April 4, 2016 23:40
Show Gist options
  • Save ordonezf/fb85a4ff609f0861bf72 to your computer and use it in GitHub Desktop.
Save ordonezf/fb85a4ff609f0861bf72 to your computer and use it in GitHub Desktop.
Finger 3
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Finger 3\n",
"##Collatz me Spark"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"l = range(2,20000000)\n",
"rdd = sc.parallelize(l,8)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def collatz(n):\n",
" n1 = n\n",
" if n == 1:\n",
" return (n,(1,[]))\n",
" l = [n]\n",
" while n != 1:\n",
" if n % 2 == 0:\n",
" n /= 2\n",
" else:\n",
" n = 3*n + 1\n",
" l.append(n)\n",
" return (n1,(len(l),l))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(15733191, (705, [15733191, 47199574, 23599787, 70799362, 35399681, 106199044, 53099522, 26549761, 79649284, 39824642, 19912321, 59736964, 29868482, 14934241, 44802724, 22401362, 11200681, 33602044, 16801022, 8400511, 25201534, 12600767, 37802302, 18901151, 56703454, 28351727, 85055182, 42527591, 127582774, 63791387, 191374162, 95687081, 287061244, 143530622, 71765311, 215295934, 107647967, 322943902, 161471951, 484415854, 242207927, 726623782, 363311891, 1089935674, 544967837, 1634903512, 817451756, 408725878, 204362939, 613088818, 306544409, 919633228, 459816614, 229908307, 689724922, 344862461, 1034587384, 517293692, 258646846, 129323423, 387970270, 193985135, 581955406, 290977703, 872933110, 436466555, 1309399666, 654699833, 1964099500, 982049750, 491024875, 1473074626, 736537313, 2209611940L, 1104805970L, 552402985L, 1657208956L, 828604478L, 414302239L, 1242906718L, 621453359L, 1864360078L, 932180039L, 2796540118L, 1398270059L, 4194810178L, 2097405089L, 6292215268L, 3146107634L, 1573053817L, 4719161452L, 2359580726L, 1179790363L, 3539371090L, 1769685545L, 5309056636L, 2654528318L, 1327264159L, 3981792478L, 1990896239L, 5972688718L, 2986344359L, 8959033078L, 4479516539L, 13438549618L, 6719274809L, 20157824428L, 10078912214L, 5039456107L, 15118368322L, 7559184161L, 22677552484L, 11338776242L, 5669388121L, 17008164364L, 8504082182L, 4252041091L, 12756123274L, 6378061637L, 19134184912L, 9567092456L, 4783546228L, 2391773114L, 1195886557L, 3587659672L, 1793829836L, 896914918L, 448457459L, 1345372378L, 672686189L, 2018058568L, 1009029284L, 504514642L, 252257321L, 756771964L, 378385982L, 189192991L, 567578974L, 283789487L, 851368462L, 425684231L, 1277052694L, 638526347L, 1915579042L, 957789521L, 2873368564L, 1436684282L, 718342141L, 2155026424L, 1077513212L, 538756606L, 269378303L, 808134910L, 404067455L, 1212202366L, 606101183L, 1818303550L, 909151775L, 2727455326L, 1363727663L, 4091182990L, 2045591495L, 6136774486L, 3068387243L, 9205161730L, 4602580865L, 13807742596L, 6903871298L, 3451935649L, 10355806948L, 5177903474L, 2588951737L, 7766855212L, 3883427606L, 1941713803L, 5825141410L, 2912570705L, 8737712116L, 4368856058L, 2184428029L, 6553284088L, 3276642044L, 1638321022L, 819160511L, 2457481534L, 1228740767L, 3686222302L, 1843111151L, 5529333454L, 2764666727L, 8294000182L, 4147000091L, 12441000274L, 6220500137L, 18661500412L, 9330750206L, 4665375103L, 13996125310L, 6998062655L, 20994187966L, 10497093983L, 31491281950L, 15745640975L, 47236922926L, 23618461463L, 70855384390L, 35427692195L, 106283076586L, 53141538293L, 159424614880L, 79712307440L, 39856153720L, 19928076860L, 9964038430L, 4982019215L, 14946057646L, 7473028823L, 22419086470L, 11209543235L, 33628629706L, 16814314853L, 50442944560L, 25221472280L, 12610736140L, 6305368070L, 3152684035L, 9458052106L, 4729026053L, 14187078160L, 7093539080L, 3546769540L, 1773384770L, 886692385L, 2660077156L, 1330038578L, 665019289L, 1995057868L, 997528934L, 498764467L, 1496293402L, 748146701L, 2244440104L, 1122220052L, 561110026L, 280555013L, 841665040L, 420832520L, 210416260L, 105208130L, 52604065L, 157812196L, 78906098L, 39453049L, 118359148L, 59179574L, 29589787L, 88769362L, 44384681L, 133154044L, 66577022L, 33288511L, 99865534L, 49932767L, 149798302L, 74899151L, 224697454L, 112348727L, 337046182L, 168523091L, 505569274L, 252784637L, 758353912L, 379176956L, 189588478L, 94794239L, 284382718L, 142191359L, 426574078L, 213287039L, 639861118L, 319930559L, 959791678L, 479895839L, 1439687518L, 719843759L, 2159531278L, 1079765639L, 3239296918L, 1619648459L, 4858945378L, 2429472689L, 7288418068L, 3644209034L, 1822104517L, 5466313552L, 2733156776L, 1366578388L, 683289194L, 341644597L, 1024933792L, 512466896L, 256233448L, 128116724L, 64058362L, 32029181L, 96087544L, 48043772L, 24021886L, 12010943L, 36032830L, 18016415L, 54049246L, 27024623L, 81073870L, 40536935L, 121610806L, 60805403L, 182416210L, 91208105L, 273624316L, 136812158L, 68406079L, 205218238L, 102609119L, 307827358L, 153913679L, 461741038L, 230870519L, 692611558L, 346305779L, 1038917338L, 519458669L, 1558376008L, 779188004L, 389594002L, 194797001L, 584391004L, 292195502L, 146097751L, 438293254L, 219146627L, 657439882L, 328719941L, 986159824L, 493079912L, 246539956L, 123269978L, 61634989L, 184904968L, 92452484L, 46226242L, 23113121L, 69339364L, 34669682L, 17334841L, 52004524L, 26002262L, 13001131L, 39003394L, 19501697L, 58505092L, 29252546L, 14626273L, 43878820L, 21939410L, 10969705L, 32909116L, 16454558L, 8227279L, 24681838L, 12340919L, 37022758L, 18511379L, 55534138L, 27767069L, 83301208L, 41650604L, 20825302L, 10412651L, 31237954L, 15618977L, 46856932L, 23428466L, 11714233L, 35142700L, 17571350L, 8785675L, 26357026L, 13178513L, 39535540L, 19767770L, 9883885L, 29651656L, 14825828L, 7412914L, 3706457L, 11119372L, 5559686L, 2779843L, 8339530L, 4169765L, 12509296L, 6254648L, 3127324L, 1563662L, 781831L, 2345494L, 1172747L, 3518242L, 1759121L, 5277364L, 2638682L, 1319341L, 3958024L, 1979012L, 989506L, 494753L, 1484260L, 742130L, 371065L, 1113196L, 556598L, 278299L, 834898L, 417449L, 1252348L, 626174L, 313087L, 939262L, 469631L, 1408894L, 704447L, 2113342L, 1056671L, 3170014L, 1585007L, 4755022L, 2377511L, 7132534L, 3566267L, 10698802L, 5349401L, 16048204L, 8024102L, 4012051L, 12036154L, 6018077L, 18054232L, 9027116L, 4513558L, 2256779L, 6770338L, 3385169L, 10155508L, 5077754L, 2538877L, 7616632L, 3808316L, 1904158L, 952079L, 2856238L, 1428119L, 4284358L, 2142179L, 6426538L, 3213269L, 9639808L, 4819904L, 2409952L, 1204976L, 602488L, 301244L, 150622L, 75311L, 225934L, 112967L, 338902L, 169451L, 508354L, 254177L, 762532L, 381266L, 190633L, 571900L, 285950L, 142975L, 428926L, 214463L, 643390L, 321695L, 965086L, 482543L, 1447630L, 723815L, 2171446L, 1085723L, 3257170L, 1628585L, 4885756L, 2442878L, 1221439L, 3664318L, 1832159L, 5496478L, 2748239L, 8244718L, 4122359L, 12367078L, 6183539L, 18550618L, 9275309L, 27825928L, 13912964L, 6956482L, 3478241L, 10434724L, 5217362L, 2608681L, 7826044L, 3913022L, 1956511L, 5869534L, 2934767L, 8804302L, 4402151L, 13206454L, 6603227L, 19809682L, 9904841L, 29714524L, 14857262L, 7428631L, 22285894L, 11142947L, 33428842L, 16714421L, 50143264L, 25071632L, 12535816L, 6267908L, 3133954L, 1566977L, 4700932L, 2350466L, 1175233L, 3525700L, 1762850L, 881425L, 2644276L, 1322138L, 661069L, 1983208L, 991604L, 495802L, 247901L, 743704L, 371852L, 185926L, 92963L, 278890L, 139445L, 418336L, 209168L, 104584L, 52292L, 26146L, 13073L, 39220L, 19610L, 9805L, 29416L, 14708L, 7354L, 3677L, 11032L, 5516L, 2758L, 1379L, 4138L, 2069L, 6208L, 3104L, 1552L, 776L, 388L, 194L, 97L, 292L, 146L, 73L, 220L, 110L, 55L, 166L, 83L, 250L, 125L, 376L, 188L, 94L, 47L, 142L, 71L, 214L, 107L, 322L, 161L, 484L, 242L, 121L, 364L, 182L, 91L, 274L, 137L, 412L, 206L, 103L, 310L, 155L, 466L, 233L, 700L, 350L, 175L, 526L, 263L, 790L, 395L, 1186L, 593L, 1780L, 890L, 445L, 1336L, 668L, 334L, 167L, 502L, 251L, 754L, 377L, 1132L, 566L, 283L, 850L, 425L, 1276L, 638L, 319L, 958L, 479L, 1438L, 719L, 2158L, 1079L, 3238L, 1619L, 4858L, 2429L, 7288L, 3644L, 1822L, 911L, 2734L, 1367L, 4102L, 2051L, 6154L, 3077L, 9232L, 4616L, 2308L, 1154L, 577L, 1732L, 866L, 433L, 1300L, 650L, 325L, 976L, 488L, 244L, 122L, 61L, 184L, 92L, 46L, 23L, 70L, 35L, 106L, 53L, 160L, 80L, 40L, 20L, 10L, 5L, 16L, 8L, 4L, 2L, 1L]))\n"
]
}
],
"source": [
"rdd = rdd.map(lambda x: collatz(x)).reduce(lambda x,y:x if x[1][0] > y[1][0] else y)\n",
"print rdd"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment