-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/tingleshao/eulerMKII
- Loading branch information
Showing
10 changed files
with
397 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# problem 112 | ||
# boundcy numbers | ||
def isDECorINC(n): | ||
n_lst = map(int, list(str(n))) | ||
if len(n_lst) <= 2: | ||
return True | ||
if n_lst[0] < n_lst[1]: | ||
return isINC(n_lst[1:]) | ||
if n_lst[0] > n_lst[1]: | ||
return isDEC(n_lst[1:]) | ||
return isDECorINC(int("".join(map(str,n_lst[1:])))) | ||
|
||
|
||
|
||
|
||
|
||
def isDEC(n_lst): | ||
if len(n_lst) == 1: | ||
return True | ||
if n_lst[0] < n_lst[1]: | ||
return False | ||
return isDEC(n_lst[1:]) | ||
|
||
def isINC(n_lst): | ||
if len(n_lst)== 1: | ||
return True | ||
if n_lst[0] > n_lst[1]: | ||
return False | ||
return isINC(n_lst[1:]) | ||
|
||
bouncy_count = 0 | ||
for i in range(1,2000001): | ||
if not isDECorINC(i): | ||
print str(i) + " is bouncy " | ||
bouncy_count += 1 | ||
else: | ||
print str(i) + ' is not bouncy' | ||
if i > 1500000: | ||
if bouncy_count / float(i) == 0.99: | ||
print i | ||
break | ||
|
||
print bouncy_count / 2000000.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# problem 166 | ||
|
||
def get_new_grid(): | ||
grid = [] | ||
for j in range(4): | ||
row = [] | ||
for k in range(4): | ||
row.append(0) | ||
grid.append(row) | ||
return grid | ||
|
||
for i in range(37): | ||
sum_ = i | ||
grid = get_new_grid() | ||
for row in grid: | ||
for ele in row: | ||
for j in range(10): | ||
grid[row] = j | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# problem 206 | ||
i = 1009950493 | ||
while True: | ||
|
||
print i | ||
isq = str(i ** 2) | ||
if len(isq) == 19: | ||
if isq[0] == '1': | ||
if isq[2] == '2': | ||
if isq[4] == '3': | ||
if isq[6] == '4': | ||
if isq[8] == '5': | ||
if isq[10] == '6': | ||
if isq[12] == '7': | ||
if isq[14] == '8': | ||
if isq[16] == '9': | ||
if isq[18] == '0': | ||
print "-----------" | ||
print i | ||
print isq | ||
break | ||
i += 1 | ||
if i == 1389026623: | ||
break | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# problem 213 | ||
# http://projecteuler.net/thread=213 | ||
#First? I just calculated the probability of each flee being in each square after 50 rounds, and #summed the probability of each square being empty based on those probabilities. Runs in a little #under 4 seconds on my machine. | ||
'''import random | ||
trials = 10001 | ||
totalcount = 0 | ||
for k in range(trials): | ||
grid = [] | ||
for i in range(30): | ||
row = [] | ||
for j in range(30): | ||
row.append(1) | ||
grid.append(row) | ||
bells = 50 | ||
for i in range(bells): | ||
for row in range(30): | ||
for col in range(30): | ||
ran = random.random() | ||
grid[row][col] -=1 | ||
if row == 0: | ||
if col == 0: | ||
if ran < 1.0 / 2: | ||
grid[row+1][col] += 1 | ||
else: | ||
grid[row][col+1] += 1 | ||
if col == 29: | ||
if ran < 1.0 / 2: | ||
grid[row+1][col] += 1 | ||
else: | ||
grid[row][col-1] += 1 | ||
else: | ||
if ran < 1.0 / 3: | ||
grid[row][col+1] += 1 | ||
elif ran < 2.0 /3: | ||
grid[row][col-1] += 1 | ||
else: | ||
grid[row+1][col] += 1 | ||
elif row == 29: | ||
if col == 0: | ||
if ran < 1.0 / 2: | ||
grid[row-1][col] += 1 | ||
else: | ||
grid[row][col+1] += 1 | ||
if col == 29: | ||
if ran < 1.0 / 2: | ||
grid[row-1][col] += 1 | ||
else: | ||
grid[row][col-1] += 1 | ||
else: | ||
if ran < 1.0 / 3: | ||
grid[row][col-1] += 1 | ||
elif ran < 2.0 /3: | ||
grid[row][col+1] += 1 | ||
else: | ||
grid[row-1][col] += 1 | ||
else: | ||
if col == 0: | ||
if ran < 1.0 / 3: | ||
grid[row-1][col] += 1 | ||
elif ran < 2.0 / 3: | ||
grid[row][col+1] += 1 | ||
else: | ||
grid[row+1][col] += 1 | ||
if col == 29: | ||
if ran < 1.0 / 3: | ||
grid[row-1][col] += 1 | ||
elif ran < 2.0 / 3: | ||
grid[row][col-1] += 1 | ||
else: | ||
grid[row+1][col] += 1 | ||
else: | ||
if ran < 1.0 / 4: | ||
grid[row][col-1] += 1 | ||
elif ran < 2.0 /4: | ||
grid[row-1][col] += 1 | ||
elif ran < 3.0 /4: | ||
grid[row][col+1] += 1 | ||
else: | ||
grid[row+1][col] += 1 | ||
count = 0 | ||
for i in range(30): | ||
for j in range(30): | ||
if grid[i][j] == 0: | ||
count += 1 | ||
print count | ||
totalcount += count | ||
print float(totalcount ) / trials''' | ||
|
||
# --- | ||
grid = [] | ||
dx = [-1,1,0,0] | ||
dy = [0,0,1,-1] | ||
for i in range(900): | ||
mat0 = [] | ||
mat1 = [] | ||
for j in range(30): | ||
row0 = [] | ||
row1 = [] | ||
for k in range(30): | ||
row0.append(0.0) | ||
row1.append(0.0) | ||
mat0.append(row0) | ||
mat1.append(row1) | ||
grid.append([mat0,mat1]) | ||
for f in range(900): | ||
print f | ||
grid[f][0][f%30][f/30] = 1.0; | ||
for r in range(1,51): | ||
cur = r % 2 | ||
prev = 1 - cur | ||
newmat = [] | ||
for j in range(30): | ||
row = [] | ||
for k in range(30): | ||
row.append(0.0) | ||
newmat.append(row) | ||
grid[f][cur] = newmat | ||
for x in range(30): | ||
for y in range(30): | ||
tot = 4 | ||
if x == 0 or x == 29: | ||
tot -= 1 | ||
if y == 0 or y == 29: | ||
tot -= 1 | ||
for d in range(4): | ||
nx = x + dx[d] | ||
ny = y + dy[d] | ||
if nx < 0 or ny < 0 or nx >= 30 or ny >= 30: | ||
continue | ||
#print grid[f][prev][x][y] / float(tot) | ||
grid[f][cur][nx][ny] += grid[f][prev][x][y] / float(tot) | ||
ret = 0.0 | ||
for x in range(30): | ||
for y in range(30): | ||
p = 1.0 | ||
for f in range(900): | ||
p *= 1 - grid[f][0][x][y] | ||
print p | ||
ret += p | ||
|
||
|
||
print ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# problem 243 | ||
#http://blog.dreamshire.com/2012/12/19/project-euler-problem-243-solution/ | ||
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31] | ||
ratio = 15499/94744.0 | ||
|
||
def denom(ratio): | ||
d, s = 1, 1 | ||
for p in primes: | ||
d *= p | ||
s *= p-1 | ||
for i in range(2, p): | ||
if s*i / (d*i-1.) < ratio: | ||
return d*i | ||
return "Please buy more primes!" | ||
|
||
print 'Answer to PE243 = ',denom(ratio) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# problem 64 | ||
# http://www.mathblog.dk/project-euler-continued-fractions-odd-period | ||
import math | ||
|
||
def loop_length(n): | ||
m = 0.0 | ||
d = 1.0 | ||
a = math.floor(math.sqrt(n)) | ||
S = float(n) | ||
param_lst = [[m,d,a]] | ||
loop_len = 0 | ||
for i in range(1000): | ||
m1 = d * a - m | ||
# print "m: " + str(m1) | ||
d1 = (S - m1 ** 2) / d | ||
# print "d: " + str(d1) | ||
a1 = math.floor( (math.sqrt(S) + m1) / float(d1) ) | ||
# print "a: " + str(a1) | ||
if [m1,d1,a1] in param_lst: | ||
# why we can instread check a1 == 2 * a0 here? | ||
return len(param_lst) - param_lst.index([m1,d1,a1]) | ||
d = d1 | ||
a = a1 | ||
m = m1 | ||
param_lst.append([m,d,a]) | ||
|
||
count= 0 | ||
|
||
for i in range(2,10001): | ||
if math.sqrt(i) % 1 != 0: | ||
#print "i: " + str(i) | ||
lop_len = loop_length(i) | ||
if lop_len % 2 == 1: | ||
count += 1 | ||
print count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# problem 70 | ||
# http://www.mathblog.dk/project-euler-70-investigate-values-of-n-for-which-%CF%86n-is-a-permutation-of-n/ | ||
|
||
phi_best = 1 | ||
best = 1 | ||
best_ratio = 100000000 | ||
|
||
|
||
def is_prime(n): | ||
for i in range(2,n): | ||
if n % i == 0: | ||
return False | ||
return True | ||
|
||
limit = 10000000 | ||
prime_lst = [] | ||
for n in range(2000,5001): | ||
if is_prime(n): | ||
prime_lst.append(n) | ||
|
||
for i in prime_lst: | ||
for j in prime_lst: | ||
n = i * j | ||
if n > limit: | ||
break | ||
phi = (i - 1) * (j-1) | ||
ratio = float(n) / phi | ||
n_sort = list(str(n)) | ||
n_sort.sort() | ||
phi_sort = list(str(phi)) | ||
phi_sort.sort() | ||
if phi_sort == n_sort and best_ratio > ratio: | ||
best = n | ||
phi_best = phi | ||
best_ratio = ratio | ||
|
||
|
||
print "best: "+ str(best) | ||
print "phi_best: " + str(phi_best) | ||
print "best_ratio: " + str(best_ratio) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# problem 74 | ||
def factorial(n): | ||
if n == 0: | ||
return 1 | ||
elif n == 1: | ||
return 1 | ||
elif n == 2: | ||
return 2 | ||
elif n == 3: | ||
return 6 | ||
elif n == 4: | ||
return 24 | ||
elif n == 5: | ||
return 120 | ||
elif n == 6: | ||
return 720 | ||
elif n == 7: | ||
return 5040 | ||
elif n == 8: | ||
return 40320 | ||
elif n == 9: | ||
return 362880 | ||
#len_lst = [] | ||
total_count = 0 | ||
for i in range(1,1000000): | ||
print "-----------" | ||
print i | ||
dd = i | ||
sum_ = 0 | ||
lst = [] | ||
while True: | ||
sum_ = 0 | ||
for d in str(dd): | ||
sum_ = sum_ + factorial(int(d)) | ||
if sum_ in lst: | ||
#len_lst.append(len(lst)) | ||
if i == 78: | ||
print "--------" | ||
print lst | ||
print "88888" | ||
if len(lst) == 59: | ||
total_count += 1 | ||
print "-->>>>>" + str(i) | ||
break | ||
lst.append(sum_) | ||
dd = sum_ | ||
print total_count | ||
|
||
#print "max: " +str( max(len_lst) ) | ||
|
||
|
||
|
Oops, something went wrong.