Math 350: HW2 Computations

115 days ago by Jonas_Kibelbek

# If you open a SAGE account, you can download this worksheet to a file and then upload it to your account. Then you # can modify the code to see what happens or write your own code. To calculate a cell, hit "Shift + Enter" while the cell # is highlighted. 
       
# We can rewrite 4/n = 1/x + 1/y + 1/z by putting everything over a common denominator # We get 4xyz = nyz + nxz + nxy. Now, putting everything on the same side, we get # 4xyz - nyz - nxz - nxy = 0. This is the version I will look for. 
       
# Here, I will look for solutions with n from 2 to 50, and with x <= y <= z such that x+y+z=S<=500. for n in range(2,51): a=0 for S in range(501): for x in range(1,floor(S/3)+1): for y in range(x,floor((S-x)/2)+1): z=S-x-y if 4*x*y*z-n*y*z-n*x*z-n*x*y==0: a=1 print n,x,y,z break if a==1: break if a==1: break continue 
       
2 1 2 2
3 2 2 3
4 3 3 3
5 2 5 10
6 4 4 6
7 4 4 14
8 6 6 6
9 6 6 9
10 5 10 10
11 6 6 33
12 9 9 9
13 4 26 52
14 7 14 14
15 10 12 12
16 12 12 12
17 6 17 102
18 12 12 18
19 6 38 57
20 15 15 15
21 14 14 21
22 11 22 22
23 12 12 138
24 18 18 18
25 10 25 50
26 13 26 26
27 18 18 27
28 21 21 21
29 10 29 290
30 20 24 24
31 16 16 248
32 24 24 24
33 22 22 33
34 17 34 34
35 21 30 30
36 27 27 27
38 19 38 38
39 26 26 39
40 30 30 30
41 12 123 164
42 30 30 35
43 12 172 258
44 33 33 33
45 30 36 36
46 23 46 46
47 14 94 329
48 36 36 36
49 28 28 98
50 25 50 50
2 1 2 2
3 2 2 3
4 3 3 3
5 2 5 10
6 4 4 6
7 4 4 14
8 6 6 6
9 6 6 9
10 5 10 10
11 6 6 33
12 9 9 9
13 4 26 52
14 7 14 14
15 10 12 12
16 12 12 12
17 6 17 102
18 12 12 18
19 6 38 57
20 15 15 15
21 14 14 21
22 11 22 22
23 12 12 138
24 18 18 18
25 10 25 50
26 13 26 26
27 18 18 27
28 21 21 21
29 10 29 290
30 20 24 24
31 16 16 248
32 24 24 24
33 22 22 33
34 17 34 34
35 21 30 30
36 27 27 27
38 19 38 38
39 26 26 39
40 30 30 30
41 12 123 164
42 30 30 35
43 12 172 258
44 33 33 33
45 30 36 36
46 23 46 46
47 14 94 329
48 36 36 36
49 28 28 98
50 25 50 50
# Notice we found solutions for every n except 37. For n=37, there is no solution with x+y+z<=500. # Also, notice that we really only need to test prime values of n. # Another (faster) way to find solutions would be to loop through values of x and y, then solve for z and check if it is an integer. # (If anyone knows a better way to handle breaking these Python loops, I would be interested to know!) 
       
 
       
# Now we look for square Fibonacci numbers. I will just compute them with the recursive formula and check each number. # There are only 2 squares among the first 100,000 Fibonacci numbers. (There are 3 if you count 0) 
       
a=0 b=1 for n in range(2,100001): c=a+b a=b b=c if c.is_square(): print "The ", n, "th Fibonacci number ", c, " is a square." if n%10000==0: print "n is ", n 
       
The  2 th Fibonacci number  1  is a square.
The  12 th Fibonacci number  144  is a square.
n is  10000
n is  20000
n is  30000
n is  40000
n is  50000
n is  60000
n is  70000
n is  80000
n is  90000
n is  100000
The  2 th Fibonacci number  1  is a square.
The  12 th Fibonacci number  144  is a square.
n is  10000
n is  20000
n is  30000
n is  40000
n is  50000
n is  60000
n is  70000
n is  80000
n is  90000
n is  100000
 
       
# Here we find the quotient and remainder when 111,111,111,111 is divided by 987,654,321. q=floor(111111111111/987654321) r=111111111111-q*987654321 print q, r 
       
112 493827159
112 493827159
 
       
# We verify the Collatz conjecture for n less than or equal to 10,000. # I won't do anything fancy to speed up the process, but I will keep track of the 5 longest paths back to 1 and which numbers # (including ties) give you paths of those lengths. # I also print an update after every 1000 numbers-- so, for example, after 2000 numbers, there are 4 different n-values tied # for the second longest path back to 1. In the first 10,000 numbers, n=6171 takes the longest time to get to 1 (166 steps). 
       
L1=[]; n1=0; L2=[]; n2=0; L3=[]; n3=0; L4=[]; n4=0; L5=[]; n5=[] for n in srange(1,10001): a=1 m=n while n>1: if n%2==0: n=n/2 a=a+1 else: n=(3*n+1)/2 a=a+1 if a==n1: L1=L1+[m] if a==n2: L2=L2+[m] if a==n3: L3=L3+[m] if a==n4: L4=L4+[m] if a==n5: L5=L5+[m] if a>n1: n5=n4; n4=n3; n3=n2; n2=n1; n1=a L5=L4; L4=L3; L3=L2; L2=L1; L1=[m] if a<n1 and a>n2: n5=n4; n4=n3; n3=n2; n2=a L5=L4; L4=L3; L3=L2; L2=[m] if a<n2 and a>n3: n5=n4; n4=n3; n3=a L5=L4; L4=L3; L3=[m] if a<n3 and a>n4: n5=n4; n4=a L5=L4; L4=[m] if a<n4 and a>n5: n5=a L5=[m] if m%1000==0: print m print n1, L1 print n2, L2 print n3, L3 print n4, L4 print n5, L5 
       
1000
114 [871]
111 [937]
109 [703]
98 [763, 775]
95 [859, 865, 873, 879, 889]
2000
116 [1161]
115 [1665, 1695, 1742, 1743]
114 [871]
113 [1249, 1263, 1307]
112 [1874, 1875, 1895, 1915, 1951, 1961]
3000
138 [2919]
133 [2463]
122 [2631]
117 [2223, 2322, 2323]
116 [1161]
4000
151 [3711]
138 [2919]
133 [2463]
132 [3695]
127 [3175]
5000
151 [3711]
138 [2919]
137 [4379]
134 [4926, 4927]
133 [2463]
6000
151 [3711]
150 [5567]
139 [5838, 5839]
138 [2919]
137 [4379]
7000
166 [6171]
163 [6943]
155 [6591]
151 [3711]
150 [5567]
8000
166 [6171]
163 [6943]
160 [7963]
155 [6591]
152 [7422, 7423]
9000
166 [6171]
163 [6943]
160 [7963]
157 [8959]
155 [6591]
10000
166 [6171]
165 [9257]
163 [6943]
160 [7963]
157 [8959]
1000
114 [871]
111 [937]
109 [703]
98 [763, 775]
95 [859, 865, 873, 879, 889]
2000
116 [1161]
115 [1665, 1695, 1742, 1743]
114 [871]
113 [1249, 1263, 1307]
112 [1874, 1875, 1895, 1915, 1951, 1961]
3000
138 [2919]
133 [2463]
122 [2631]
117 [2223, 2322, 2323]
116 [1161]
4000
151 [3711]
138 [2919]
133 [2463]
132 [3695]
127 [3175]
5000
151 [3711]
138 [2919]
137 [4379]
134 [4926, 4927]
133 [2463]
6000
151 [3711]
150 [5567]
139 [5838, 5839]
138 [2919]
137 [4379]
7000
166 [6171]
163 [6943]
155 [6591]
151 [3711]
150 [5567]
8000
166 [6171]
163 [6943]
160 [7963]
155 [6591]
152 [7422, 7423]
9000
166 [6171]
163 [6943]
160 [7963]
157 [8959]
155 [6591]
10000
166 [6171]
165 [9257]
163 [6943]
160 [7963]
157 [8959]