mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Merge pull request #192 from sachinarora707/master
Project Euler Solutions Added.
This commit is contained in:
commit
dc5c5768e0
12
Project Euler/Problem 01/sol1.py
Normal file
12
Project Euler/Problem 01/sol1.py
Normal file
@ -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;
|
15
Project Euler/Problem 01/sol2.py
Normal file
15
Project Euler/Problem 01/sol2.py
Normal file
@ -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
|
42
Project Euler/Problem 01/sol3.py
Normal file
42
Project Euler/Problem 01/sol3.py
Normal file
@ -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;
|
18
Project Euler/Problem 02/sol1.py
Normal file
18
Project Euler/Problem 02/sol1.py
Normal file
@ -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
|
38
Project Euler/Problem 03/sol1.py
Normal file
38
Project Euler/Problem 03/sol1.py
Normal file
@ -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
|
16
Project Euler/Problem 03/sol2.py
Normal file
16
Project Euler/Problem 03/sol2.py
Normal file
@ -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
|
15
Project Euler/Problem 04/sol1.py
Normal file
15
Project Euler/Problem 04/sol1.py
Normal file
@ -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
|
18
Project Euler/Problem 04/sol2.py
Normal file
18
Project Euler/Problem 04/sol2.py
Normal file
@ -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<n):
|
||||||
|
print i
|
||||||
|
exit(0)
|
20
Project Euler/Problem 05/sol1.py
Normal file
20
Project Euler/Problem 05/sol1.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
'''
|
||||||
|
Problem:
|
||||||
|
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(divisible with no remainder) by all of the numbers from 1 to N?
|
||||||
|
'''
|
||||||
|
|
||||||
|
n = int(input())
|
||||||
|
i = 0
|
||||||
|
while 1:
|
||||||
|
i+=n*(n-1)
|
||||||
|
nfound=0
|
||||||
|
for j in range(2,n):
|
||||||
|
if (i%j != 0):
|
||||||
|
nfound=1
|
||||||
|
break
|
||||||
|
if(nfound==0):
|
||||||
|
if(i==0):
|
||||||
|
i=1
|
||||||
|
print i
|
||||||
|
break
|
19
Project Euler/Problem 06/sol1.py
Normal file
19
Project Euler/Problem 06/sol1.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
Problem:
|
||||||
|
The sum of the squares of the first ten natural numbers is,
|
||||||
|
1^2 + 2^2 + ... + 10^2 = 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 N natural numbers and the square of the sum.
|
||||||
|
'''
|
||||||
|
|
||||||
|
suma = 0
|
||||||
|
sumb = 0
|
||||||
|
n = int(input())
|
||||||
|
for i in range(1,n+1):
|
||||||
|
suma += i**2
|
||||||
|
sumb += i
|
||||||
|
sum = sumb**2 - suma
|
||||||
|
print sum
|
15
Project Euler/Problem 06/sol2.py
Normal file
15
Project Euler/Problem 06/sol2.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
Problem:
|
||||||
|
The sum of the squares of the first ten natural numbers is,
|
||||||
|
1^2 + 2^2 + ... + 10^2 = 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 N natural numbers and the square of the sum.
|
||||||
|
'''
|
||||||
|
n = int(input())
|
||||||
|
suma = n*(n+1)/2
|
||||||
|
suma **= 2
|
||||||
|
sumb = n*(n+1)*(2*n+1)/6
|
||||||
|
print suma-sumb
|
29
Project Euler/Problem 07/sol1.py
Normal file
29
Project Euler/Problem 07/sol1.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
'''
|
||||||
|
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 Nth prime number?
|
||||||
|
'''
|
||||||
|
from math import sqrt
|
||||||
|
def isprime(n):
|
||||||
|
if (n==2):
|
||||||
|
return True
|
||||||
|
elif (n%2==0):
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
sq = int(sqrt(n))+1
|
||||||
|
for i in range(3,sq,2):
|
||||||
|
if(n%i==0):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
n = int(input())
|
||||||
|
i=0
|
||||||
|
j=1
|
||||||
|
while(i!=n and j<3):
|
||||||
|
j+=1
|
||||||
|
if (isprime(j)):
|
||||||
|
i+=1
|
||||||
|
while(i!=n):
|
||||||
|
j+=2
|
||||||
|
if(isprime(j)):
|
||||||
|
i+=1
|
||||||
|
print j
|
Loading…
Reference in New Issue
Block a user