Advertisement
Peaser

math-er

Aug 9th, 2015
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. def findThis(number, memorySafe=True, singleDigits=True, upto=100):
  2.   constructRetVal = lambda x_, y_, op_: "{} {} {} = {}".format(x_, op_, y_, number)
  3.   symbols = ["%", "^", "&", "*", "-", "+", ">>", "/"]
  4.   if not memorySafe:
  5.     symbols += ["<<", "**"]
  6.   iterRange = range(10) if singleDigits else range(upto)
  7.   found = []
  8.   for x in iterRange:
  9.     for y in iterRange:
  10.       for operator in symbols:
  11.         evalConstruct = "x {} y".format(operator)
  12.         try:
  13.           if eval(evalConstruct) == number:
  14.             found.append(constructRetVal(x, y, operator))
  15.         except ZeroDivisionError:
  16.           pass
  17.   return found
  18.  
  19.  
  20. # Demonstration
  21.  
  22. # >>> findThis(69, singleDigits=False)
  23. # ['0 ^ 69 = 69',
  24. #  '0 + 69 = 69',
  25. #  '1 ^ 68 = 69',
  26. #  '1 + 68 = 69',
  27. #  '1 * 69 = 69',
  28. #  '2 + 67 = 69',
  29. #  ...
  30. #  ...
  31. #  ...
  32. #  '87 ^ 18 = 69',
  33. #  '87 - 18 = 69',
  34. #  '87 & 69 = 69',
  35. #  '87 & 77 = 69',
  36. #  '88 - 19 = 69',
  37. #  '88 ^ 29 = 69',
  38. #  '89 - 20 = 69',
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement