Wednesday, 4 December 2013

Project Euler Problem 10 - Solved

Q) The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

Solution:

size = 2000000
num = [True] * (size+1)

def Prime():
    for i in xrange(2,((size/2))):
        if num[i]:
            for j in xrange(i*2,size+1,i):
                num[j] = False
    ans = sum((i for i in xrange(size+1) if num[i]))               
    return ans-1

print Prime()




Project Euler Problem 9 - Solved

Q) A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Solution:

Python: One Liner:

[(c*b*(1000-c-b)) for c in range(334,500)for b in range(250,500)if(1000-c-b)**2+b**2==c**2]


 Description:
 ===================================================
limits are decided based on following:
1) a+b+c = 1000
2) a**2 + b**2 = c**2
3) a + b > c
- a/b can't be 0 or 1 (as a<c,b<c also if a/b = 1, a+b can not be greater than c)
- c < 500
- a+b > 500
4) a<b<c
- a < 250
- b > 250 (as (a+ b) > 500)
- c > 333
- b < 500 (lesser than max of c)

Project Euler Problem 8 - Solved

Q) Find the greatest product of five consecutive digits in the 1000-digit number.

Solution:

num = """
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
""".replace('\n','').strip()

list1 = []
for i in xrange(len(num) - 4):
    list1.append(num[i:i+5])

ans = []
for i in list1:
    res1 = reduce(lambda x,y:int(x)*int(y),i)
    ans.append(res1)
print max(ans)

Project Euler Problem 7 - Solved

Q) By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?

Solution: Python:
=============== from math import sqrt db= [1,2,3] def Prime(x): num = 5 while True: num1 = sqrt(num) try: ans = db[x] break except IndexError: for i in db[2:]: if not(num%i): break elif i>num1: break if (num%i): db.append(num) num += 2 return ans print Prime(10001)

Project Euler Problem 6 - Solved

Q) The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


Solution: Python: ================= def SumSqr(n): x = (n*(n+1)/2)**2 - (n*(n+1))*((2*n+1)/6.0) return x print int(SumSqr(100))

Project Euler Problem 5 - Solved

Q) 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Solution: Python: Quite Fast
========================== num = 20 def GCD1(num1,num2): if num1>num2: num1,num2 = num2,num1 x = [m for m in range(2,num1+1) if not (num1%m)] y = [] for n in range(num1,1,-1): if not(num2%n)and (n in x): y.append(n) return y[0] def GCD(num1,num2): if num1>num2: return GCD1(num1-num2,num2) elif num2>num1: return GCD1(num1, num2-num1) else: return num1 def LCM(num1,num2): x = abs(num1*num2) y = GCD(num1,num2) if y == None: ans = x else: ans = x/y return ans def SmallMul(): k = ans = num for i in range(k,k/2,-1): ans = LCM(ans,i) return ans print SmallMul()

Project Euler Problem 4 - Solved

Q) A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 ×99.
Find the largest palindrome made from the product of two 3-digit numbers.

Solution: Python
============= def isPalindrome(x): if len(x)<=1: return True else: return (x[0] == x[-1]) and isPalindrome(x[1:-1]) db = [0] def TestPalindrome(): x = 1 y = 1 for i in range(999,100,-1): for j in range(999,100,-1): ans = str(i*j) if isPalindrome(ans): db.append(int(ans)) x,y = i,j break elif max(i,j)<min(x,y): return max(db) print TestPalindrome()