Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #====================================================================
- # Project Euler Base File - for those who want to try
- #
- # By Charc
- #
- # Projects can be found at: https://projecteuler.net/problems
- # Some of the projects have also been provided with Titles
- # and Project aims.
- # This porgram accepts an input for which project you would like to
- # check the solution for.
- # A link with solutions is also provided in the comments below.
- # With Problem 0 (unofficial & my own) solved to give an example.
- # For convinience's sake, please don't run with -OO
- #====================================================================
- #=======SOLUTIONS - BEWARE===========================================
- # Proj 0: 145;
- # http://www.tsm-resources.com/alists/trip.html
- # Ctrl-F if you don't believe me.
- # Proj >0:
- # https://code.google.com/p/projecteuler-solutions/wiki/ProjectEulerSolutions
- #====================================================================
- #====================================================================
- # Valid Input
- # TODO make the valid input block more pretty
- #====================================================================
- import sys
- if len(sys.argv) > 2:
- print "Try again with a whole number."
- sys.exit(-1)
- try:
- n = int(sys.argv[1])
- except IndexError:
- n = 0
- except ValueError:
- print "Try again with a whole number."
- sys.exit(-1)
- print "You have chosen question {0}.".format(n)
- #====================================================================
- # General solutions for the problems defined as functions
- #====================================================================
- import math as m
- def prob0(x):
- """Given the right angle with a sidelength 12, the largest
- hypotenuse part of an integer pythagorean triple is 37.
- Given a sidelength of 24, what's the largest value for the
- hypotenuse part of an integer pythagorean triangle?"""
- # Exactly as asked -- We want the largest int Hyp given
- # one length is x -- can be shown this is only the case
- # for primitive triangles TODO find proof of this?
- # There is an m, n formula by Euclid
- # A = a^2 - b^2, B = 2ab, C = a^2 + b^2
- # a!=b (mod 2) and gcd(a, b)=1 gives all primitive triangles
- # We can simply work out all such solutions and take max
- # by defn of A, a > b
- ans = 0
- if x % 2 == 0:
- # So x is the even side, i.e. B
- # a > b --> sqrt(B/2) > b
- for b in range(1, int((m.sqrt(x/2)))+1):
- if x/2 % b == 0:
- ans = max(ans, (x/(2*b))**2 + b*b)
- return ans
- else:
- # x is on the odd side, i.e. A
- # now (a-b)(a+b) = A, so:
- for i in range(1, int(m.sqrt(n))):
- if n%i == 0:
- p = (i + n/i)/2
- q = n/i -p
- ans = max(ans, p*p + q*q)
- return ans
- def prob1(x):
- """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 1000."""
- return x
- def prob2(x):
- """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 four million, find the sum of the even-valued terms."""
- return x
- def prob3(x):
- """The prime factors of 13195 are 5, 7, 13 and 29.
- What is the largest prime factor of the number 600851475143?"""
- return x
- def prob4(x):
- """A palindromic number reads the same both ways. The largest
- palindrome made from the product of two 2-digit numbers is
- 9009 = 91 * 99.
- Find the largest palindrome made from the product of two 3-digit
- numbers."""
- return x
- #====================================================================
- # Printing questions and answers
- #====================================================================
- t = "Problem number {0}?!".format(n)
- q = "Sorry, I haven't done that one.\nYet!"
- z = "going to be ready soon!\nhttps://projecteuler.net/problem={0}".format(n)
- if n == 0:
- t = "Largest hypotenuse"
- q = prob0.__doc__
- z = prob0(24)
- elif n == 1:
- t = "Multiples of 3 and 5"
- q = prob1.__doc__
- z = prob1(1000)
- elif n == 2:
- t = "Even Fibonacci numbers"
- q = prob2.__doc__
- z = prob2(4000000)
- elif n == 3:
- t = "Largest prime factor"
- q = prob3.__doc__
- z = prob3(600851475143)
- elif n == 4:
- t = "Largest palindrome product"
- q = prob4.__doc__
- z = prob4(3)
- print "\n{ti}\n\n{qu}\n\nMy solution is {ans}.".format(ti=t, qu=q, ans = z)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement