diff --git a/Project Euler/Problem 01/sol1.py b/Project Euler/Problem 01/sol1.py new file mode 100644 index 000000000..512154e29 --- /dev/null +++ b/Project Euler/Problem 01/sol1.py @@ -0,0 +1,12 @@ +''' +Problem Statement: +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3,5,6 and 9. The sum of these multiples is 23. +Find the sum of all the multiples of 3 or 5 below N. +''' +n = int(raw_input().strip()) +sum=0; +for a in range(3,n): + if(a%3==0 or a%5==0): + sum+=a +print sum; \ No newline at end of file diff --git a/Project Euler/Problem 01/sol2.py b/Project Euler/Problem 01/sol2.py new file mode 100644 index 000000000..5e368c220 --- /dev/null +++ b/Project Euler/Problem 01/sol2.py @@ -0,0 +1,15 @@ +''' +Problem Statement: +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3,5,6 and 9. The sum of these multiples is 23. +Find the sum of all the multiples of 3 or 5 below N. +''' +n = int(raw_input().strip()) +sum = 0 +terms = (n-1)/3 +sum+= ((terms)*(6+(terms-1)*3))/2 #sum of an A.P. +terms = (n-1)/5 +sum+= ((terms)*(10+(terms-1)*5))/2 +terms = (n-1)/15 +sum-= ((terms)*(30+(terms-1)*15))/2 +print sum \ No newline at end of file diff --git a/Project Euler/Problem 01/sol3.py b/Project Euler/Problem 01/sol3.py new file mode 100644 index 000000000..0caa30a53 --- /dev/null +++ b/Project Euler/Problem 01/sol3.py @@ -0,0 +1,42 @@ +''' +Problem Statement: +If we list all the natural numbers below 10 that are multiples of 3 or 5, +we get 3,5,6 and 9. The sum of these multiples is 23. +Find the sum of all the multiples of 3 or 5 below N. +''' +''' +This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3. +''' +n = int(raw_input().strip()) +sum=0; +num=0; +while(1): + num+=3 + if(num>=n): + break + sum+=num + num+=2 + if(num>=n): + break + sum+=num + num+=1 + if(num>=n): + break + sum+=num + num+=3 + if(num>=n): + break + sum+=num + num+=1 + if(num>=n): + break + sum+=num + num+=2 + if(num>=n): + break + sum+=num + num+=3 + if(num>=n): + break + sum+=num +print sum; \ No newline at end of file diff --git a/Project Euler/Problem 02/sol1.py b/Project Euler/Problem 02/sol1.py new file mode 100644 index 000000000..6cf520767 --- /dev/null +++ b/Project Euler/Problem 02/sol1.py @@ -0,0 +1,18 @@ +''' +Problem: +Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, +the first 10 terms will be: + 1,2,3,5,8,13,21,34,55,89,.. +By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms. +e.g. for n=10, we have {2,8}, sum is 10. +''' + +n = int(raw_input().strip()) +i=1; j=2; sum=0 +while(j<=n): + if((j&1)==0): #can also use (j%2==0) + sum+=j + temp=i + i=j + j=temp+i +print sum \ No newline at end of file diff --git a/Project Euler/Problem 03/sol1.py b/Project Euler/Problem 03/sol1.py new file mode 100644 index 000000000..bd3e237e7 --- /dev/null +++ b/Project Euler/Problem 03/sol1.py @@ -0,0 +1,38 @@ +''' +Problem: +The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? +e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +''' + +import math + +def isprime(no): + if(no==2): + return True + elif (no%2==0): + return False + sq = int(math.sqrt(no))+1 + for i in range(3,sq,2): + if(no%i==0): + return False + return True + +max=0 +n=int(input()) +if(isprime(n)): + print n +else: + while (n%2==0): + n=n/2 + if(isprime(n)): + print n + else: + n1 = int(math.sqrt(n))+1 + for i in range(3,n1,2): + if(n%i==0): + if(isprime(n/i)): + max=n/i + break + elif(isprime(i)): + max=i + print max diff --git a/Project Euler/Problem 03/sol2.py b/Project Euler/Problem 03/sol2.py new file mode 100644 index 000000000..2577892c4 --- /dev/null +++ b/Project Euler/Problem 03/sol2.py @@ -0,0 +1,16 @@ +''' +Problem: +The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? +e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17. +''' +n=int(input()) +prime=1 +i=2 +while(i*i<=n): + while(n%i==0): + prime=i + n/=i + i+=1 +if(n>1): + prime=n +print prime diff --git a/Project Euler/Problem 04/sol1.py b/Project Euler/Problem 04/sol1.py new file mode 100644 index 000000000..f8ed832d8 --- /dev/null +++ b/Project Euler/Problem 04/sol1.py @@ -0,0 +1,15 @@ +''' +Problem: +A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. +Find the largest palindrome made from the product of two 3-digit numbers which is less than N. +''' +n=int(input()) +for i in range(n-1,10000,-1): + temp=str(i) + if(temp==temp[::-1]): + j=999 + while(j!=99): + if((i%j==0) and (len(str(i/j))==3)): + print i + exit(0) + j-=1 diff --git a/Project Euler/Problem 04/sol2.py b/Project Euler/Problem 04/sol2.py new file mode 100644 index 000000000..4d2006242 --- /dev/null +++ b/Project Euler/Problem 04/sol2.py @@ -0,0 +1,18 @@ +''' +Problem: +A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99. +Find the largest palindrome made from the product of two 3-digit numbers which is less than N. +''' +arr = [] +for i in range(999,100,-1): + for j in range(999,100,-1): + t = str(i*j) + if t == t[::-1]: + arr.append(i*j) +arr.sort() + +n=int(input()) +for i in arr[::-1]: + if(i