From 84e85ffb25e7c3941cca28090ea7f919fea257ab Mon Sep 17 00:00:00 2001 From: phong3112 Date: Wed, 7 Sep 2016 23:10:55 +0900 Subject: [PATCH] first 10 problem of project-eulers --- problem001.py | 11 +++++++++++ problem002.py | 16 ++++++++++++++++ problem003.py | 16 ++++++++++++++++ problem004.py | 10 ++++++++++ problem005.py | 23 +++++++++++++++++++++++ problem006.py | 8 ++++++++ problem007.py | 24 ++++++++++++++++++++++++ problem008.py | 23 +++++++++++++++++++++++ problem009.py | 13 +++++++++++++ problem010.py | 21 +++++++++++++++++++++ 10 files changed, 165 insertions(+) create mode 100644 problem001.py create mode 100644 problem002.py create mode 100644 problem003.py create mode 100644 problem004.py create mode 100644 problem005.py create mode 100644 problem006.py create mode 100644 problem007.py create mode 100644 problem008.py create mode 100644 problem009.py create mode 100644 problem010.py diff --git a/problem001.py b/problem001.py new file mode 100644 index 0000000..777122b --- /dev/null +++ b/problem001.py @@ -0,0 +1,11 @@ +# coding: utf-8 +s = 0 +for i in range(1000): + #print i, + if (i % 3 == 0): + s = s + i + if (i % 5 == 0): + s = s + i + if (i % 15 == 0): + s = s - i +print s diff --git a/problem002.py b/problem002.py new file mode 100644 index 0000000..a924906 --- /dev/null +++ b/problem002.py @@ -0,0 +1,16 @@ +#coding: utf-8 + +c = [1] +a,b = 1,2 +s = 0 +while b <4000000: + a,b = b, a+b + c.append(a) +print c +print len(c) +for i in range(len(c)): + if c[i] % 2 == 0: + s = s + c[i] +print s + + diff --git a/problem003.py b/problem003.py new file mode 100644 index 0000000..41ab12a --- /dev/null +++ b/problem003.py @@ -0,0 +1,16 @@ +#coding: utf-8 + +import math + + +def prime_factors(n): + factors = [] + d = 2 + while n > 1: + while n % d == 0: + factors.append(d) + n /= d + d += 1 + return factors +print prime_factors(600851475143) +print max(prime_factors(600851475143)) diff --git a/problem004.py b/problem004.py new file mode 100644 index 0000000..2bed140 --- /dev/null +++ b/problem004.py @@ -0,0 +1,10 @@ +# coding: utf-8 +c = [] +for i in range(999): + for j in range(999): + a = i * j + b = a % 1000 + d = a / 1000 + if (str(d) == str(b)[::-1]): + c.append(a) +print max(c) diff --git a/problem005.py b/problem005.py new file mode 100644 index 0000000..7113e7e --- /dev/null +++ b/problem005.py @@ -0,0 +1,23 @@ +#coding: utf-8 + +def ucln(a, b): + while b: + r = a % b + a, b = b, r + return a +def bcnn(a, b): + bcnn1 = a * b / ucln(a, b) + return bcnn1 + +lists = list(range(2,21)) + +bcnn1 = bcnn(lists[0], lists[1]) + +for i in range(2,len(lists)): + bcnn1 = bcnn(bcnn1, lists[i]) + +print bcnn1 + + + + diff --git a/problem006.py b/problem006.py new file mode 100644 index 0000000..0097963 --- /dev/null +++ b/problem006.py @@ -0,0 +1,8 @@ +#coding: utf-8 + +SOQ = 0 +QOS = 0 +for i in range(101): + SOQ = SOQ + i*i + QOS = QOS + i +print QOS*QOS-SOQ diff --git a/problem007.py b/problem007.py new file mode 100644 index 0000000..c28f3bd --- /dev/null +++ b/problem007.py @@ -0,0 +1,24 @@ +#coding: utf-8 + +import time + +def fast_nth_prime(n, limit = 125000): + if limit % 2 != 0: + limit += 1 + primes = [True] * limit + primes[0],primes[1] = [None, None] + count = 0 + for ind,val in enumerate(primes): + if val: + primes[ind*2::ind] = [False] * ((limit -1)//ind - 1) + count += 1 + if count == n: + return ind + return False + + +start = time.time() +prime = fast_nth_prime(10001) +elapsed = (time.time() - start) + +print "found %s in %s seconds." % (prime,elapsed) diff --git a/problem008.py b/problem008.py new file mode 100644 index 0000000..dc80eb5 --- /dev/null +++ b/problem008.py @@ -0,0 +1,23 @@ +#coding: utf-8 + +import time +start = time.time() +s = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450" + +P = [] + +for i in range(0, len(s) - 13): + + product = 1 + + for j in range(i, i + 13): + product *= int(s[j]) + P.append(product) + +elapsed = (time.time() - start) +print max(P) +print elapsed + + + + diff --git a/problem009.py b/problem009.py new file mode 100644 index 0000000..40dbb70 --- /dev/null +++ b/problem009.py @@ -0,0 +1,13 @@ +#coding: utf-8 + +from math import sqrt +import time +start = time.time() + +for b in range(500): + for a in range(b): + c = sqrt(a*a + b*b) + if a + b + c == 1000: + print a*b*c + break +print time.time() - start diff --git a/problem010.py b/problem010.py new file mode 100644 index 0000000..6aaaf15 --- /dev/null +++ b/problem010.py @@ -0,0 +1,21 @@ +#coding: utf-8 + +import time +start = time.time() + +def prime(limit): + prime_num = [] + primes = [True] * limit + primes[0],primes[1] = [None, None] + for ind,val in enumerate(primes): + if val: + primes[ind*2::ind] = [False] * ((limit -1)//ind - 1) + prime_num.append(ind) + return prime_num + +a = prime(2000000) +s = 0 +for i in a: + s += i +print s, time.time()-start +