Skip to content

Instantly share code, notes, and snippets.

@orjanv
Created July 14, 2023 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orjanv/dc912f0b81a1fcf732979027d869244d to your computer and use it in GitHub Desktop.
Save orjanv/dc912f0b81a1fcf732979027d869244d to your computer and use it in GitHub Desktop.
Find dates in Pi decimals and check if their positions are prime or palindromprime
#!/usr/bin/python
# coding: utf-8
def main():
# Get date from user
birthday = input('Enter birth date on the form DDMMYYYY: ')
print(f'You entered {birthday}, {len(birthday)} digits.\n')
num = len(birthday)
# Read in decimals of Pi
pidecimalfile = 'pi-decimals.txt'
with open(pidecimalfile, 'r') as decimalfile:
decimals = decimalfile.read()
print(f'Loaded {len(decimals)} pi-decimals from {pidecimalfile}\n')
# Look for date as a slice in the long range of decimals
for pos in range(0,len(str(decimals)),1):
x = decimals[pos:pos+num]
if len(x) < num:
break
if x == birthday:
print(f'Found number {birthday} in pos {pos}')
# Check if position found is prime
if (pos % 2) != 0:
if all(int(pos) % j for j in range(2, int(pos))):
# Also, check if pos is palindrom
reverse_pos = str(pos)[::-1]
if str(pos) == str(reverse_pos):
print(f"Position {pos} is a palindromeprime")
else:
print(f"Position {pos} is a prime")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment