Last active
August 21, 2021 14:00
-
-
Save jac18281828/867f56b1ea2a89dd817b212a861b7875 to your computer and use it in GitHub Desktop.
Show the Feynman Point is the longest sequence in the first 100000 digits of py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
class longest_seq: | |
def find(sentence): | |
pos = 0 | |
l = 0 | |
cl = 0 | |
cpos = 0 | |
i = 0 | |
t = '' | |
while i < len(sentence): | |
if t == sentence[i]: | |
cl += 1 | |
else: | |
if cl > l: | |
l = cl + 1 | |
pos = cpos | |
t = sentence[i] | |
cl = 0 | |
cpos = i | |
i += 1 | |
if cl > l: | |
l = cl | |
pos = cpos | |
if l > 0: | |
return (pos, sentence[pos], l) | |
return None | |
class Testing(unittest.TestCase): | |
def test_example1(self): | |
result = longest_seq.find('abababaaaababab') | |
self.assertEqual(6, result[0]) | |
self.assertEqual('a', result[1]) | |
self.assertEqual(4, result[2]) | |
def test_increasing(self): | |
result = longest_seq.find('012333456789') | |
self.assertEqual(3, result[0]) | |
self.assertEqual('3', result[1]) | |
self.assertEqual(3, result[2]) | |
def test_secondshorter(self): | |
result = longest_seq.find('abababaaaabaabab') | |
self.assertEqual(6, result[0]) | |
self.assertEqual('a', result[1]) | |
self.assertEqual(4, result[2]) | |
def test_repetitive(self): | |
result = longest_seq.find('abababab') | |
self.assertEqual(None,result) | |
class SearchFeynman: | |
def __init__(self, n): | |
self.n = n | |
def pi_digits(n): | |
"Generate n digits of Pi." | |
k, a, b, a1, b1 = 2, 4, 1, 12, 4 | |
while n > 0: | |
p, q, k = k * k, 2 * k + 1, k + 1 | |
a, b, a1, b1 = a1, b1, p * a + q * a1, p * b + q * b1 | |
d, d1 = a / b, a1 / b1 | |
while d == d1 and n > 0: | |
yield int(d) | |
n -= 1 | |
a, a1 = 10 * (a % b), 10 * (a1 % b1) | |
d, d1 = a / b, a1 / b1 | |
def search(self): | |
digits = [ k for k in map(str, SearchFeynman.pi_digits(self.n))] | |
pi = digits[0] + '.' + ''.join(digits[1:]) | |
result = longest_seq.find(pi) | |
if result is None: | |
print('No sequence found') | |
else: | |
print('longest sequence of length %d is %s at %d' % (result[2], result[1]*result[2], result[0])) | |
if __name__ == '__main__': | |
# unittest.main() | |
sf = SearchFeynman(100500) | |
sf.search() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment