-
-
Save pgtwitter/2d687ad920c7b335d4c5857b86035d12 to your computer and use it in GitHub Desktop.
Collatz, Euler, Prime Stair
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
| #%% | |
| def collatz(): | |
| for i in range(1, 1000000): | |
| ns=[i] | |
| while(ns[-1]!=1): | |
| ns.append(3*ns[-1]+1 if ns[-1]%2==1 else ns[-1]/2) | |
| print(i, len(ns)) | |
| #%% | |
| import sympy | |
| def euler(n): | |
| if n==1: | |
| return 0 | |
| ret = n | |
| for p in sympy.factorint(n).keys(): | |
| ret *= 1-1/sympy.expand(p) | |
| return ret | |
| euler(1050) | |
| #%% | |
| import sympy | |
| import numpy as np | |
| cnt = 0 | |
| data = [] | |
| for k in range(2, 100000): | |
| if sympy.isprime(k): | |
| cnt+= 1 | |
| d= [k, cnt, k/np.log(k)] | |
| data.append(d) | |
| d.append((d[2]-d[1])/d[1]) | |
| import pandas as pd | |
| df= pd.DataFrame(data) | |
| import matplotlib.pyplot as plt | |
| plt.grid() | |
| plt.plot(df.loc[100:, 0], df.loc[100:, 1]) | |
| plt.plot(df.loc[100:, 0], df.loc[100:, 2]) | |
| ax=plt.twinx() | |
| ax.plot(df.loc[100:,0], df.loc[100:,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment