Advertisement
zeromega64twenty

PI - Consecutive Number Sequence Finder in pi

Aug 27th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | Source Code | 0 0
  1. """
  2. Copyright (c) 2023 Zeromega
  3. Drop a link or a Sub on one of my videos if this script help you, copy the link below
  4. https://www.youtube.com/channel/UCfqUJ4rmk6W-ZAjDtkBZ1CA?sub_confirmation=1
  5. """
  6.  
  7. from mpmath import mp
  8. import threading
  9.  
  10. def find_target_sequence(sequence, target, start, end, result):
  11.     for i in range(start, end - len(target) + 1):
  12.         if sequence[i:i + len(target)] == target:
  13.             result.append((i + 1, i + len(target)))
  14.             print(f"Thread-{threading.get_ident()}: Target sequence found at position {i + 1} to {i + len(target)}: {sequence[i:i + len(target)]}")
  15.             break
  16.  
  17. def find_consecutive_sequence(sequence, target, start, end, result):
  18.     consecutive_count = 1
  19.     for i in range(start + 1, end):
  20.         if sequence[i] == sequence[i - 1]:
  21.             consecutive_count += 1
  22.             if consecutive_count == len(target):
  23.                 result.append((i - len(target) + 2, i + 1))
  24.                 print(f"Thread-{threading.get_ident()}: Consecutive sequence of length {len(target)} found at position {i - len(target) + 2} to {i + 1}: {sequence[i - len(target) + 1:i + 1]}")
  25.                 break
  26.         else:
  27.             consecutive_count = 1
  28.  
  29. precision = 10000000
  30. target_sequence = "9999999"
  31. search_range = 1000000
  32. mp.dps = precision
  33. pi = mp.pi
  34.  
  35. pi_str = str(pi)
  36.  
  37. target_positions = []
  38. consecutive_positions = []
  39.  
  40. num_threads = 4
  41.  
  42. target_threads = []
  43. chunk_size = len(pi_str) // num_threads
  44. for i in range(num_threads):
  45.     start = i * chunk_size
  46.     end = start + chunk_size
  47.     thread = threading.Thread(target=find_target_sequence, args=(pi_str, target_sequence, start, end, target_positions))
  48.     target_threads.append(thread)
  49.     thread.start()
  50.  
  51. print(f"Main thread: Target sequence search started using {num_threads} threads.")
  52. for thread in target_threads:
  53.     thread.join()
  54. print("Main thread: Target sequence search completed.")
  55.  
  56. consecutive_threads = []
  57. chunk_size = search_range // num_threads
  58. for i in range(num_threads):
  59.     start = i * chunk_size
  60.     end = start + chunk_size
  61.     thread = threading.Thread(target=find_consecutive_sequence, args=(pi_str, target_sequence, start, end, consecutive_positions))
  62.     consecutive_threads.append(thread)
  63.     thread.start()
  64.  
  65. print(f"Main thread: Consecutive sequence search started using {num_threads} threads.")
  66. for thread in consecutive_threads:
  67.     thread.join()
  68. print("Main thread: Consecutive sequence search completed.")
  69.  
  70. print("Target sequences found:")
  71. for start, end in target_positions:
  72.     print(f"Position {start} to {end}: {pi_str[start - 1:end]}")
  73.  
  74. print("Consecutive sequences found:")
  75. for start, end in consecutive_positions:
  76.     print(f"Position {start} to {end}: {pi_str[start - 1:end]}")
  77.  
  78. if not target_positions:
  79.     print("No target sequences found.")
  80.  
  81. if not consecutive_positions:
  82.     print("No consecutive sequences found.")
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement