Advertisement
makispaiktis

Search if a given sequence exists in 'pi' digits

May 18th, 2022 (edited)
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. try:
  2.     # import version included with old SymPy
  3.     from sympy.mpmath import mp
  4. except ImportError:
  5.     # import newer version
  6.     from mpmath import mp
  7.  
  8. # Decimal places = 1000
  9. LIMIT = 3000
  10. mp.dps = LIMIT
  11. _PI_ = mp.pi
  12. PI = [int(element) for element in str(_PI_) if element != "."]
  13.  
  14. for ROUND in range(100):
  15.     print(80 * "*")
  16.     # Ask the user to give an input to be checked
  17.     sequence = input("Which number sequence do you want me to search if it exists in 'pi'? ")
  18.     target = [int(element) for element in str(sequence)]
  19.     LEN = len(target)
  20.     index = -1000
  21.     for i in range(len(PI) - len(target)):
  22.         if PI[i : i+LEN] == target:
  23.             index = i
  24.             break
  25.     if index == -1000:
  26.         print("Sequence " + sequence + " does not exist in the first " + str(LIMIT) + " digits of pi.")
  27.     else:
  28.         print("Sequence " + sequence + " exists in pi located from index " + str(index) + " to index " + str(index + LEN - 1))
  29.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement