Advertisement
Charc248

ProjEuler - Python Base

Dec 22nd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1. #!/usr/bin/python
  2. #====================================================================
  3. # Project Euler Base File - for those who want to try
  4. #
  5. # By Charc
  6. #
  7. # Projects can be found at: https://projecteuler.net/problems
  8. # Some of the projects have also been provided with Titles
  9. # and Project aims.
  10. # This porgram accepts an input for which project you would like to
  11. # check the solution for.
  12. # A link with solutions is also provided in the comments below.
  13. # With Problem 0 (unofficial & my own) solved to give an example.
  14. # For convinience's sake, please don't run with -OO
  15. #====================================================================
  16.  
  17. #=======SOLUTIONS - BEWARE===========================================
  18. # Proj 0: 145;
  19. # http://www.tsm-resources.com/alists/trip.html
  20. # Ctrl-F if you don't believe me.
  21. # Proj >0:
  22. # https://code.google.com/p/projecteuler-solutions/wiki/ProjectEulerSolutions
  23. #====================================================================
  24.  
  25.  
  26. #====================================================================
  27. #   Valid Input
  28. #   TODO make the valid input block more pretty
  29. #====================================================================
  30. import sys
  31.  
  32. if len(sys.argv) > 2:
  33.     print "Try again with a whole number."
  34.     sys.exit(-1)
  35. try:
  36.     n = int(sys.argv[1])
  37. except IndexError:
  38.     n = 0
  39. except ValueError:
  40.     print "Try again with a whole number."
  41.     sys.exit(-1)
  42.  
  43. print "You have chosen question {0}.".format(n)
  44.  
  45. #====================================================================
  46. #   General solutions for the problems defined as functions
  47. #====================================================================
  48. import math as m
  49.  
  50. def prob0(x):
  51.     """Given the right angle with a sidelength 12, the largest
  52. hypotenuse part of an integer pythagorean triple is 37.
  53. Given a sidelength of 24, what's the largest value for the
  54. hypotenuse part of an integer pythagorean triangle?"""
  55.     # Exactly as asked -- We want the largest int Hyp given
  56.     # one length is x -- can be shown this is only the case
  57.     # for primitive triangles TODO find proof of this?
  58.    
  59.     # There is an m, n formula by Euclid
  60.     # A = a^2 - b^2, B = 2ab, C = a^2 + b^2
  61.     # a!=b (mod 2) and gcd(a, b)=1 gives all primitive triangles
  62.     # We can simply work out all such solutions and take max
  63.     # by defn of A, a > b
  64.     ans = 0
  65.     if x % 2 == 0:
  66.     # So x is the even side, i.e. B
  67.     # a > b --> sqrt(B/2) > b
  68.         for b in range(1, int((m.sqrt(x/2)))+1):
  69.             if x/2 % b == 0:
  70.                 ans = max(ans, (x/(2*b))**2 + b*b)
  71.         return ans
  72.     else:
  73.     # x is on the odd side, i.e. A
  74.     # now (a-b)(a+b) = A, so:
  75.         for i in range(1, int(m.sqrt(n))):
  76.             if n%i == 0:
  77.                 p = (i + n/i)/2
  78.                 q = n/i -p
  79.                 ans = max(ans, p*p + q*q)
  80.     return ans
  81.  
  82.  
  83. def prob1(x):
  84.     """If we list all the natural numbers below 10 that are multiples of
  85. 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
  86. Find the sum of all the multiples of 3 or 5 below 1000."""
  87.     return x
  88.    
  89.  
  90. def prob2(x):
  91.     """Each new term in the Fibonacci sequence is generated by
  92. adding the previous two terms. By starting with 1 and 2, the first
  93. 10 terms will be:
  94.             1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  95. By considering the terms in the Fibonacci sequence whose values do
  96. not exceed four million, find the sum of the even-valued terms."""
  97.     return x
  98.  
  99.  
  100. def prob3(x):
  101.     """The prime factors of 13195 are 5, 7, 13 and 29.
  102. What is the largest prime factor of the number 600851475143?"""
  103.     return x
  104.  
  105.  
  106. def prob4(x):
  107.     """A palindromic number reads the same both ways. The largest
  108. palindrome made from the product of two 2-digit numbers is
  109. 9009 = 91 * 99.
  110. Find the largest palindrome made from the product of two 3-digit
  111. numbers."""
  112.     return x
  113. #====================================================================
  114. #   Printing questions and answers
  115. #====================================================================  
  116. t = "Problem number {0}?!".format(n)
  117. q = "Sorry, I haven't done that one.\nYet!"
  118. z = "going to be ready soon!\nhttps://projecteuler.net/problem={0}".format(n)
  119.  
  120. if n == 0:
  121.     t = "Largest hypotenuse"
  122.     q = prob0.__doc__
  123.     z = prob0(24)
  124. elif n == 1:
  125.     t = "Multiples of 3 and 5"
  126.     q = prob1.__doc__
  127.     z = prob1(1000)
  128. elif n == 2:
  129.     t = "Even Fibonacci numbers"
  130.     q = prob2.__doc__
  131.     z = prob2(4000000)
  132. elif n == 3:
  133.     t = "Largest prime factor"
  134.     q = prob3.__doc__
  135.     z = prob3(600851475143)
  136. elif n == 4:
  137.     t = "Largest palindrome product"
  138.     q = prob4.__doc__
  139.     z = prob4(3)
  140. 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