Created
July 14, 2023 17:56
-
-
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
This file contains hidden or 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
#!/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