Skip to content

Commit

Permalink
Upto Problem10
Browse files Browse the repository at this point in the history
  • Loading branch information
dheeru0198 committed Apr 12, 2012
1 parent 33cc43f commit 1e3bb1a
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 0 deletions.
12 changes: 12 additions & 0 deletions prob1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Project Euler
# Problem - 1
# Add all the natural numbers below one thousand that are multiples of 3 or 5.

s = 0
i = 3

while i < 1000 :
if (i%3 == 0 or i%5 == 0) :
s = s + i
i = i + 1
print "%d" % s
18 changes: 18 additions & 0 deletions prob10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Project Euler
# Problem - 8
# Calculate the sum of all the primes below two million.

from math import sqrt

s = 2

def is_prime(n) :
for j in range(2, int(sqrt(n)) + 1) :
if (n % j == 0) :
return 0
return 1

for i in range(3, 2000000) :
if is_prime(i) :
s = s + i
print s
20 changes: 20 additions & 0 deletions prob2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Project Euler
# Problem - 2
# By considering the terms in the Fibonacci sequence whose values do not exceed
# four million, find the sum of the even-valued terms.

# Each new term in the Fibonacci sequence is generated by adding the previous two terms.
# The first two terms of fibonacci sequence will be 1 and 2.

a = 1
b = 2
c = a + b
s = b

while c < 4000000 :
if (c % 2 == 0) :
s = s + c
a = b
b = c
c = a + b
print s
23 changes: 23 additions & 0 deletions prob3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Project Euler
# Problem - 3
# Find the largest prime factor of a composite number.

c = int(raw_input("Enter any Composite number : "))
i = 2
j = 1
cnt = 0

while i < c :
if c % i == 0 :
x = c / i
while j < x :
if x % j == 0 :
cnt = cnt + 1
j = j + 1
j = 1
if (cnt == 1) :
lpf = x
break
cnt = 0
i = i + 1
print lpf
14 changes: 14 additions & 0 deletions prob4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Project Euler
# Problem - 4
# Find the largest palindrome made from the product of two 3-digit numbers.

p = 0

for a in range(100, 1000) :
for b in range(100, a) :
c = a * b
if c > p :
s = str(a * b)
if s == s[::-1] :
p = a * b
print p
16 changes: 16 additions & 0 deletions prob5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Project Euler
# Problem - 5
# What is the smallest positive number that is evenly divisible by all of
# the numbers from 1 to 20?

i = 20
cnt = 0
while i > 0 :
for j in range(1, 21) :
if ( i % j == 0 ) :
cnt = cnt + 1
if (cnt == 20) :
break
cnt = 0
i = i + 1
print i
12 changes: 12 additions & 0 deletions prob6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Project Euler
# Problem - 6
# What is the difference between the sum of the squares and the square of the sums?

from math import pow

n = int(raw_input("Enter n : "))

sum_of_squares = n * (n + 1) * (2 * n + 1) / 6
square_of_sums = pow(n, 2) * pow(n + 1, 2) / 4

print "\nThe difference between the sum of the squares and the square of the sums of first %d natural numbers is : %d" % (n, square_of_sums - sum_of_squares)
27 changes: 27 additions & 0 deletions prob7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Project Euler
# Problem - 6
# What is the 10001st prime number?

from math import sqrt

count = 0
num = 2

def is_prime(n) :
cnt = 0
for i in range(1, int(sqrt(n)) + 1) :
if (n % i == 0) :
cnt = cnt + 1
if (cnt == 1) :
return 1
else :
return 0

while(1) :
if is_prime(num) :
count = count + 1
if count >= 10001 :
break
num = num + 1

print num
63 changes: 63 additions & 0 deletions prob8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Project Euler
# Problem - 8
"""
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
"""

num = '73167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450'

start = 0
end = 5
prod = 0
x = 1

while (end <= 1000) :
for i in range(start, end) :
x = x * int(num[i])
if (prod < x) :
prod = x
x = 1
start += 1
end += 1
print prod
21 changes: 21 additions & 0 deletions prob9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Project Euler
# Problem - 8
"""
Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
"""

for a in range(1, 1000) :
for b in range((a + 1), 1000) :
c = 1000 - a - b
if ( c*c == a*a + b*b) :
print a*b*c
exit

0 comments on commit 1e3bb1a

Please sign in to comment.