# original code by Daniel Hutama
# modified by Darrell Cox to support linearly interpolated zeros
# Not all imports are used.

import numpy as np
import cmath
import matplotlib.pyplot as plt
import math
import cmath
import mpmath as mp
from numpy import polyfit
plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.unicode'] = True
import scipy

from sage.functions.spike_function import SpikeFunction
from scipy.interpolate import interp1d
from scipy import stats
import scipy.integrate as integrate
import scipy.special as special
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
from mpl_toolkits.axes_grid1.inset_locator import mark_inset

from sage.libs.lcalc.lcalc_Lfunction import *
# These functions count the exact value of pi_G(x)

def gi_of_norm(max_norm):
    Gaussian_primes = {}
    Gaussian_integers = {}
    Gaussian_integers[0] = [(0,0)]
    for x in range(1, ceil(sqrt(max_norm))):
        for y in range(0, ceil(sqrt(max_norm - x^2))):
            N = x^2 + y^2
            if Gaussian_integers.has_key(N):
                Gaussian_integers[N].append((x,y))
            else:
                Gaussian_integers[N] = [(x,y)]
            if(y == 0 and is_prime(x) and x%4==3):
                have_prime = True
            elif is_prime(N) and N%4==1 or N==2:
                have_prime = True
            else:
                have_prime =False
            if have_prime:
                if Gaussian_primes.has_key(N):
                    Gaussian_primes[N].append((x,y))
                else:
                    Gaussian_primes[N] = [(x,y)]
    return Gaussian_primes,Gaussian_integers

def all_gaussian_primes_up_to_norm(N):
    gips = gi_of_norm(N+1)[0]
    return flatten([uniq([(x,y), (-y,x), (y,-x), (-x,-y)]) for x,y in flatten(gips.values(), max_level=1)], max_level=1)

def all_gaussian_integers_up_to_norm(N):
    gis = gi_of_norm(N+1)[1]
    return flatten([uniq([(x,y), (-y,x), (y,-x), (-x,-y)]) for x,y in flatten(gis.values(), max_level=1)], max_level=1)

# returns a list of the cumulative value of Gaussian primes up to a given norm (unique up to multiplcation by units in Z[i])
def prime_counter(maxnorm):
    myrange=range(0,maxnorm+1)
    emptyset = []
    for mynorm in myrange:
        emptyset.append(len(all_gaussian_primes_up_to_norm(mynorm))/4)
    return emptyset
# These lines create a list of L function's non-trivial zeros
chi_mod4=DirichletGroup(4).gen() 
L_mod4=Lfunction_from_character(chi_mod4, type="int")
#L_mod4_zeros_list = L_mod4.find_zeros(0,1000,.1)

# one value interpolated between successive zeros (868 values)
L_mod4_zeros_list=[6.0209489046976001, 8.1323596044321000, 10.2437703041666010, 11.6159341582395010, 12.9880980123124010, 14.6653525584498010,
 16.3426071045871990, 17.3173001503553510, 18.2919931961234990, 19.8713022700534980, 21.4506113439835000, 22.3644939322215000,
 23.2783765204594990, 24.5035664727741000, 25.7287564250887010, 27.0441953840569980, 28.3596343430252990, 29.0080091788092000,
 29.6563840145931010, 31.1242852708551500, 32.5921865271172000, 33.3960720181651500, 34.1999575092131000, 35.1714189837581020,
 36.1428804583030970, 37.3274018000108970, 38.5119231417186970, 39.4172986042046030, 40.3226740666905030, 41.0648793433475560,
 41.8070846200046020, 43.2124878393334480, 44.6178910586623020, 45.1087377277269470, 45.5995843967915990, 46.6705733388653530,
 47.7415622809391000, 48.7323458023608540, 49.7231293237826010, 50.7046113883265550, 51.6860934528705020, 52.2274571103375960,
 52.7688207678046980, 54.0181821762519480, 55.2675435846991970, 56.1009588199507480, 56.9343740552022980, 57.5255405829380990,
 58.1167071106739000, 59.2692105298408480, 60.4217139490078030, 61.2151731173878060, 62.0086322857678030, 62.8616367022765980,
 63.7146411187854010, 64.3454058459407040, 64.9761705730959990, 66.3065457183210470, 67.6369208635460950, 68.0014026836902450,
 68.3658845038343940, 69.2758822063182440, 70.1858799088020930, 71.1706824415919930, 72.1554849743819060, 72.9615602479339030,
 73.7676355214859000, 74.4553785844595100, 75.1431216474331050, 75.9197124254316550, 76.6963032034302050, 77.7531507588755490,
 78.8099983143208930, 79.5100647763437390, 80.2101312383666000, 80.7120414326248580, 81.2139516268831020, 82.4403038206768600,
 83.6666560144706040, 84.1991981891260930, 84.7317403637815970, 85.6547002660859390, 86.5776601683902950, 87.1036891439891010,
 87.6297181195879060, 88.7154248681418520, 89.8011316166957980, 90.5754177156967020, 91.3497038146976050, 91.7936018625759120,
 92.2374999104542040, 93.2020597482071050, 94.1666195859600070, 95.1513153738702610, 96.1360111617805020, 96.5488763705990040,
 96.9617415794175060, 97.8585209975859980, 98.7553004157545050, 99.4450935594112480, 100.1348867030680100, 101.1381337649790000,
 102.1413808268900000, 102.7147281042844900, 103.2880753816789900, 103.8106726129729900, 104.3332698442669900, 105.5138643765814900,
 106.6944589088960000, 107.1923329420214900, 107.6902069751470100, 108.4750749745375100, 109.2599429739280100, 109.8797755751785100,
 110.4996081764290100, 111.4337124602205100, 112.3678167440120000, 113.0913061465025000, 113.8147955489930000, 114.4785825983210000,
 115.1423696476490100, 115.6677871530550000, 116.1932046584610000, 117.3653795186540100, 118.5375543788470100, 118.9952721275280100,
 119.4529898762090000, 120.0921417474345100, 120.7312936186600000, 121.5893774988645100, 122.4474613790690000, 123.1210050696944900,
 123.7945487603200000, 124.7815341601180000, 125.7685195599160000, 126.0336477924285000, 126.2987760249410100, 127.1290918540020100,
 127.9594076830630100, 128.9225155208540100, 129.8856233586450100, 130.4896010563645100, 131.0935787540840100, 131.6185776775360000,
 132.1435766009880100, 132.9438790324815000, 133.7441814639750000, 134.6175093582659800, 135.4908372525569900, 136.0190747599199900,
 136.5473122672829900, 137.5023033884895100, 138.4572945096960000, 138.6037361071535100, 138.7501777046109900, 140.0019051071815000,
 141.2536325097520100, 141.8240250159750000, 142.3944175221980100, 142.8617401319079900, 143.3290627416180000, 144.1536145096645200,
 144.9781662777110100, 145.7500857813095100, 146.5220052849080000, 147.2282680485445200, 147.9345308121810100, 148.5614942647420100,
 149.1884577173030100, 149.7424083594230000, 150.2963590015430100, 151.1291733360135100, 151.9619876704840100, 152.8308001469995100,
 153.6996126235150100, 154.1375520261175000, 154.5754914287200000, 155.1128700459814800, 155.6502486632429900, 156.6992769855729900,
 157.7483053079030000, 158.2266632167159900, 158.7050211255289900, 159.4707526111499800, 160.2364840967709900, 160.8218155364639900,
 161.4071469761570100, 161.9865968328780100, 162.5660466895990100, 163.6486056530880000, 164.7311646165769900, 165.0662919512199900,
 165.4014192858630000, 166.0776492274854900, 166.7538791691079900, 167.3991499754089900, 168.0444207817099900, 169.0477759846185100,
 170.0511311875270100, 170.3929490910530000, 170.7347669945789900, 171.5076243830999900, 172.2804817716210000, 172.8617303038780100,
 173.4429788361350100, 174.1790334607675000, 174.9150880853999900, 175.7561951136589800, 176.5973021419179900, 177.1492573582459900,
 177.7012125745739900, 178.0317937417800100, 178.3623749089860000, 179.4658426471360100, 180.5693103852860000, 181.0921120584949900,
 181.6149137317040100, 182.2658410303365000, 182.9167683289690000, 183.5159003521685000, 184.1150323753680000, 184.7445144915345200,
 185.3739966077010000, 186.2213785991975200, 187.0687605906940100, 187.6700674614825200, 188.2713743322709900, 188.8815529121999900,
 189.4917314921289900, 189.9314595514425200, 190.3711876107560100, 191.3661627434059900, 192.3611378760560100, 193.0791054011015100,
 193.7970729261470000, 194.0144780777540100, 194.2318832293609900, 195.1819444420224800, 196.1320056546840100, 196.6227259381300000,
 197.1134462215760000, 197.9599609788614900, 198.8064757361470100, 199.4842575824230000, 200.1620394286989900, 200.5318286497340000,
 200.9016178707690000, 201.5810979333505200, 202.2605779959320100, 203.2408243880449800, 204.2210707801579900, 204.6065371176279900,
 204.9920034550980000, 205.7019572513544900, 206.4119110476110000, 206.8646442647495000, 207.3173774818880000, 208.2725649872680000,
 209.2277524926479900, 209.6654690050235100, 210.1031855173990000, 210.9682986689115000, 211.8334118204240000, 212.1855096788185100,
 212.5376075372130100, 213.1497248192455100, 213.7618421012780100, 214.7778302416470000, 215.7938183820160100, 216.2486168241094900,
 216.7034152662030000, 217.1426733421205000, 217.5819314180380100, 218.3773009924910000, 219.1726705669439900, 219.7891461387905000,
 220.4056217106370100, 221.1670851089455100, 221.9285485072539900, 222.4658258439415000, 223.0031031806290100, 223.5626708342879900,
 224.1222384879469900, 224.7077499815760000, 225.2932614752049900, 226.1407249817899900, 226.9881884883749900, 227.6968473929055100,
 228.4055062974360100, 228.6822649650555100, 228.9590236326750100, 229.6445771052399900, 230.3301305778049900, 231.2153061624370000,
 232.1004817470690100, 232.5742727461840200, 233.0480637452990000, 233.6999257235660000, 234.3517877018329900, 235.0940032364870000,
 235.8362187711410100, 236.0388896328084900, 236.2415604944760000, 237.3893367722295100, 238.5371130499829900, 238.9378005928014900,
 239.3384881356200000, 239.9825996525065100, 240.6267111693929900, 241.0523160488020000, 241.4779209282110100, 242.3534260539750000,
 243.2289311797389900, 243.8710498816635100, 244.5131685835880000, 245.0390249818510100, 245.5648813801140000, 246.1444699574195200,
 246.7240585347250000, 247.3595885587320100, 247.9951185827390000, 248.5898137738120100, 249.1845089648850000, 250.1355360576704900,
 251.0865631504559900, 251.3617394950350100, 251.6369158396140100, 252.1301808096409900, 252.6234457796680000, 253.4689415648954900,
 254.3144373501230000, 255.0761776999324900, 255.8379180497420000, 256.1712514985450200, 256.5045849473480100, 257.3349238988495200,
 258.1652628503509800, 258.4998666869379900, 258.8344705235250000, 259.6324713292250400, 260.4304721349250300, 261.1720435612875200,
 261.9136149876500200, 262.3986161650555000, 262.8836173424609800, 263.4689376152945200, 264.0542578881280100, 264.4935585146739600,
 264.9328591412199800, 265.9667551704674800, 267.0006511997149800, 267.4010490921584700, 267.8014469846020200, 268.2923765233250000,
 268.7833060620479800, 269.5306966147650200, 270.2780871674820000, 270.7665354309655200, 271.2549836944489800, 272.0054311480710100,
 272.7558786016929800, 273.4636684571060000, 274.1714583125190100, 274.6022797745495200, 275.0331012365800200, 275.4460236238314800,
 275.8589460110829900, 276.8156343247010200, 277.7723226383190000, 278.2880032628029900, 278.8036838872869800, 279.4804143791309900,
 280.1571448709750000, 280.4751259101919900, 280.7931069494089800, 281.5863737064850100, 282.3796404635609700, 282.9920919487219600,
 283.6045434338830100, 284.2650696286500000, 284.9255958234169800, 285.5029145655669800, 286.0802333077169800, 286.6148289779795300,
 287.1494246482420200, 287.5639504557815400, 287.9784762633210000, 289.1153116292480300, 290.2521469951750000, 290.4647216592179600,
 290.6772963232609800, 291.2546604560529900, 291.8320245888450000, 292.5172293831590200, 293.2024341774729800, 293.7648513144664500,
 294.3272684514599900, 295.0653696380894600, 295.8034708247190000, 296.3520684817870000, 296.9006661388550000, 297.4897701268735100,
 298.0788741148920200, 298.4745972109850000, 298.8703203070779800, 299.6523900788059800, 300.4344598505339800, 301.1771511895655100,
 301.9198425285969700, 302.4188900558569900, 302.9179375831170100, 303.2895278117255200, 303.6611180403339700, 304.3631634528765100,
 305.0652088654189800, 305.9301714145655100, 306.7951339637119800, 307.0406094502134800, 307.2860849367149900, 308.2042839835685300,
 309.1224830304220200, 309.4311953221985000, 309.7399076139749900, 310.2817912874600100, 310.8236749609450300, 311.6900209142530100,
 312.5563668675610000, 313.2171546613145100, 313.8779424550680200, 314.1555538121784800, 314.4331651692890000, 315.0840756471079700,
 315.7349861249269900, 316.3736520835800000, 317.0123180422330100, 317.7342676492940000, 318.4562172563549900, 319.0179542766950400,
 319.5796912970350300, 319.9929974707640100, 320.4063036444929900, 321.2041357244914900, 322.0019678044899900, 322.2705426141409900,
 322.5391174237919900, 323.5287810487579900, 324.5184446737239900, 324.9966143589150000, 325.4747840441060000, 325.9666626319855100,
 326.4585412198650300, 326.8592364931885200, 327.2599317665120100, 328.2300148520455400, 329.2000979375790200, 329.6180737639625100,
 330.0360495903460100, 330.6237262449640200, 331.2114028995820300, 331.8921747139080500, 332.5729465282340100, 332.8772274284640400,
 333.1815083286940100, 333.9751041116865100, 334.7686998946790000, 335.4607087245560100, 336.1527175544330200, 336.6461903046050000,
 337.1396630547769700, 337.6829728704069600, 338.2262826860370000, 338.6187676506185100, 339.0112526152000200, 339.9266024837764900,
 340.8419523523530200, 341.4340527678590400, 342.0261531833650100, 342.3560057103620100, 342.6858582373590100, 343.3847621969640000,
 344.0836661565690000, 344.6467207489574800, 345.2097753413460200, 345.7362679042240100, 346.2627604671020000, 347.0937042907324900,
 347.9246481143629800, 348.4532232514550300, 348.9817983885470200, 349.2080704225735400, 349.4343424566000100, 350.2057777829545000,
 350.9772131093089900, 351.6844906762170200, 352.3917682431250000, 353.0425507844040000, 353.6933333256830000, 353.9975355335735100,
 354.3017377414640200, 355.0230890020304700, 355.7444402625969800, 356.1860437381819800, 356.6276472137669800, 357.4579611533545200,
 358.2882750929420000, 358.6923681427740000, 359.0964611926060000, 359.9037511085824700, 360.7110410245589900, 360.9542670303835100,
 361.1974930362080200, 361.7372309646875100, 362.2769688931670100, 363.3483466625010000, 364.4197244318349900, 364.6117302619734900,
 364.8037360921119900, 365.4267128051905000, 366.0496895182690200, 366.5876122266720400, 367.1255349350750000, 367.7796749124414600,
 368.4338148898079900, 368.9677541108519600, 369.5016933318959800, 370.2853547112305300, 371.0690160905650100, 371.4195560028575200,
 371.7700959151500200, 372.3274784008705200, 372.8848608865910100, 373.4098649010264800, 373.9348689154620000, 374.7408718212350300,
 375.5468747270080000, 376.1153878324914800, 376.6839009379750100, 377.1215621060799800, 377.5592232741850000, 377.9730761599735200,
 378.3869290457619700, 379.2220011140379900, 380.0570731823140100, 380.5528716526745300, 381.0486701230349800, 381.6442182710254700,
 382.2397664190160100, 382.8624293866095100, 383.4850923542030000, 383.9289690179419900, 384.3728456816809900, 384.7940366767454600,
 385.2152276718100000, 386.1822184797205200, 387.1492092876309800, 387.6156758826730300, 388.0821424777150200, 388.4299302197835000,
 388.7777179618519800, 389.4734152953744800, 390.1691126288969800, 390.6255191744289700, 391.0819257199610200, 391.9668443166615400,
 392.8517629133619900, 393.1475853121864900, 393.4434077110109900, 394.1317024014024900, 394.8199970917939900, 395.3123033476059600,
 395.8046096034179900, 396.2695325129830100, 396.7344554225480200, 397.4924356996430100, 398.2504159767380000, 398.9079634494405000,
 399.5655109221430100, 400.1291933454275000, 400.6928757687119900, 400.7596533554834700, 400.8264309422550000, 401.8223747005774800,
 402.8183184589000200, 403.3628617511919900, 403.9074050434840000, 404.4264956320479900, 404.9455862206119700, 405.5534170092499800,
 406.1612477978880000, 406.6531316798875100, 407.1450155618870200, 407.5922711174205200, 408.0395266729540300, 408.8138698968949700,
 409.5882131208359700, 410.2299379921594800, 410.8716628634829800, 411.2661171571679700, 411.6605714508530100, 412.1958592042179800,
 412.7311469575830100, 413.2002161764395400, 413.6692853952960100, 414.5492906341085000, 415.4292958729209900, 415.8999435497685200,
 416.3705912266160000, 416.7499307201144900, 417.1292702136129800, 417.8433734702200000, 418.5574767268270200, 419.0224680745669700,
 419.4874594223069800, 420.0082066601449900, 420.5289538979830000, 421.5196634637815100, 422.5103730295800200, 422.5988138173570400,
 422.6872546051340000, 423.3770750489360400, 424.0668954927380100, 424.4451421563715000, 424.8233888200049900, 425.7779029143274600,
 426.7324170086499700, 427.0582174099960200, 427.3840178113420100, 428.0410628827049800, 428.6981079540680100, 429.1437469632765000,
 429.5893859724849900, 430.1034719041904900, 430.6175578358959800, 431.3391651759329800, 432.0607725159699700, 432.5491078177739700,
 433.0374431195779700, 433.7383977592654700, 434.4393523989530200, 434.8937532796479700, 435.3481541603429700, 435.6261589188965200,
 435.9041636774500100, 436.7431915383215300, 437.5822193991929800, 438.3701638271954800, 439.1581082551979900, 439.3478388501870300,
 439.5375694451760200, 440.1073986725155000, 440.6772278998549800, 441.4234839085185100, 442.1697399171820300, 442.4733921042020500,
 442.7770442912220100, 443.6076673995555100, 444.4382905078890100, 444.9510148381509700, 445.4637391684129900, 445.9560353951749800,
 446.4483316219369700, 446.8751702633740000, 447.3020089048109800, 447.9044699967345200, 448.5069310886580100, 449.2817756977880200,
 450.0566203069179800, 450.5636981061715000, 451.0707759054250200, 451.4553741832010000, 451.8399724609769800, 452.3599209996280000,
 452.8798695382790200, 453.5666919338165100, 454.2535143293540000, 454.8380058753664900, 455.4224974213789800, 455.9074977990139800,
 456.3924981766489900, 457.0875243391919900, 457.7825505017349900, 458.1923413575374900, 458.6021322133399800, 458.9175077936839700,
 459.2328833740280100, 460.2313717318564800, 461.2298600896850200, 461.6615610968179900, 462.0932621039510200, 462.5389182149010000,
 462.9845743258509900, 463.5294622301189500, 464.0743501343869800, 464.5794231409954600, 465.0844961476040000, 465.8248951091794700,
 466.5652940707549900, 467.0175862538899900, 467.4698784370249900, 468.1613209439984800, 468.8527634509720200, 469.1314584374949800,
 469.4101534240180100, 470.1301361611025400, 470.8501188981870200, 471.2043037592565000, 471.5584886203259900, 472.5040640178934800,
 473.4496394154609800, 473.8506503733294700, 474.2516613311980200, 474.5570323737690000, 474.8624034163399900, 475.4846561263809700,
 476.1069088364220100, 476.8157068625445200, 477.5245048886670200, 478.0548398833785200, 478.5851748780900200, 479.0744828685070000,
 479.5637908589239900, 480.2086110071695100, 480.8534311554150200, 481.2445781552495400, 481.6357251550840000, 482.0979073556520100,
 482.5600895562200200, 483.4667706596030100, 484.3734517629860100, 484.6989297239215400, 485.0244076848570100, 485.7291299499395300,
 486.4338522150220000, 486.6473366937744900, 486.8608211725269800, 487.5312837739579700, 488.2017463753890100, 488.9646652534430000,
 489.7275841314970000, 490.2267479978469900, 490.7259118641969700, 491.0537843254929800, 491.3816567867889900, 492.1950402846929900,
 493.0084237825969900, 493.2452536727914800, 493.4820835629860200, 494.1614827089025000, 494.8408818548189700, 495.5527390135170000,
 496.2645961722150200, 496.8896232014994900, 497.5146502307840100, 497.6396003860210200, 497.7645505412580200, 498.3929811437875500,
 499.0214117463170300, 499.8095928389215000, 500.5977739315259800, 501.0341874639989900, 501.4706009964720000, 502.1230295960040200,
 502.7754581955359800, 503.1209443725789500, 503.4664305496219800, 504.0602777555265000, 504.6541249614310200, 505.1966322004969900,
 505.7391394395630200, 506.3395962606205100, 506.9400530816780000, 507.6193633834200200, 508.2986736851619900, 508.6582091161375300,
 509.0177445471130100, 509.5905665573535000, 510.1633885675939800, 510.4765729370004700, 510.7897573064070100, 511.8098326444135200,
 512.8299079824199700, 513.1767556179374900, 513.5236032534550100, 513.8736603915920100, 514.2237175297290200, 515.0123035144799800,
 515.8008894992309500, 516.1062370231754800, 516.4115845471200100, 517.0737799813094900, 517.7359754154989600, 518.3764810439570300,
 519.0169866724149900, 519.5921396938374500, 520.1672927152600300, 520.5199802491405300, 520.8726677830210300, 521.3570745733179600,
 521.8414813636150000, 522.4812338083570500, 523.1209862530989800, 523.8619602896349100, 524.6029343261709500, 524.9819415772969900,
 525.3609488284230300, 525.8825423853270400, 526.4041359422310500, 526.8024010448534700, 527.2006661474760000, 527.9781556958969300,
 528.7556452443179800, 529.1675565719859800, 529.5794678996539900, 530.2109018767159800, 530.8423358537779700, 531.4489158967860500,
 532.0554959397940100, 532.4354272072200700, 532.8153584746460200, 533.1618708516764400, 533.5083832287069800, 534.4196705075110000,
 535.3309577863150300, 535.8330791129419600, 536.3352004395690100, 536.7811537422779800, 537.2271070449869500, 537.6269669076599500,
 538.0268267703329500, 538.7388233831894700, 539.4508199960459900, 539.8356020969924900, 540.2203841979389800, 541.0639916680265700,
 541.9075991381140500, 542.1850810996149900, 542.4625630611160400, 543.0793279107324500, 543.6960927603489600, 544.2407770123329600,
 544.7854612643169500, 545.0844520557784600, 545.3834428472399600, 546.2853504650464600, 547.1872580828529600, 547.7024686204545100,
 548.2176791580559400, 548.5806057384209000, 548.9435323187859700, 549.3830859962535000, 549.8226396737210300, 550.4712535385180000,
 551.1198674033149700, 551.8673315342705300, 552.6147956652259800, 552.8245703618704300, 553.0343450585149900, 553.8845730984160100,
 554.7348011383170400, 555.0579315852445500, 555.3810620321720500, 555.7998760082015200, 556.2186899842309900, 556.8906182534744900,
 557.5625465227179800, 558.2761151093234300, 558.9896836959289900, 559.3895522742295700]

#one value linearly interpolated between successive zeros (649 values)
zeta_zeros=[14.1347251417347000, 17.5783823902531290, 21.0220396387715600, 23.0164486094586240, 25.0108575801456890, 27.7178668530025970,
 30.4248761258595090, 31.6799688567993480, 32.9350615877391920, 35.2606198732824370, 37.5861781588256820, 39.2524485854865900,
 40.9187190121474980, 42.1228961465312470, 43.3270732809150020, 45.6661120810410780, 48.0051508811671610, 48.8894916794197340,
 49.7738324776723000, 51.3720769776933820, 52.9703214777144570, 54.7082845873889240, 56.4462476970633920, 57.8966458498328720,
 59.3470440026023520, 60.0894112636060810, 60.8317785246098100, 62.9721612863457100, 65.1125440480816020, 66.0961772887878850,
 67.0798105294941680, 68.3131061203340830, 69.5464017111739850, 70.8067796928279450, 72.0671576744819050, 73.8859241867829160,
 75.7046906990839260, 76.4247653839793630, 77.1448400688747990, 78.2411075445620840, 79.3373750202493680, 81.1238779371676910,
 82.9103808540860290, 83.8229369173015470, 84.7354929805170510, 86.0803837968211380, 87.4252746131252250, 88.1171929103798420,
 88.8091112076344590, 90.6505052390964750, 92.4918992705584910, 93.5716216555391900, 94.6513440405198880, 95.2609891343825980,
 95.8706342282453080, 97.3509142232194900, 98.8311942181936870, 100.0745226119625400, 101.3178510057313800, 102.5216945231048600,
 103.7255380404783400, 104.5860805464022100, 105.4466230523260900, 106.3076171183012400, 107.1686111842764000, 109.0990733637230400,
 111.0295355431696700, 111.4520973600811500, 111.8746591769926400, 113.0974400462226700, 114.3202209154527100, 115.2734506181551200,
 116.2266803208575500, 117.5087315934168800, 118.7907828659762100, 120.0804539341984300, 121.3701250024206500, 122.1584771479866200,
 122.9468292935525800, 123.6018239239491800, 124.2568185543457700, 125.8867512169711300, 127.5166838795965000, 128.5476940397762700,
 129.5787041999560600, 130.3331963654443700, 131.0876885309326700, 132.2927128669651300, 133.4977372029976000, 134.1271234781857500,
 134.7565097533738800, 136.4362759039536600, 138.1160420545334400, 138.9261255033274000, 139.7362089521213900, 140.4299581780712700,
 141.1237074040211300, 142.1177766058208900, 143.1118458076206300, 144.5564141471930700, 146.0009824867655100, 146.7118739146625600,
 147.4227653425596100, 148.7381428816722500, 150.0535204207848800, 150.4893890165131700, 150.9252576122414700, 151.9749757117201600,
 153.0246938111988900, 154.5688015527183800, 156.1129092942378800, 156.8552505559159700, 157.5975918175940600, 158.2237899945072900,
 158.8499881714205100, 160.0194761545082500, 161.1889641375960300, 162.1098369123890300, 163.0307096871820000, 164.2838894375412100,
 165.5370691879004100, 166.3607545830374800, 167.1844399781745100, 168.1394776968716700, 169.0945154155688200, 169.5032459474902600,
 169.9119764794116900, 171.6617564995016200, 173.4115365195915500, 174.0828640214786600, 174.7541915233657300, 175.5978129105381000,
 176.4414342977104300, 177.4094210369052000, 178.3774077760999700, 179.1469458981785000, 179.9164840202570000, 181.0617812523117300,
 182.2070784843664600, 183.5407731663769900, 184.8744678483875000, 185.2366257630474700, 185.5987836777074700, 186.4138531306046500,
 187.2289225835018600, 188.3225406197593900, 189.4161586560169300, 190.7214075083653600, 192.0266563607137900, 192.5531914822797400,
 193.0797266038457000, 194.1725616416874800, 195.2653966795292300, 196.0709392602437900, 196.8764818409583200, 197.4458957586051000,
 198.0153096762519200, 199.6400308099778700, 201.2647519437038000, 201.8791732289221800, 202.4935945141405400, 203.3416331586225300,
 204.1896718031045500, 204.7921845026339200, 205.3946972021632900, 206.6504780449847700, 207.9062588878062200, 208.7413843023312600,
 209.5765097168562600, 210.6336861561108000, 211.6908625953653000, 212.5193909775389900, 213.3479193597126800, 213.9474820716020500,
 214.5470447834914300, 215.3582916458775600, 216.1695385082637100, 217.6185674286425600, 219.0675963490213900, 219.8912575941677100,
 220.7149188393140100, 221.0728121970036600, 221.4307055546933300, 222.7188529046488300, 224.0070002546043200, 224.4951624620933000,
 224.9833246695822900, 226.2023844746307800, 227.4214442796792900, 228.3794287926023100, 229.3374133055253600, 230.2938010030122800,
 231.2501887004991700, 231.6187119768397100, 231.9872352531802400, 232.8403197160442900, 233.6934041789083100, 235.1088169223622500,
 236.5242296658161900, 237.1470250733706800, 237.7698204809252000, 238.6626490271264100, 239.5554775733276400, 240.3023176847721100,
 241.0491577962165800, 241.9362148652196000, 242.8232719342225900, 243.4470852156503600, 244.0708984970781600, 245.6039442859878400,
 247.1369900748975100, 247.6194900675229700, 248.1019900601484700, 248.8378398524278300, 249.5736896447072000, 250.2943187198616200,
 251.0149477950160100, 252.0424672715077500, 253.0699867479994800, 254.1881216014567700, 255.3062564549140300, 255.8434850746742500,
 256.3807136944344600, 257.4955765929829600, 258.6104394915313900, 259.2424232406046900, 259.8744069896779900, 260.3397457471374400,
 260.8050845045968900, 262.1894892047334900, 263.5738939048701500, 264.5658728718732400, 265.5578518388763300, 266.0864128101886800,
 266.6149737815010800, 267.2684444321626000, 267.9219150828240600, 268.9461820534108400, 269.9704490239976200, 270.7322523328213000,
 271.4940556416449900, 272.4768324150241500, 273.4596091884033100, 274.5235509188735800, 275.5874926493438600, 276.0197710762383800,
 276.4520495031329100, 277.3513965164874000, 278.2507435298419400, 278.7399972287935300, 279.2292509277451700, 280.8471828463986000,
 282.4651147650520800, 282.8381502491429800, 283.2111857332338900, 284.0235748570693200, 284.8359639809047500, 285.7517046719538000,
 286.6674453630029100, 287.2896829322125400, 287.9119205014221700, 288.7458877153204600, 289.5798549292188100, 290.7130731291431400,
 291.8462913290674100, 292.7023627342118700, 293.5584341393562800, 294.2619018793109300, 294.9653696192655200, 295.2693122491118600,
 295.5732548789582700, 296.7762659704508200, 297.9792770619434400, 298.9098015578323400, 299.8403260537213000, 300.7448257579577600,
 301.6493254621941600, 302.1730375259005500, 302.6967495896069000, 303.7805604652321000, 304.8643713408573000, 305.2966419714470600,
 305.7289126020368100, 306.4742043651034500, 307.2194961281700800, 308.6644796374359900, 310.1094631467019000, 310.6373023385289100,
 311.1651415303559800, 311.7964713554784500, 312.4278011806009100, 313.2065434558799100, 313.9852857311589100, 314.7304509103173600,
 315.4756160894757500, 316.6052110159229800, 317.7348059423702000, 318.2939550993434100, 318.8531042563166100, 320.0066192827150600,
 321.1601343091135700, 321.6523464907982700, 322.1445586724829600, 322.8057641149974900, 323.4669695575120700, 324.1649178046258200,
 324.8628660517396200, 326.1533836568225400, 327.4439012619054700, 328.2384864711932000, 329.0330716804809300, 329.4931557043573800,
 329.9532397282338800, 330.7138536554486400, 331.4744675826634100, 332.5599230537666200, 333.6453785248698400, 333.9283666790571500,
 334.2113548332444000, 335.5266026308175400, 336.8418504283906700, 337.5909216395986000, 338.3399928508066000, 339.0991047880850700,
 339.8582167253635500, 340.4502389182050600, 341.0422611110465700, 341.5485693107050900, 342.0548775103636100, 343.3582902253080000,
 344.6617029402523300, 345.5047867531311600, 346.3478705660099300, 346.8102740752152000, 347.2726775844204800, 348.2944692275583500,
 349.3162608706961700, 349.8623401099441700, 350.4084193491921200, 351.1435341872756900, 351.8786490253592700, 352.6837747570390300,
 353.4889004887188000, 354.7532377329918600, 356.0175749772649300, 356.5844386146523000, 357.1513022520396100, 357.5519936768359300,
 357.9526851016322600, 358.8482200273733700, 359.7437549531144300, 360.5165583244595400, 361.2893616958046400, 362.3103461373892700,
 363.3313305789738500, 364.0336773465314200, 364.7360241140889900, 365.4743672012101600, 366.2127102883313200, 367.1031428850358200,
 367.9935754817403200, 368.4810067887373200, 368.9684380957343700, 369.5096786539202200, 370.0509192121060100, 371.5564237921094000,
 373.0619283721128500, 373.4634011415107000, 373.8648739109085600, 374.8453933388239500, 375.8259127667393500, 376.0750024987037200,
 376.3240922306680400, 377.3803862403167400, 378.4366802499654900, 379.1548277982489000, 379.8729753465323700, 380.6787219818594400,
 381.4844686171865100, 382.4639990333614600, 383.4435294495364700, 384.1998231322000900, 384.9561168148636600, 385.4087088304189600,
 385.8613008459742600, 386.5420955341811600, 387.2228902223880000, 388.0345092883101200, 388.8461283542322300, 390.1511059589351500,
 391.4560835636380600, 391.8505834515785900, 392.2450833395191100, 392.8364135919765700, 393.4277438444340200, 394.5053069277138300,
 395.5828700109937000, 395.9823621167929600, 396.3818542225922100, 397.1502952161032500, 397.9187362096142200, 398.9519280429045700,
 399.9851198761949100, 400.9121742383640600, 401.8392286005332000, 402.3505731822097000, 402.8619177638861400, 403.5491797820470800,
 404.2364418002080100, 404.6854146300589700, 405.1343874599099300, 406.3579239234030600, 407.5814603868962000, 408.2643529446236200,
 408.9472455023511100, 409.7305573478588500, 410.5138691933666500, 411.2430684988227100, 411.9722678042787700, 412.6175019372319100,
 413.2627360701850400, 414.1407729126700600, 415.0188097551551300, 415.2370123757248700, 415.4552149962946100, 416.9214603929147000,
 418.3877057895347800, 419.1245353038435700, 419.8613648181523100, 420.2525962215970600, 420.6438276250418100, 421.3602688419342700,
 422.0767100588267300, 422.8966448431542600, 423.7165796274817900, 424.3932310609715700, 425.0698824944613500, 426.1393537892679400,
 427.2088250840745900, 427.6683695803456000, 428.1279140766166600, 429.2283297537776400, 430.3287454309386200, 430.8150261808211200,
 431.3013069307036200, 431.7199743326460700, 432.1386417345885900, 433.0139301077579100, 433.8892184809272400, 435.0251124567871000,
 436.1610064326469600, 436.8713523001578100, 437.5816981676686100, 438.1017184119704100, 438.6217386562722100, 439.2700904353214400,
 439.9184422143706600, 440.8008207077798500, 441.6831992011890400, 442.2938727518992400, 442.9045463026094500, 443.6119412900843000,
 444.3193362775591600, 445.5899794869943700, 446.8606226964295200, 447.1511634454614100, 447.4417041944933000, 448.2951249397583000,
 449.1485456850232900, 449.6377457326684100, 450.1269457803135200, 450.7651271128511300, 451.4033084453888000, 452.6950231260333300,
 453.9867378066779200, 454.4807107876473500, 454.9746837686167900, 455.6515552289314400, 456.3284266892460400, 457.1161598766744900,
 457.9038930641029900, 458.7086541726045000, 459.5134152811060100, 459.8006798516408900, 460.0879444221758400, 461.0766558485291900,
 462.0653672748825300, 463.0613270927153700, 464.0572869105482700, 464.8644130609596900, 465.6715392113711100, 466.1209130710987000,
 466.5702869308262300, 467.0046665705439200, 467.4390462102616700, 468.4875253846868200, 469.5360045591120300, 470.1548300186068400,
 470.7736554781016600, 471.7864150700052100, 472.7991746619088100, 473.3172035035242400, 473.8352323451396700, 474.7177858572577000,
 475.6003393693757700, 476.1846773034301300, 476.7690152374845000, 477.4221395020777000, 478.0752637666709600, 478.5087226506528900,
 478.9421815346348100, 480.3862604554607300, 481.8303393762865900, 482.3325610836344600, 482.8347827909823900, 483.3431050017324500,
 483.8514272124825200, 484.6952876709192400, 485.5391481293560200, 486.0339331955036600, 486.5287182616512500, 487.4546426758343500,
 488.3805670900174500, 489.0211643339868000, 489.6617615779561600, 490.5302915858095500, 491.3988215936630000, 492.3566315877241100,
 493.3144415817852700, 493.6362196935773500, 493.9579978053694700, 494.6584133137503700, 495.3588288221312700, 495.8942625189452000,
 496.4296962157591200, 497.5052393227228400, 498.5807824296865600, 499.4449336856885100, 500.3090849416905100, 500.9567659534179700,
 501.6044469651454800, 501.9403586461318700, 502.2762703271182500, 503.3880218202730200, 504.4997733134277400, 504.9575025278360800,
 505.4152317422444300, 505.9396922258839600, 506.4641527095235500, 507.6324265229956700, 508.8007003364678000, 509.5324641400703200,
 510.2642279436728400, 510.9132588220237500, 511.5622897003746100, 512.0927171158909900, 512.6231445314074300, 513.1460650434405600,
 513.6689855554736800, 514.5520213613865500, 515.4350571672994200, 516.5123628698834200, 517.5896685724674200, 517.9119458600087000,
 518.2342231475501000, 519.1702667796366800, 520.1063104117232600, 520.8157519306075800, 521.5251934494920100, 521.9909448136111200,
 522.4566961777302400, 523.2086135348730500, 523.9605308920158700, 524.5189582896478000, 525.0773856872796200, 526.4905136442760100,
 527.9036416012724000, 528.1549277267824900, 528.4062138522926900, 529.1062200854997900, 529.8062263187068800, 530.3365721013340100,
 530.8669178839610400, 531.7775504561273000, 532.6881830282936800, 533.2339068910312100, 533.7796307537687400, 534.7219724148210400,
 535.6643140758732200, 536.3670365794978400, 537.0697590831223400, 537.7491426296851400, 538.4285261762479400, 539.3208462762380600,
 540.2131663762281700, 540.4222783117616000, 540.6313902472951400, 541.2394136842482300, 541.8474371212013200, 543.0856636111033100,
 544.3238901010053000, 544.9803616749700300, 545.6368332489348600, 546.3238726535285000, 547.0109120581222400, 547.4712627113058300,
 547.9316133644892900, 548.7145904635754100, 549.4975675626614000, 550.2337888010727100, 550.9700100394839000, 551.5097911200243700,
 552.0495722005648500, 552.9072721598618000, 553.7649721191588700, 554.7784963404207000, 555.7920205616825300, 556.3457484842689400,
 556.8994764068553400, 557.2320677894570100, 557.5646591720585500, 558.4404481003703000, 559.3162370286821700, 559.7785222629888700,
 560.2408074972956900, 561.4000075566707400, 562.5592076160458000, 563.3600433634160300, 564.1608791107861400, 564.3334675244680100,
 564.5060559381498700, 565.6024218104789700, 566.6987876828079600, 567.2152727919924500, 567.7317579011769300, 568.3278565404032200,
 568.9239551796293900, 569.4875349810465700, 570.0511147824636300, 571.2355494574582100, 572.4199841324527800, 573.0172973296055300,
 573.6146105267581600, 574.3542482706265000, 575.0938860144948400, 575.4505665777118100, 575.8072471409287800, 576.4231253065135000,
 577.0390034720982200]

#sorted list of zeros of zeta and L with imaginary parts less than 1000 
#all_zeros = sorted(L_mod4_zeros_list + zeta_zeros()[0:648])
all_zeros = sorted(L_mod4_zeros_list + zeta_zeros)

x = var('x')
def Riemann_main(x, numbersums, numberzeros, numbertrivials):
    return sum([moebius(n)/n*Ei((1/n)*log(x)) for n in xrange(1, numbersums+1)])

def nontrivial_correction(x, numbersums, numberzeros, numbertrivials):
    zz = all_zeros[0:numberzeros]
    n_dict = {n:[] for n in xrange(1, numbersums+1)}
    for n in xrange(1, numbersums+1):
        if moebius(n)==1:
            n_dict[n] = -sum([(1/n)*2*real(Ei((0.5+I*theta)/n*log(x))) for theta in zz])
        elif moebius(n)==0:
            n_dict[n] = 0
        elif moebius(n)==-1:
            n_dict[n] = sum([(1/n)*2*real(Ei((0.5+I*theta)/n*log(x))) for theta in zz])
    return sum(n_dict.values())

def trivial_zeros_correction(x, numbersums, numberzeros, numbertrivials):
    n_dict = {n:[] for n in xrange(1, numbersums + 1)}
    for n in xrange(1, numbersums+1):
        if moebius(n)==1:
            n_dict[n] = -sum([(m/n)*Ei((-m/n)*log(x)) for m in xrange(1, numbertrivials+1)])
        elif moebius(n)==0:
            n_dict[n] = 0
        elif moebius(n)==-1:
            n_dict[n] = sum([(m/n)*Ei((-m/n)*log(x)) for m in xrange(1, numbertrivials+1)])
    return sum(n_dict.values())

def pi_G(x, numbersums, numberzeros,  numbertrivials):
    return sum([Riemann_main(x, numbersums, numberzeros, numbertrivials), nontrivial_correction(x, numbersums, numberzeros,  numbertrivials), trivial_zeros_correction(x, numbersums, numberzeros,  numbertrivials)])

mindistance = 2
maxdistance = 10000
nontrivialzeros = 400
trivialzeros = 200

pi_G_plot = plot(pi_G(x, ceil(log(maxdistance)-log(2))+1, nontrivialzeros, trivialzeros), (x, mindistance, maxdistance), rgbcolor = 'blue', legend_label=r'$\pi_0^G(x)$, 400 non-trivial "zeros" ')
G_primes = list_plot(prime_counter(maxdistance), linestyle='steps-post', plotjoined=True, rgbcolor = 'red', legend_label = r'$\pi_G(x)$')

p = plot(G_primes + pi_G_plot)
p.set_legend_options(loc=(0.05, 0.8))
p.show(legend_font_size=18, fontsize = 12, ymin=-1)
p.save('nugauss2.pdf',legend_font_size=18, fontsize=12, ymin=-1)