Guest User

Scan Adder

a guest
Jan 3rd, 2021
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. addition = True
  2.  
  3. data = ''
  4. name = 'Pi'
  5.  
  6. choice = raw_input('Enter constant Choice (Pi, 2Pi, Phi, e, em, A, cat, kl, rt): ')
  7.  
  8. if choice.lower().strip() == '2pi':
  9.     name = '2Pi'
  10.     with open('/path/to/2pi_20m.txt') as the_file: data = the_file.read()
  11. elif choice.lower().strip() == 'phi':
  12.     name = 'Phi'
  13.     with open('/path/to/phi_20m.txt') as the_file: data = the_file.read()
  14. elif choice.lower().strip() == 'e':
  15.     name = 'e'
  16.     with open('/path/to/e_20m.txt') as the_file: data = the_file.read()
  17. elif choice.lower().strip() == 'em':
  18.     name = 'Euler-Mascheroni'
  19.     with open('/path/to/em_20m.txt') as the_file: data = the_file.read()
  20. elif choice.lower().strip() == 'a':
  21.     name = 'A'
  22.     with open('/path/to/A.txt') as the_file: data = the_file.read()
  23. elif choice.lower().strip() == 'cat':
  24.     name = 'Catalan'
  25.     with open('/path/to/catalan_20m.txt') as the_file: data = the_file.read()
  26. elif choice.lower().strip() == 'kl':
  27.     name = 'Khinchin-Levy'
  28.     with open('/path/to/khinchin-levy_20m.txt') as the_file: data = the_file.read()
  29. elif choice.lower().strip() == 'rt':
  30.     name = 'Sqrt2'
  31.     with open('/path/to/sqrt_2.txt') as the_file: data = the_file.read()
  32. else:
  33.     name = 'Pi'
  34.     with open('/path/to/pi_20m.txt') as the_file: data = the_file.read()
  35.  
  36. data = data[:100000]
  37.  
  38. target = 1
  39.  
  40. while True:
  41.     num_input = raw_input('Enter number to search for: ').strip()
  42.     if num_input.isdigit():
  43.         if int(num_input) > 0:
  44.             target = int(num_input)
  45.             break
  46.  
  47. answer = raw_input('\nPress Enter to do addition. Enter anything to do subtraction.')
  48. if answer != '': addition = False
  49.  
  50. matches = []
  51.  
  52. print('')
  53.  
  54. for i in range(0, len(data)):
  55.     for length in range(2, 15):
  56.         section = data[i:i + length]
  57.         end_of = i + length
  58.  
  59.         for j in range(1, len(section)):
  60.             a_s = section[:j]
  61.             b_s = section[j:]
  62.             a = int(a_s)
  63.             b = int(b_s)
  64.  
  65.             if addition == True:
  66.                 if a + b == target:
  67.                     if end_of not in matches:
  68.                         matches.append(end_of)
  69.                         print('"' + a_s + ' ' + b_s + '" occurs at the end of ' + str(end_of) + ' digits of ' + name + ' (' + str(a) + ' + ' + str(b) + ' = ' + str(target) + ')')
  70.             else:
  71.                 if abs(a - b) == target:
  72.                     a_msg = str(a)
  73.                     b_msg = str(b)
  74.  
  75.                     if a < b:
  76.                         a_msg = str(b)
  77.                         b_msg = str(a)
  78.  
  79.                     if end_of not in matches:
  80.                         matches.append(end_of)
  81.                         print('"' + a_s + ' ' + b_s + '" occurs at the end of ' + str(end_of) + ' digits of ' + name + ' (' + a_msg + ' - ' + b_msg + ' = ' + str(target) + ')')
Advertisement
Add Comment
Please, Sign In to add comment