Skip to content
[For Fun] Visualization
  • AI Chat
  • Code
  • Report
  • Visualizations for excercise. Taken from Numberphile video

    import matplotlib.pyplot as plt
    import pandas as pd
    import numpy as np
    
    #Defines the range for the visualization
    prime_range = range(0, 200000)
    
    #creates list of primes
    primes = [i for i in prime_range if 0 not in [i%n for n in range(2, i)]]
    n_range = range(0, len(primes))
    
    df = pd.DataFrame(primes, columns=['prime'])
    df['n']=n_range
    df['p_to_bin']=df['prime'].apply(bin)
    df['prime_binary']=df['p_to_bin'].str.replace("0b","")
    
    reverse_bynary = []
    
    for i in df['prime_binary']:
        bin_value = "0b"+ str(i[::-1])
        reverse_bynary.append(bin_value)
    
    df['rev_bin']=reverse_bynary
    
    df['rev_prime']=df['rev_bin'].apply((lambda x: int(x,2) ))
    
    df['diff']=df['prime']-df['rev_prime']
    
    print(df.info())
    print(df.head(15))
    
    plt.scatter(df['n'],df['diff'])
    plt.show()
    # Start coding here...