pyzuki

find_pi_sequence.py

Sep 13th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1.  # find_pi_sequence.py
  2.  
  3. from mycalc import int_commas, int_to_ordinal, random_digits
  4.  
  5. from time import sleep
  6. import sys
  7.  
  8. N = 100000000  # 100 million
  9. random_seq_length = 7
  10.  
  11. while True:
  12.     while True:
  13.         msg = "Enter sequence of digits to be searched for; q to quit; nothing to enter a random, "
  14.         msg2 = "-digit sequence."
  15.         msg3 = " Enter a zero to get a prompt for getting a random sequence of a different length"
  16.         print(msg, random_seq_length, msg2, msg3, sep="")
  17.         seq = input("Enter: ")
  18.         if seq == '0':
  19.             y = input("Enter new length for sequence of random digits: ")
  20.             random_seq_length = int(y)
  21.             seq = random_digits(random_seq_length)
  22.             break
  23.         elif seq.isnumeric():  
  24.             break    
  25.         elif seq == 'q':
  26.             print("Bye.")
  27.             sleep(2.1)
  28.             sys.exit()
  29.         elif seq == "":
  30.             seq = random_digits(random_seq_length)
  31.             print("seq =", seq)
  32.             break
  33.         print("At least one of the chars you entered is not a digit; try again.")
  34.         print()
  35.     secs_to_sleep = 0
  36.     flag = 1
  37.    
  38.     # orig_pid means the original string of 100 million mantissa digits of pi
  39.     orig_pid = (open('/p31working/Data/100million_pi_digits.txt').read())[2:N+2]
  40.     print("sequence to find in first", int_commas(N), "digits is", seq)
  41.     print()
  42.     seq_idx_in_orig_pid = orig_pid.find(seq)
  43.     prev_seq_idx = seq_idx_in_orig_pid
  44.     sleep(secs_to_sleep)
  45.     if seq_idx_in_orig_pid == -1:
  46.         print(seq, "doesn't appear in first 100 million digits of the mantissa of pi.")
  47.         flag = 0
  48.     elif 0 <= seq_idx_in_orig_pid < 25:
  49.         print("The 1st instance of", seq, "begins at index", int_commas(seq_idx_in_orig_pid))
  50.         print("A 50-digit sequence that contains this instance is:")
  51.     else:
  52.         print("The 1st instance of", seq, "begins at index", int_commas(seq_idx_in_orig_pid))
  53.         print("The 50-digit sequence that contains this instance is:")
  54.         p = (orig_pid[seq_idx_in_orig_pid-25:seq_idx_in_orig_pid] + "*" + seq + "*" +
  55.              orig_pid[seq_idx_in_orig_pid+len(seq):seq_idx_in_orig_pid+25])
  56.         print(p)
  57.        
  58.     seq_length = len(seq)
  59.     orig_pid_length = len(orig_pid)
  60.     pid = orig_pid
  61.     for x in range(2,10010000):
  62.         sleep(0.1)
  63.         seq_idx_in_orig_pid += seq_length
  64.         new_pid = pid[seq_idx_in_orig_pid:]
  65.         seq_idx = new_pid.find(seq)
  66.         seq_idx_in_orig_pid += seq_idx
  67.         if seq_idx == -1:
  68.             break
  69.        
  70.         print()
  71.         sleep(secs_to_sleep)
  72.         print("The", int_to_ordinal(x), "instance of", seq, "begins at index", int_commas(seq_idx_in_orig_pid))
  73.         print("The 50-digit sequence that contains this instance is:")
  74.         print((orig_pid[seq_idx_in_orig_pid-25:seq_idx_in_orig_pid] + "*" + seq + "*" +
  75.                orig_pid[seq_idx_in_orig_pid+len(seq):seq_idx_in_orig_pid+25]))
  76.     print()
  77.     if x > 2:
  78.         sleep(secs_to_sleep)
  79.         print("There are", x-1, "instances of", seq)
  80.         print()
  81.     elif x == 2 and flag == 1:
  82.         sleep(secs_to_sleep)
  83.         print("There is only 1 instance of", seq, "in the first 100 million digits of pi's mantissa")
  84.         print()
  85.     z = input("Enter q to quit; nothing to continue: ")
  86.     if z == 'q':
  87.         print("Bye.")
  88.         sleep(1.5)
  89.         sys.exit()
  90.     print()
Advertisement
Add Comment
Please, Sign In to add comment