a pastebin project

Paste Description for python program to solve the prob

My answer to http://ask.metafilter.com/64035/Equation-solving-help-for-the-intellectually-challenged

python program to solve the prob

  1. # Solve original equation for y
  2. # x + y - x*y = 79
  3. #     y - x*y = 79 - x
  4. #   y * (1-x) = 79 - x
  5. #           y = (79-x) / (1-x)
  6.  
  7. # Find the value for y using integer math
  8. # then check whether this gives the exact value
  9. def candidate(x):
  10.     y = (79-x) / (1-x)
  11.     z = x + y - (x*y)
  12.     if y != 0 and z == 79: print x, y
  13.  
  14. # Starting at x=2, try x and -x as candidates
  15. # Zero is ruled out by the problem statement, and
  16. # 1 causes division by zero so it can't be a valid 'x' either
  17. # just keep going until interrupted, though there don't seem to be any
  18. # integer solutions for |x| > 77
  19. def main():
  20.     from itertools import count
  21.     try:
  22.         for x in count(2):
  23.             candidate(x)
  24.             candidate(-x)
  25.     except KeyboardInterrupt:
  26.         print "last number considered:", x
  27.  
  28. main()

advertising

Create a Paste

Please enter your new post below (or upload a file instead):





Please note that information posted here will not expire by default. If you want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords.

fantasy-obligation
fantasy-obligation