Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # find_pi_sequence.py
- from mycalc import int_commas, int_to_ordinal, random_digits
- from time import sleep
- import sys
- N = 100000000 # 100 million
- random_seq_length = 7
- while True:
- while True:
- msg = "Enter sequence of digits to be searched for; q to quit; nothing to enter a random, "
- msg2 = "-digit sequence."
- msg3 = " Enter a zero to get a prompt for getting a random sequence of a different length"
- print(msg, random_seq_length, msg2, msg3, sep="")
- seq = input("Enter: ")
- if seq == '0':
- y = input("Enter new length for sequence of random digits: ")
- random_seq_length = int(y)
- seq = random_digits(random_seq_length)
- break
- elif seq.isnumeric():
- break
- elif seq == 'q':
- print("Bye.")
- sleep(2.1)
- sys.exit()
- elif seq == "":
- seq = random_digits(random_seq_length)
- print("seq =", seq)
- break
- print("At least one of the chars you entered is not a digit; try again.")
- print()
- secs_to_sleep = 0
- flag = 1
- # orig_pid means the original string of 100 million mantissa digits of pi
- orig_pid = (open('/p31working/Data/100million_pi_digits.txt').read())[2:N+2]
- print("sequence to find in first", int_commas(N), "digits is", seq)
- print()
- seq_idx_in_orig_pid = orig_pid.find(seq)
- prev_seq_idx = seq_idx_in_orig_pid
- sleep(secs_to_sleep)
- if seq_idx_in_orig_pid == -1:
- print(seq, "doesn't appear in first 100 million digits of the mantissa of pi.")
- flag = 0
- elif 0 <= seq_idx_in_orig_pid < 25:
- print("The 1st instance of", seq, "begins at index", int_commas(seq_idx_in_orig_pid))
- print("A 50-digit sequence that contains this instance is:")
- else:
- print("The 1st instance of", seq, "begins at index", int_commas(seq_idx_in_orig_pid))
- print("The 50-digit sequence that contains this instance is:")
- p = (orig_pid[seq_idx_in_orig_pid-25:seq_idx_in_orig_pid] + "*" + seq + "*" +
- orig_pid[seq_idx_in_orig_pid+len(seq):seq_idx_in_orig_pid+25])
- print(p)
- seq_length = len(seq)
- orig_pid_length = len(orig_pid)
- pid = orig_pid
- for x in range(2,10010000):
- sleep(0.1)
- seq_idx_in_orig_pid += seq_length
- new_pid = pid[seq_idx_in_orig_pid:]
- seq_idx = new_pid.find(seq)
- seq_idx_in_orig_pid += seq_idx
- if seq_idx == -1:
- break
- print()
- sleep(secs_to_sleep)
- print("The", int_to_ordinal(x), "instance of", seq, "begins at index", int_commas(seq_idx_in_orig_pid))
- print("The 50-digit sequence that contains this instance is:")
- print((orig_pid[seq_idx_in_orig_pid-25:seq_idx_in_orig_pid] + "*" + seq + "*" +
- orig_pid[seq_idx_in_orig_pid+len(seq):seq_idx_in_orig_pid+25]))
- print()
- if x > 2:
- sleep(secs_to_sleep)
- print("There are", x-1, "instances of", seq)
- print()
- elif x == 2 and flag == 1:
- sleep(secs_to_sleep)
- print("There is only 1 instance of", seq, "in the first 100 million digits of pi's mantissa")
- print()
- z = input("Enter q to quit; nothing to continue: ")
- if z == 'q':
- print("Bye.")
- sleep(1.5)
- sys.exit()
- print()
Advertisement
Add Comment
Please, Sign In to add comment