Skip to content

Instantly share code, notes, and snippets.

@herbiebradley
Last active December 12, 2018 21:47
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 herbiebradley/d31e87ebf1e4c325a2658ed1df21f171 to your computer and use it in GitHub Desktop.
Save herbiebradley/d31e87ebf1e4c325a2658ed1df21f171 to your computer and use it in GitHub Desktop.
Python module for turning a piece of rectangular number art into a prime number version.
"""This module loads a piece of rectangular number art and generates a prime number version.
Requires:
- Python 2.7 or 3+
- gmpy2
The code slides a filter window of size 3 across the number art from the start_ind to the
end_ind, checking each possible 3 digit combination for primality with the Miller-Rabin
probabalistic primality test.
This website is useful for verifying that the possible prime is actually a prime:
https://www.alpertron.com.ar/ECM.HTM
Command Line Args:
num_string (String): The incoming number art, formatted as a single string. Default: prime_logo.txt.
start_ind (Integer): Starting index for the filter window. Default: 1920.
end_ind (Integer): Ending index for the filter window. Default: 1949.
logo_width (Integer): Width of the number art, used to write the possible prime file. Default: 40.
Output:
If at least one possible prime is found, the possible primes will be saved
in the format: possible_prime_logo1.txt, possible_prime_logo2.txt, etc.
"""
from __future__ import print_function
import os
import argparse
from gmpy2 import mpz, is_strong_prp
def read_num_from_file(file):
with open(file) as f:
num_string = "".join(line.rstrip() for line in f)
return num_string
def isPrime(num_string):
base = 10
mpznum = mpz(num_string, base)
return is_strong_prp(mpznum, base)
def search_for_prime(num_string, start_ind, end_ind, logo_width):
primes_found = 0
for filter in range(start_ind, end_ind):
print("Filter position is ", filter)
original_seq = num_string[filter:filter + 3]
for i in range(10):
print("Searching: First digit is {} out of 10...".format(i))
for j in range(10):
for k in range(10):
seq_to_try = str(i) + str(j) + str(k)
num_string = num_string[:filter] + seq_to_try + num_string[filter + 3:]
if isPrime(num_string):
primes_found += 1
save_prime(num_string, logo_width, file_index=primes_found)
num_string = num_string[:filter] + original_seq + num_string[filter + 3:]
return primes_found
def save_prime(possible_prime, logo_width, file_index):
n = logo_width
line_list = [possible_prime[i:i+n] for i in range(0, len(possible_prime), n)]
filename = "possible_prime_logo" + str(file_index) + ".txt"
print("Found possible prime, saved at ", filename)
with open(filename, 'w') as file:
for line in line_list:
file.write("%s\n" % line)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--file_to_read", type=str, default="prime_logo.txt",
help="Path to the file to generate prime from.")
parser.add_argument("--start_ind", type=int, default=1920,
help="Starting index for the filter window.")
parser.add_argument("--end_ind", type=int, default=1949,
help="Ending index for the filter window.")
parser.add_argument("--logo_width", type=int, default=40,
help="Width of the number art, used to write the possible prime file.")
FLAGS, _ = parser.parse_known_args()
path_to_file = os.path.abspath(FLAGS.file_to_read)
num_string = read_num_from_file(path_to_file)
num_possible_primes = search_for_prime(num_string, FLAGS.start_ind, FLAGS.end_ind, FLAGS.logo_width)
if num_possible_primes == 0:
print("No possible primes found. :(")
else:
print("Found {} possible primes!".format(num_possible_primes))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment