Created
November 4, 2020 09:06
-
-
Save Eliran-Turgeman/4110cf7840a381ed0bb80e6c09dd629d 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
import matplotlib.pyplot as plt | |
%matplotlib inline | |
from tqdm import trange | |
def isPalindrome(x): | |
return str(x) == str(x)[::-1] | |
def graph_number_to_iterations(): | |
lychrel = [196,295,394,493,592,689,691,788,790,879,887,978, | |
986,1495,1497,1585,1587,1675,1677,1765,1767,1855, | |
1857,1945,1947,1997,2494,2496,2584,2586,2674,2676, | |
2764,2766,2854,2856,2944,2946,2996,3493,3495,3583, | |
3585,3673,3675] | |
iters=[] | |
iter = 0 | |
for x in trange(1000): | |
iter = 0 | |
while not isPalindrome(x) and x not in lychrel: | |
iter+=1 | |
x = int(x) + int(str(x)[::-1]) | |
iters.append(iter) | |
x_axis = [i for i in range(1000)] | |
plt.plot(x_axis, iters, 'b.') | |
plt.xlabel('Numbers') | |
plt.ylabel('Number of iterations until palindrome') | |
def graph_digits_to_iterations(): | |
x_axis2 = [] | |
x = 196 | |
for iter in trange(100000): | |
x = int(x) + int(str(x)[::-1]) | |
x_axis2.append(len(str(x))) | |
plt.plot(x_axis2, [i for i in range(100000)], 'r.') | |
plt.xlabel('Number of digits') | |
plt.ylabel('Number of iterations') | |
if __name__ == '__main__': | |
graph_number_to_iterations() | |
# graph_digits_to_iterations() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment