Last active
February 20, 2016 00:17
-
-
Save Yepoleb/80e57fb8f9143062fe77 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
""" | |
Solution for https://scrap.tf/raffles/2JKVYD | |
""" | |
# https://github.com/savoirfairelinux/num2words | |
from num2words import num2words | |
PRIMES = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, | |
61, 67, 71, 73, 79, 83, 89, 97] | |
# The pi function returns the number of primes below x, this is a lookup table | |
# for a few results | |
PI_LOOKUP = { | |
2357947691: 114870774, | |
25937424601: 1131108518, | |
10604499373: 481269293, | |
137858491849: 5602858280, | |
6975757441: 322734457, | |
118587876497: 4849395783 | |
} | |
def format_num(num): | |
num_str = num2words(num) | |
# Replace some stuff to make the format as required | |
num_str = num_str.replace(", ", " ").replace("-", " ").replace(" and", "").title() | |
return num_str | |
start = PRIMES.index(7)+1 | |
powers = set() | |
# Loop over primes between 11 and 29 (just an arbitrary limit) | |
for x in PRIMES[start:10]: | |
# Loop over numbers between 0 and 20 (arbitrary as well) | |
for y in range(0,20): | |
power = x ** y | |
# Check if the number is between the 100 millionth and 10 billionth prime | |
# (range of numers with ~14 words) | |
if 2038074743 < power < 252097800623: | |
powers.add(power) | |
print(x, y, power) | |
print() | |
# I manually filled in PI_LOOKUP from https://primes.utm.edu/nthprime/index.php | |
# because there wasn't enough time left to write a parser for a list or website | |
for power in powers: | |
if power not in PI_LOOKUP: | |
continue | |
# Get the number of primes below the power from the lookup table | |
num_primes = PI_LOOKUP[power] | |
# Format the number as words | |
num_str = format_num(num_primes) | |
# Check if the string has 14 words | |
if len(num_str.split()) == 14: | |
print(power, num_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment