Created
June 26, 2017 04:14
-
-
Save aagallag/715b849875ce27ebade43fdfdc4aa5b6 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 python2 | |
def try_numb_part1(last2): | |
ecx = 0x1 | |
while last2 > 0: | |
last2 = last2 - ecx | |
ecx += 2 | |
if last2 != 0: | |
return False | |
return True | |
def try_numb_part2(first_4, last2): | |
ebx = last2*last2 | |
eax = first_4 | |
result = (eax ^ ebx) >> 8 | |
if result == 0: | |
return True | |
else: | |
return False | |
def brute_part1(): | |
possible_vals = [] | |
for i in range(0, 100)[::-1]: | |
if try_numb_part1(i): | |
print('Last 2 digits may be... %02d' % i) | |
possible_vals.append(i) | |
return possible_vals | |
def brute_part2(): | |
solutions = [] | |
possible_vals = brute_part1() | |
for i in range(0,10000)[::-1]: | |
for last in possible_vals: | |
if try_numb_part2(i,last): | |
solutions.append((i,last)) | |
print('# of possible solutions: %d' % len(solutions)) | |
print('First possible solution: %04d%02d' % solutions[0]) | |
return solutions | |
#https://stackoverflow.com/a/15285588/5948112 | |
def isprime(n): | |
if n==2 or n==3: return True | |
if n%2==0 or n<2: return False | |
for i in range(3,int(n**0.5)+1,2): # only odd numbers | |
if n%i==0: | |
return False | |
return True | |
def brute_prime_numbs(): | |
primes = [] | |
for i in range(0,999999+1): | |
numb = '%06d' % i | |
if not isprime(int(numb)): | |
continue | |
if not isprime(int(numb[:2])): | |
continue | |
if not isprime(int(numb[2:4])): | |
continue | |
primes.append(numb) | |
return primes | |
possible_vals = brute_part2() | |
primes = brute_prime_numbs() | |
prime_solutions = [] | |
for vals in possible_vals: | |
val = '%04d%02d' % vals | |
if val not in primes: | |
continue | |
prime_solutions.append(val) | |
print('Calculated %d possible solutions.' % len(prime_solutions)) | |
print(prime_solutions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment