Skip to content

Commit d1961f2

Browse files
committed
update pe and spoj
1 parent 1370192 commit d1961f2

File tree

15 files changed

+612
-0
lines changed

15 files changed

+612
-0
lines changed

ProjectEuler/Note~

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
64: finding the period of convergent of sqrt (n).
2+
65: convergent continued fractions.
3+
66: pell equation.

ProjectEuler/p002.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import sys
2+
s , x, x1, x2= [0, 0, 1, 0];
3+
while x <= 4000000:
4+
x = x1 + x2;
5+
if x <= 4000000 and x % 2 == 0:
6+
s += x;
7+
x2 = x1; x1 = x;
8+
print (s);

ProjectEuler/p003.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys;
2+
3+
pm = [];
4+
st = [True]*1000001;
5+
6+
pm.append (2);
7+
for i in xrange (4, 1000001, 2):
8+
st[i] = False;
9+
for i in xrange (3, 1000001, 2):
10+
if st[i] == True:
11+
pm.append (i);
12+
for j in range (long(i * i), 1000001L, i):
13+
st[j] = False;
14+
15+
while True:
16+
n = input ();
17+
ans = 2;
18+
for x in pm:
19+
while n % x == 0:
20+
n /= x;
21+
ans = x;
22+
if n != 1:
23+
ans = n;
24+
print ans;
25+
26+
27+
28+
29+
30+

ProjectEuler/p004.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ans = -1;
2+
for i in range (999, 99, -1):
3+
for j in range (999, 99, -1):
4+
t = i * j;
5+
s = str(t);
6+
if s == s[::-1] and t > ans:
7+
ans = t;
8+
print (ans);
9+
10+
11+

ProjectEuler/p005.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def gcd (x, y):
2+
r = x % y;
3+
while r != 0:
4+
x = y;
5+
y = r;
6+
r = x % y;
7+
return y
8+
9+
ans = 1;
10+
for i in range (2, 21):
11+
ans = i * ans / gcd (i, ans);
12+
13+
print (ans);

ProjectEuler/p006.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a = 0; b = 0;
2+
for i in range (101):
3+
a += i**2;
4+
b += i;
5+
print (b**2 - a);

ProjectEuler/p048.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
md = 10000000000;
2+
ans = 0;
3+
for i in range (1, 1001):
4+
ans += i ** i;
5+
print (ans % md);

ProjectEuler/p052.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def chk (x) :
2+
lst = [];
3+
for i in xrange (2, 7):
4+
lst.append (''.join(sorted (str (i * x))))
5+
ans = True;
6+
7+
for i in xrange (0, 5):
8+
if i < 4 and lst[i] != lst[i+1]:
9+
ans = False;
10+
break
11+
return ans;
12+
13+
for i in xrange (1, 10000000000):
14+
if chk (i):
15+
ans = i;
16+
break
17+
18+
print ans;
19+

ProjectEuler/p064.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import math
2+
def Pell (n, a0):
3+
4+
p0, p1, q0, q1 = 0, 1, 1, 0;
5+
g, h = 0, 1;
6+
a = a0;
7+
while True:
8+
p = a * p1 + p0;
9+
q = a * q1 + q0;
10+
if p * p - n * q * q == 1:
11+
return p, q
12+
13+
p0 = p1; p1 = p;
14+
q0 = q1; q1 = q;
15+
16+
g = -g + a * h;
17+
h = (n - g * g) / h;
18+
a = (g + a0) // h;
19+
20+
# calculate the period of the continued fractions of sqrt (n)
21+
def CalCFPeriod (n):
22+
rt = math.sqrt (n);
23+
a0 = int (rt);
24+
p0, p1, q0, q1 = 0, 1, 1, 0;
25+
g, h = 0, 1;
26+
a, cnt = a0, 0;
27+
28+
while True:
29+
p, q = a * p1 + p0, a * q1 + q0;
30+
p0 = p1; p1 = p; q0 = q1; q1 = q;
31+
g = a * h - g; h = (n - g * g) / h;
32+
cnt += 1;
33+
a = (g + a0) // h;
34+
t = (g + rt) / h;
35+
if cnt == 1:
36+
num = t * t;
37+
elif t * t == num:
38+
return cnt - 1
39+
40+
# How to compute CF for any irrational number to arbitrary precision ?
41+
42+
def ProjectEuler66 ():
43+
ma = -1; ans = -1;
44+
for n in xrange (1, 1001):
45+
a0 = int (math.floor (math.sqrt (n)));
46+
if a0 * a0 == n:
47+
continue
48+
49+
x, y = Pell (n, a0);
50+
print n, x;
51+
if x > ma:
52+
ma = x;
53+
ans = n;
54+
55+
print ans;
56+
57+
def ProjectEuler65 ():
58+
v = math.e
59+
a0 = int (v);
60+
61+
p0, p1, q0, q1 = 1, a0, 0, 1;
62+
k = 0;
63+
for i in xrange (1, 100):
64+
a = 1;
65+
if i % 3 == 2:
66+
k += 2;
67+
a = k;
68+
69+
p , q = a * p1 + p0, a * q1 + q0;
70+
p0 = p1; p1 = p; q0 = q1; q1 = q;
71+
72+
k = 0; x = int (p);
73+
while x != 0:
74+
k += x % 10;
75+
x //= 10;
76+
print k
77+
78+
79+
80+
def ProjectEuler64 ():
81+
ans = 0;
82+
for i in xrange (1, 10001):
83+
rt = int (math.sqrt (i));
84+
if rt * rt == i:
85+
continue
86+
x = CalCFPeriod (i);
87+
if x & 1:
88+
ans += 1
89+
print ans
90+
91+
if __name__ == "__main__":
92+
ProjectEuler64 ();
93+
94+
95+
96+
97+
98+

ProjectEuler/p065.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import math
2+
def Pell (n, a0):
3+
4+
p0, p1, q0, q1 = 0, 1, 1, 0;
5+
g, h = 0, 1;
6+
a = a0;
7+
while True:
8+
p = a * p1 + p0;
9+
q = a * q1 + q0;
10+
if p * p - n * q * q == 1:
11+
return p, q
12+
13+
p0 = p1; p1 = p;
14+
q0 = q1; q1 = q;
15+
16+
g = -g + a * h;
17+
h = (n - g * g) / h;
18+
a = (g + a0) // h;
19+
20+
21+
# How to compute CF for any irrational number to arbitrary precision ?
22+
23+
def ProjectEuler66 ():
24+
ma = -1; ans = -1;
25+
for n in xrange (1, 1001):
26+
a0 = int (math.floor (math.sqrt (n)));
27+
if a0 * a0 == n:
28+
continue
29+
30+
x, y = Pell (n, a0);
31+
print n, x;
32+
if x > ma:
33+
ma = x;
34+
ans = n;
35+
36+
print ans;
37+
38+
def ProjectEuler65 ():
39+
v = math.e
40+
a0 = int (v);
41+
42+
p0, p1, q0, q1 = 1, a0, 0, 1;
43+
k = 0;
44+
for i in xrange (1, 100):
45+
a = 1;
46+
if i % 3 == 2:
47+
k += 2;
48+
a = k;
49+
50+
p , q = a * p1 + p0, a * q1 + q0;
51+
p0 = p1; p1 = p; q0 = q1; q1 = q;
52+
53+
k = 0; x = int (p);
54+
while x != 0:
55+
k += x % 10;
56+
x //= 10;
57+
print k
58+
59+
if __name__ == "__main__":
60+
ProjectEuler65 ();
61+
62+
63+
64+
65+
66+

ProjectEuler/p066.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import math
2+
def Pell (n, a0):
3+
4+
p0, p1, q0, q1 = 0, 1, 1, 0;
5+
g, h = 0, 1;
6+
a = a0;
7+
while True:
8+
p = a * p1 + p0;
9+
q = a * q1 + q0;
10+
if p * p - n * q * q == 1:
11+
return p, q
12+
13+
p0 = p1; p1 = p;
14+
q0 = q1; q1 = q;
15+
16+
g = -g + a * h;
17+
h = (n - g * g) / h;
18+
a = (g + a0) // h;
19+
20+
21+
# How to compute CF for any irrational number to arbitrary precision ?
22+
23+
def ProjectEuler66 ():
24+
ma = -1; ans = -1;
25+
for n in xrange (1, 1001):
26+
a0 = int (math.floor (math.sqrt (n)));
27+
if a0 * a0 == n:
28+
continue
29+
30+
x, y = Pell (n, a0);
31+
print n, x;
32+
if x > ma:
33+
ma = x;
34+
ans = n;
35+
36+
print ans;
37+
38+
def ProjectEuler65 ():
39+
v = math.e
40+
a0 = int (v);
41+
42+
p0, p1, q0, q1 = 1, a0, 0, 1;
43+
k = 0;
44+
for i in xrange (1, 100):
45+
a = 1;
46+
if i % 3 == 2:
47+
k += 2;
48+
a = k;
49+
50+
p , q = a * p1 + p0, a * q1 + q0;
51+
p0 = p1; p1 = p; q0 = q1; q1 = q;
52+
53+
k = 0; x = int (p);
54+
while x != 0:
55+
k += x % 10;
56+
x //= 10;
57+
print k
58+
59+
if __name__ == "__main__":
60+
ProjectEuler65 ();
61+
62+
63+
64+
65+
66+

ProjectEuler/p081.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import urllib
2+
3+
fin = urllib.urlopen ('http://projecteuler.net/project/matrix.txt');
4+
a = [];
5+
for cnt in xrange (80):
6+
a.append (map (int, fin.readline().split (',')))
7+
8+
d = [ [100000000000000 for i in xrange (81)] for j in xrange (81)];
9+
d[0][1] = 0;
10+
for i in xrange (1, 81):
11+
for j in xrange (1, 81):
12+
d[i][j] = min (d[i][j - 1], d[i - 1][j]) + a[i-1][j-1];
13+
print d[80][80];
14+

0 commit comments

Comments
 (0)