︠f226d0ce-fbf8-4ffd-a00a-7dc3641f8386︠ #density estimator n=10000 #check from 1...n sum = 0 for k in [1..n]: if moebius(k)!=0: #test for k's membership sum += 1 float(sum/n) ︡4f4d4bec-4671-401d-a051-444a1264e2a8︡{"stdout":"0.6083\n"}︡ ︠ebee70cd-958e-4f52-92dc-da44b246353c︠ r=[0,1] n=10 for k in [1..n]: r.append(.5*(r[k]+2./(k+1))) print 2^k, r[k], r[k]*2^k,prime_pi(2^k) ︡b4c99e4e-6880-425b-8931-e6992482417c︡{"stdout":"2 1 2 1\n4 1.00000000000000 4.00000000000000 2\n8 0.833333333333333 6.66666666666667 4\n16 0.666666666666667 10.6666666666667 6\n32 0.533333333333333 17.0666666666667 11\n64 0.433333333333333 27.7333333333333 18\n128 0.359523809523810 46.0190476190476 31\n256 0.304761904761905 78.0190476190476 54\n512 0.263492063492063 134.907936507937 97\n1024 0.231746031746032 237.307936507937 172\n"}︡ ︠ae9439bf-f101-40cd-bb59-1a736938b7d0︠ prime_pi(10^6) ︡790b9240-fe7d-40e5-b6a4-8989ff94624f︡{"stdout":"78498\n"}︡ ︠1df0b901-b232-47d0-a180-3e560f6263ce︠ 7*8 ︡582a0921-2cdd-4ceb-9baa-82603bd3d37c︡{"stdout":"56\n"}︡ ︠d8fa636d-faa7-4941-8d3e-215beeae943d︠ 5667^79 ︡8c9bdede-f2a9-4be1-bd4f-94b830192317︡{"stdout":"327268193105661183797371787183788407076350243721782765059766772115276680877327201409266959755406511909524212919139023898815457959703176202017356583363286595211739210796061985288573900187007140725411329731580142846117411835707993405458437717490490789889678522564843345607290152996427337407660016203\n"}︡ ︠c2d7c25c-7e2c-4204-adaa-367e8d883fd8︠ euler_phi(9) ︡cc6ff0da-331b-4052-a768-a4fd7d61f211︡{"stdout":"6\n"}︡ ︠aeb11848-4613-4f65-8436-ce9845e7ec6e︠ mod(6^16,17) ︡365e5005-41dc-4c1a-9972-8034c546a507︡{"stdout":"1\n"}︡ ︠1301e5ac-5043-43ac-a498-ae5d26b42d1b︠ sum=0 for k in [1..1000]: sum += k^3 ︡8ad85574-c40b-4312-b6e8-f3f5476bd829︡ ︠8a6aa7d4-6ab2-4a72-87f6-dc9c9b2d3b9a︠ sum ︡7cfabd95-f5d0-4bde-bbb7-0f6cc0085876︡{"stdout":"250500250000\n"}︡ ︠a66a2642-32db-45af-9975-b64c5de42959︠ n=10 print k ︡43bf5c6b-3f6a-4026-a509-294288bfce34︡ ︠7fb34dc5-9c66-4672-8395-c24770f5fdd3︠ n=100 for k in [0..n]: print k, 2^k ︡3c854518-778b-4910-a832-60c64a9abb53︡{"stdout":"0 1\n1 2\n2 4\n3 8\n4 16\n5 32\n6 64\n7 128\n8 256\n9 512\n10 1024\n11 2048\n12 4096\n13 8192\n14 16384\n15 32768\n16 65536\n17 131072\n18 262144\n19 524288\n20 1048576\n21 2097152\n22 4194304\n23 8388608\n24 16777216\n25 33554432\n26 67108864\n27 134217728\n28 268435456\n29 536870912\n30 1073741824\n31 2147483648\n32 4294967296\n33 8589934592\n34 17179869184\n35 34359738368\n36 68719476736\n37 137438953472\n38 274877906944\n39 549755813888\n40 1099511627776\n41 2199023255552\n42 4398046511104\n43 8796093022208\n44 17592186044416\n45 35184372088832\n46 70368744177664\n47 140737488355328\n48 281474976710656\n49 562949953421312\n50 1125899906842624\n51 2251799813685248\n52 4503599627370496\n53 9007199254740992\n54 18014398509481984\n55 36028797018963968\n56 72057594037927936\n57 144115188075855872\n58 288230376151711744\n59 576460752303423488\n60 1152921504606846976\n61 2305843009213693952\n62 4611686018427387904\n63 9223372036854775808\n64 18446744073709551616\n65 36893488147419103232\n66 73786976294838206464\n67 147573952589676412928\n68 295147905179352825856\n69 590295810358705651712\n70 1180591620717411303424\n71 2361183241434822606848\n72 4722366482869645213696\n73 9444732965739290427392\n74 18889465931478580854784\n75 37778931862957161709568\n76 75557863725914323419136\n77 151115727451828646838272\n78 302231454903657293676544\n79 604462909807314587353088\n80 1208925819614629174706176\n81 2417851639229258349412352\n82 4835703278458516698824704\n83 9671406556917033397649408\n84 19342813113834066795298816\n85 38685626227668133590597632\n86 77371252455336267181195264\n87 154742504910672534362390528\n88 309485009821345068724781056\n89 618970019642690137449562112\n90 1237940039285380274899124224\n91 2475880078570760549798248448\n92 4951760157141521099596496896\n93 9903520314283042199192993792\n94 19807040628566084398385987584\n95 39614081257132168796771975168\n96 79228162514264337593543950336\n97 158456325028528675187087900672\n98 316912650057057350374175801344\n99 633825300114114700748351602688\n100 1267650600228229401496703205376\n"}︡ ︠da453c20-5d68-45db-8a16-6e97858ac8de︠ b=2^12 b ︡7087506a-498c-40bd-86bd-a7a41b7eb1a1︡{"stdout":"4096\n"}︡ ︠d8d997aa-7fcf-41ef-b432-6d15c8467a9c︠ b.digits()[-1] ︡0e9c054e-a473-4ceb-8504-eb3724d9f2ed︡{"stdout":"4\n"}︡ ︠bb728505-ee51-46bb-bb64-54233426d4e6︠ mylist = [1,2,3,4,5,6,77] ︡a1c3f65f-dc5f-4f4f-a6a5-17de5fe1982b︡ ︠a9f8fc30-ede6-4e61-9229-81ee37294a98︠ mylist ︡040ebf9d-2655-4d4f-8e65-418154cdfabf︡{"stdout":"[1, 2, 3, 4, 5, 6, 77]\n"}︡ ︠dc5f047e-a37e-45f6-841f-f2ef07a4332e︠ mylist[-1] ︡0688f2bb-495b-4645-b8d9-9184e4265130︡{"stdout":"77\n"}︡ ︠2735fcad-cf9d-4471-8331-5d54c996831e︠ a=2 n=10000 powers = [a^k for k in range(n)] lastDigits = [powers[i].digits()[-1] for i in range(n)] [float(lastDigits.count(j)/n) for j in [1..9]] ︡0dd531ba-8a78-4034-a3d5-239bcfaea7ad︡︡{"stdout":"[0.301, 0.1761, 0.1249, 0.097, 0.0791, 0.067, 0.0579, 0.0512, 0.0458]\n","done":false}︡{"done":true} ︠0205eb9b-740f-4aee-95a5-8f23e9785721︠ sum = 0 vals =[] n = 2016 for k in range(n): sum += moebius(k) vals.append(sum) ︡d123a03b-84fb-439d-859e-a45070bc9dad︡ ︠be9038ff-111e-4583-a303-09066e94e855︠ list_plot(vals,plotjoined=true) CRT_list? ︡e3b2332c-20c6-4673-ad16-1f8661351872︡{"once":false,"file":{"show":true,"uuid":"f804f8d0-f91a-4f21-9c3b-665d31e0d19f","filename":"/projects/dcd3daf5-4da6-409e-97fd-dc703e1c80f3/.sage/temp/compute3-us/16493/tmp_haMyFj.svg"}}︡{"html":"
"}︡{"code":{"source":"File: /projects/sage/sage-6.7/local/lib/python2.7/site-packages/sage/rings/arith.py\nSignature : CRT_list()\nDocstring :\nGiven a list \"v\" of elements and a list of corresponding\n \"moduli\", find a single element that reduces to each element of\n \"v\" modulo the corresponding moduli.\n\n See also: * \"crt()\"\n\n\n EXAMPLES:\n\n sage: CRT_list([2,3,2], [3,5,7])\n 23\n sage: x = polygen(QQ)\n sage: c = CRT_list([3], [x]); c\n 3\n sage: c.parent()\n Univariate Polynomial Ring in x over Rational Field\n\n It also works if the moduli are not coprime:\n\n sage: CRT_list([32,2,2],[60,90,150])\n 452\n\n But with non coprime moduli there is not always a solution:\n\n sage: CRT_list([32,2,1],[60,90,150])\n Traceback (most recent call last):\n ...\n ValueError: No solution to crt problem since gcd(180,150) does not divide 92-1\n\n The arguments must be lists:\n\n sage: CRT_list([1,2,3],\"not a list\")\n Traceback (most recent call last):\n ...\n ValueError: Arguments to CRT_list should be lists\n sage: CRT_list(\"not a list\",[2,3])\n Traceback (most recent call last):\n ...\n ValueError: Arguments to CRT_list should be lists\n\n The list of moduli must have the same length as the list of\n elements:\n\n sage: CRT_list([1,2,3],[2,3,5])\n 23\n sage: CRT_list([1,2,3],[2,3])\n Traceback (most recent call last):\n ...\n ValueError: Arguments to CRT_list should be lists of the same length\n sage: CRT_list([1,2,3],[2,3,5,7])\n Traceback (most recent call last):\n ...\n ValueError: Arguments to CRT_list should be lists of the same length\n\n TESTS:\n\n sage: CRT([32r,2r,2r],[60r,90r,150r])\n 452","mode":"text/x-rst","lineno":-1,"filename":null}}︡ ︠1125cd70-bee6-4cf9-97c8-1b6b54ec5725︠ CRT_list([1,2,3,4,5,6,7,8,9],[2,3,4,5,6,7,8,9,10]) ︡20d6d127-efa3-415e-98bf-c99b3ecd3494︡{"stdout":"2519\n"}︡ ︠2767c57c-c5ec-4712-8fdf-a15a2f752a14︠ michelle = Primes() ︡675dcdef-646a-40c7-bb38-64bc4dbe45b2︡ ︠83ff1942-a15f-4506-95fb-2c44b4a87313︠ michelle ︡0dc15620-c2f2-468d-b3fb-6f072c624399︡{"stdout":"Set of all prime numbers: 2, 3, 5, 7, ...\n"}︡ ︠2ac4c4ff-1ccc-4290-955a-ca700b52497d︠ michelle.next(9) ︡8c05872a-b1f4-4105-a485-7f9c5d060be6︡{"stdout":"11\n"}︡ ︠e13300c3-91fc-4686-ac06-ef6504b86b74︠ michelle.unrank(9) ︡9b273587-1d39-4fce-8e01-06b3cbb3b165︡{"stdout":"29\n"}︡ ︠8f9a51cd-28cf-4be4-845a-fb1ae43e2d54︠ ︡b129a01e-4afe-4be8-8255-70b450ab4f21︡ ︠4b57a35c-456d-4a7c-a0ae-b1ebfddd20e1︠ l = 2015 valList = [-k for k in range(l)] modList = [michelle.unrank(k)^2 for k in range(l)] n =CRT_list(valList, modList) n [moebius(n+k) for k in range(l)] ︡3d674cb6-2ff0-41c7-b271-23b21e9f5b06︡ ︠0c6457ad-e1a5-439a-bd8d-db02ed8cc8cd︠ int(log(n,10)) ︡401799aa-57b3-4a7a-bdf7-f49a9bb726b0︡{"stdout":"15091\n"}︡ ︠aaa463ce-3da7-4855-929c-e1a4001a3b01︠ modList ︡ee2d08e4-abe0-4411-85ed-93a0111665bf︡{"stdout":"[4, 9, 25, 49, 121, 169, 289, 361, 529, 841]\n"}︡ ︠8e786300-18af-4d9b-b3ad-5d4a872d9e53︠ n=400 relPrimeArray=[[gcd(x,y)==1 for x in [1..n]] for y in [1..n]] matrix_plot(relPrimeArray,origin='lower',cmap='winter') sum =0 #let's compute the number of green pixels for x in [1..n]: for y in [1..n]: if gcd(x,y)==1: sum += 1 print float(sum/n^2) ︡da3d5cfa-f9cd-433e-a30e-b73f1804490d︡{"once":false,"file":{"show":true,"uuid":"3a4d8116-9944-44dc-9d7a-be9315bc83a7","filename":"/projects/dcd3daf5-4da6-409e-97fd-dc703e1c80f3/.sage/temp/compute3-us/4988/tmp_TNp0oj.svg"}}︡{"html":"
"}︡{"stdout":"0.60846875\n"}︡ ︠107f43ce-0d57-4af8-a857-e177f36fcc2f︠ n = 100000 sum = 0 for k in [1..n]: if moebius(k) != 0: sum += 1 sum ︡bc6fc4ee-57b4-4cdc-bde0-093b49cfe83f︡{"stdout":"60794\n"}︡ ︠aa567b1c-54b3-496d-9df8-00d9bac0c3d0︠ ######################################################################################### # This is an INTERACTIVE simulation which allows you to change the price per game # and the total number of games. It also prints out the maximum payout, and # when this happened, the final amount of money on hand, the minimum amount, and the maximum, # and the average payout (which, theoretically, is INFINITY). # # Note the use of the @interactive command, followed by a function definition (the # name of the function can be anything, but we choose the generic "_" since we will never use the # function's name. You can look up the use of Sage interactive online, or just study this example. # It's pretty flexible, and easier to use than Mathematica's Manipulate command, although less slick. ############################################################################################## # We include the already-defined first_head function, so that this is a stand-alone cell. def first_head(): coin=0 # 0 for tails, 1 for heads. Start by setting the coin to tails count=0 # Keep track of the number of flips before first head while coin == 0: # the two lines in this "while loop" will be executed as long as the coin value is 0 # but once it equals 1, the loop ends and we move to the final line of the function def. coin = randint(0,1) # set coin equal to 0 or 1 randomly count += 1 # increment the count return count # this is the count value when the coin first equals 1 (and thus, the while loop ends). ############################################################################################################ # interactive part starts here ############################################################################################################ @interact def _(price= [5,10,15,20,100], nGames = [100,1000,10000,100000]): # note that the selection boxes for price and nGames are defined in the function defn above. accumulation = 0 #player's profit sequence = [] #keep track of the first_head() outputs daily = [] #accumlation on each day; to be built for _ in range(nGames): fh = first_head() sequence.append(fh) accumulation += 2^fh-price daily.append(accumulation) # Compute average payout totalPayout = 0 for i in range(len(sequence)): totalPayout += 2^sequence[i] avg = float(totalPayout/nGames) # Compute longest run of tails, max min, etc. longestRun=max(sequence) longestRunIndex=sequence.index(longestRun) print('Luckiest: game # %s, first head at position %s, winning $%s.'%(longestRunIndex, longestRun, 2^longestRun)) print('Lowest: %s'%min(daily)) print('Highest: %s'%max(daily)) print('Final: %s'%daily[nGames-1]) print('Average payout: %s.'%avg) # Plot the money on hand. l=list_plot(daily,plotjoined = True,gridlines=True) show(l) #in interactive mode, the plot doesn't display with just the list_plot() command. #instead, we use the show() function. ︡abac6a81-f0aa-48e7-b516-544297207e34︡︡{"interact":{"style":"None","flicker":false,"layout":[[["price",12,null]],[["nGames",12,null]],[["",12,null]]],"id":"f4320f19-a7b2-4f0d-8b33-6520dcdecba3","controls":[{"buttons":true,"control_type":"selector","ncols":null,"button_classes":null,"default":0,"lbls":["5","10","15","20","100"],"label":"price","nrows":null,"width":null,"var":"price"},{"buttons":true,"control_type":"selector","ncols":null,"button_classes":null,"default":0,"lbls":["100","1000","10000","100000"],"label":"nGames","nrows":null,"width":null,"var":"nGames"}]},"done":false}︡{"done":true} ︠51c7ad38-8cae-48f2-bdbb-59af19e833f6︠