Skip to content

Commit dc5c576

Browse files
authored
Merge pull request #192 from sachinarora707/master
Project Euler Solutions Added.
2 parents 07e8e25 + 7284714 commit dc5c576

File tree

12 files changed

+257
-0
lines changed

12 files changed

+257
-0
lines changed

Diff for: Project Euler/Problem 01/sol1.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'''
2+
Problem Statement:
3+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
4+
we get 3,5,6 and 9. The sum of these multiples is 23.
5+
Find the sum of all the multiples of 3 or 5 below N.
6+
'''
7+
n = int(raw_input().strip())
8+
sum=0;
9+
for a in range(3,n):
10+
if(a%3==0 or a%5==0):
11+
sum+=a
12+
print sum;

Diff for: Project Euler/Problem 01/sol2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Problem Statement:
3+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
4+
we get 3,5,6 and 9. The sum of these multiples is 23.
5+
Find the sum of all the multiples of 3 or 5 below N.
6+
'''
7+
n = int(raw_input().strip())
8+
sum = 0
9+
terms = (n-1)/3
10+
sum+= ((terms)*(6+(terms-1)*3))/2 #sum of an A.P.
11+
terms = (n-1)/5
12+
sum+= ((terms)*(10+(terms-1)*5))/2
13+
terms = (n-1)/15
14+
sum-= ((terms)*(30+(terms-1)*15))/2
15+
print sum

Diff for: Project Euler/Problem 01/sol3.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Problem Statement:
3+
If we list all the natural numbers below 10 that are multiples of 3 or 5,
4+
we get 3,5,6 and 9. The sum of these multiples is 23.
5+
Find the sum of all the multiples of 3 or 5 below N.
6+
'''
7+
'''
8+
This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3.
9+
'''
10+
n = int(raw_input().strip())
11+
sum=0;
12+
num=0;
13+
while(1):
14+
num+=3
15+
if(num>=n):
16+
break
17+
sum+=num
18+
num+=2
19+
if(num>=n):
20+
break
21+
sum+=num
22+
num+=1
23+
if(num>=n):
24+
break
25+
sum+=num
26+
num+=3
27+
if(num>=n):
28+
break
29+
sum+=num
30+
num+=1
31+
if(num>=n):
32+
break
33+
sum+=num
34+
num+=2
35+
if(num>=n):
36+
break
37+
sum+=num
38+
num+=3
39+
if(num>=n):
40+
break
41+
sum+=num
42+
print sum;

Diff for: Project Euler/Problem 02/sol1.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Problem:
3+
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2,
4+
the first 10 terms will be:
5+
1,2,3,5,8,13,21,34,55,89,..
6+
By considering the terms in the Fibonacci sequence whose values do not exceed n, find the sum of the even-valued terms.
7+
e.g. for n=10, we have {2,8}, sum is 10.
8+
'''
9+
10+
n = int(raw_input().strip())
11+
i=1; j=2; sum=0
12+
while(j<=n):
13+
if((j&1)==0): #can also use (j%2==0)
14+
sum+=j
15+
temp=i
16+
i=j
17+
j=temp+i
18+
print sum

Diff for: Project Euler/Problem 03/sol1.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
Problem:
3+
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
4+
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
5+
'''
6+
7+
import math
8+
9+
def isprime(no):
10+
if(no==2):
11+
return True
12+
elif (no%2==0):
13+
return False
14+
sq = int(math.sqrt(no))+1
15+
for i in range(3,sq,2):
16+
if(no%i==0):
17+
return False
18+
return True
19+
20+
max=0
21+
n=int(input())
22+
if(isprime(n)):
23+
print n
24+
else:
25+
while (n%2==0):
26+
n=n/2
27+
if(isprime(n)):
28+
print n
29+
else:
30+
n1 = int(math.sqrt(n))+1
31+
for i in range(3,n1,2):
32+
if(n%i==0):
33+
if(isprime(n/i)):
34+
max=n/i
35+
break
36+
elif(isprime(i)):
37+
max=i
38+
print max

Diff for: Project Euler/Problem 03/sol2.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'''
2+
Problem:
3+
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
4+
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
5+
'''
6+
n=int(input())
7+
prime=1
8+
i=2
9+
while(i*i<=n):
10+
while(n%i==0):
11+
prime=i
12+
n/=i
13+
i+=1
14+
if(n>1):
15+
prime=n
16+
print prime

Diff for: Project Euler/Problem 04/sol1.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Problem:
3+
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.
4+
Find the largest palindrome made from the product of two 3-digit numbers which is less than N.
5+
'''
6+
n=int(input())
7+
for i in range(n-1,10000,-1):
8+
temp=str(i)
9+
if(temp==temp[::-1]):
10+
j=999
11+
while(j!=99):
12+
if((i%j==0) and (len(str(i/j))==3)):
13+
print i
14+
exit(0)
15+
j-=1

Diff for: Project Euler/Problem 04/sol2.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Problem:
3+
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.
4+
Find the largest palindrome made from the product of two 3-digit numbers which is less than N.
5+
'''
6+
arr = []
7+
for i in range(999,100,-1):
8+
for j in range(999,100,-1):
9+
t = str(i*j)
10+
if t == t[::-1]:
11+
arr.append(i*j)
12+
arr.sort()
13+
14+
n=int(input())
15+
for i in arr[::-1]:
16+
if(i<n):
17+
print i
18+
exit(0)

Diff for: Project Euler/Problem 05/sol1.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Problem:
3+
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
4+
What is the smallest positive number that is evenly divisible(divisible with no remainder) by all of the numbers from 1 to N?
5+
'''
6+
7+
n = int(input())
8+
i = 0
9+
while 1:
10+
i+=n*(n-1)
11+
nfound=0
12+
for j in range(2,n):
13+
if (i%j != 0):
14+
nfound=1
15+
break
16+
if(nfound==0):
17+
if(i==0):
18+
i=1
19+
print i
20+
break

Diff for: Project Euler/Problem 06/sol1.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
Problem:
4+
The sum of the squares of the first ten natural numbers is,
5+
1^2 + 2^2 + ... + 10^2 = 385
6+
The square of the sum of the first ten natural numbers is,
7+
(1 + 2 + ... + 10)^2 = 552 = 3025
8+
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.
9+
Find the difference between the sum of the squares of the first N natural numbers and the square of the sum.
10+
'''
11+
12+
suma = 0
13+
sumb = 0
14+
n = int(input())
15+
for i in range(1,n+1):
16+
suma += i**2
17+
sumb += i
18+
sum = sumb**2 - suma
19+
print sum

Diff for: Project Euler/Problem 06/sol2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: utf-8 -*-
2+
'''
3+
Problem:
4+
The sum of the squares of the first ten natural numbers is,
5+
1^2 + 2^2 + ... + 10^2 = 385
6+
The square of the sum of the first ten natural numbers is,
7+
(1 + 2 + ... + 10)^2 = 552 = 3025
8+
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.
9+
Find the difference between the sum of the squares of the first N natural numbers and the square of the sum.
10+
'''
11+
n = int(input())
12+
suma = n*(n+1)/2
13+
suma **= 2
14+
sumb = n*(n+1)*(2*n+1)/6
15+
print suma-sumb

Diff for: Project Euler/Problem 07/sol1.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
By listing the first six prime numbers:
3+
2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
4+
What is the Nth prime number?
5+
'''
6+
from math import sqrt
7+
def isprime(n):
8+
if (n==2):
9+
return True
10+
elif (n%2==0):
11+
return False
12+
else:
13+
sq = int(sqrt(n))+1
14+
for i in range(3,sq,2):
15+
if(n%i==0):
16+
return False
17+
return True
18+
n = int(input())
19+
i=0
20+
j=1
21+
while(i!=n and j<3):
22+
j+=1
23+
if (isprime(j)):
24+
i+=1
25+
while(i!=n):
26+
j+=2
27+
if(isprime(j)):
28+
i+=1
29+
print j

0 commit comments

Comments
 (0)