Last active
October 10, 2017 03:31
-
-
Save scratchmex/620c37486ef39343424143fdb1aa7d41 to your computer and use it in GitHub Desktop.
Check if a number is capicúa and if not, add its palindrome and check it again until n iterations given.
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/env python3 | |
from time import time as time | |
#Made by Ivan Gonzalez https://github.com/scratchmex (05/09/2017) | |
#Use python3 | |
def reverse_number(n): | |
n = str(n) | |
return int(''.join([n[-i-1] for i in range(len(n))])) | |
def is_capicua(n): | |
n = str(n) | |
for i in range(len(n)//2): | |
if n[i] is not n[-i-1]: | |
return False | |
return True | |
def process_numbers(numbers, max_iter, debug=False): | |
start_time = time() | |
proc_num = {} | |
for number in numbers: | |
i = 0 | |
num = number | |
while i is not max_iter: | |
if is_capicua(num): | |
proc_num[number] = (i, num, 1) | |
print('{} Done'.format(number), end='\r') if debug else None | |
break | |
else: | |
prev_num, num = num, num + reverse_number(num) | |
i += 1 | |
if i is max_iter: | |
print('{} Reach limit'.format(number)) if debug else None | |
proc_num[number] = (i, num, -1) | |
global process_time | |
process_time = time()-start_time | |
print('This process took {} seconds'.format(process_time)) | |
return proc_num | |
def save_file(data, name): | |
init_t = time() | |
print('Starting saving...') | |
with open(name, 'w') as f: | |
f.write(data) | |
print('Saved, this procces took {} seconds'.format(time()-init_t)) | |
def format_save_cap(data, name): | |
keys = data.keys() | |
txt = '#Made by Ivan Gonzalez https://github.com/scratchmex (05/09/2017)\n' | |
txt += 'This process took {} seconds\n\n'.format(process_time) | |
print('Processing data...') | |
for key in keys: | |
if data[key][2] == 1: | |
txt += 'Its capicua number of "{}" is "{}" in {} iterations\n'.format(key, data[key][1], data[key][0]) | |
elif data[key][2] == -1: | |
txt += '"{}" has not capicua after {} iterations, I stayed in "{}"\n'.format(key, data[key][0], data[key][1]) | |
print('Done processing') | |
save_file(txt, name) | |
def main(numbers, max_iter): | |
format_save_cap(process_numbers(numbers, max_iter), '1milCapicuas.txt') | |
if __name__ == '__main__': | |
main(range(1,10000+1), 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment