Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

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
Monday, June 4th, 2007 at 8:22:47am MDT 

  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

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not 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. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right
fantasy-obligation