- Mar 3, 2011
- 424
Hello Chaps!
Here is a great site/project that is fun for all programmers and mathematicians when you have some spare time to kill.
http://projecteuler.net/
Simply, This site contains various math problems to solve using a programming language (any language you choose).
When there hasn't much to do at work I have been completing some using Python and I thought i would update this thread with code as i complete problems and hopefully get you guys submitting your solutions.
NOTE: The idea is to complete these problems as efficiently and elegantly as possible and to not just brute force it
I have made the code text white to not spoil it for people that want to have a go before looking at my code (obv if you don't use the default theme this wont apply to you)
Also if there is a way to toggle the display of the code box's could you tell me how, I know how to do it with java script but the forum doesn't allow it.
Problem 1 - Python
Problem 2 - Python
Problem 3 - Python
Problem 4 - Python
Problem 5 - Python
Problem 6 - Python
Problem 7 - Python
Problem 8 - Python
Problem 9 - Python
Problem 10 - Python
There's 10 for now I will post up my other completed problems later.
Cheers
Here is a great site/project that is fun for all programmers and mathematicians when you have some spare time to kill.
http://projecteuler.net/
Simply, This site contains various math problems to solve using a programming language (any language you choose).
When there hasn't much to do at work I have been completing some using Python and I thought i would update this thread with code as i complete problems and hopefully get you guys submitting your solutions.
NOTE: The idea is to complete these problems as efficiently and elegantly as possible and to not just brute force it
I have made the code text white to not spoil it for people that want to have a go before looking at my code (obv if you don't use the default theme this wont apply to you)
Also if there is a way to toggle the display of the code box's could you tell me how, I know how to do it with java script but the forum doesn't allow it.
Problem 1 - Python
Code:
total = 0
for n in range(3, 1000):
if n % 3 == 0:
total = total + n
elif n % 5 == 0:
total = total + n
print total
Problem 2 - Python
Code:
result = []
answer = 0
a, b = 0, 1
while b < 4000000:
result.append(b)
a, b = b, a+b
for i in result:
if i % 2 == 0:
answer = answer + i
print answer
Problem 3 - Python
Code:
num = 600851475143
factor = 3
min_factor = 1
def find_factor(num, factor):
found_factor = "false"
while factor <= num and found_factor == "false":
if num % factor == 0:
found_factor = "true"
else:
factor = factor + 2
print "factor: %i" % factor
return factor
while num > 1:
min_factor = find_factor(num, factor)
num = num/min_factor
print "Max factor is %i" % min_factor
Problem 4 - Python
Code:
def palandromic(num):
test = "false"
num_string = str(num)
num_string_reversed = num_string[::-1]
num_int_reversed = int(num_string_reversed)
if num == num_int_reversed:
test = "true"
return test
answer = 0
for x in range(100, 999):
for y in range(100, 999):
num = x * y
if palandromic(num) == "true" and num > answer:
answer = num
print answer
Problem 5 - Python
Code:
counter = 2520
test = "true"
while test == "true":
for i in range(1, 20):
if counter % i != 0:
counter = counter + 20
break
else:
print counter
test = "false"
Problem 6 - Python
Code:
total1, total2 = 0, 0
for i in range(1, 101):
total1 = total1 + int(i*i)
total2 = total2 + i
total2 = total2 * total2
answer = total2 - total1
print answer
Problem 7 - Python
Code:
counter, n = 0, 1
while counter < 10001:
n = n + 1
for x in range (2, int((n**0.5)+1)):
if n % x == 0:
break
else:
counter = counter + 1
if counter == 10001:
print n
Problem 8 - Python
Code:
num = 7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
num_string = str(num)
last_product = len(num_string)
index1 = 0
index2 = 1
index3 = 2
index4 = 3
index5 = 4
answer = 0
product = 0
while index5 < last_product:
product = int(int(num_string[index1])*int(num_string[index2])*int(num_string[index3])*int(num_string[index4])*int(num_string[index5]))
index1 += 1
index2 += 1
index3 += 1
index4 += 1
index5 += 1
if product > answer:
answer = product
print answer
Problem 9 - Python
Code:
for a in range(1, 500):
for b in range(1, 500):
for c in [1000-b-a]:
if a * a + b * b == c * c and a < b and b < c:
print a * b * c
Problem 10 - Python
Code:
sum, n = 0, 1
while n < 2000000:
n = n + 1
for x in range (2, int((n**0.5)+1)):
if n % x == 0:
break
else:
sum = sum + n
print sum
Cheers