Пример #1
0
def numsubplots(n, recursive=False):
    """
    Define the best arrangement of subplots for a
    given number of plots.

    Parameters:
        n:
            Number of total plots needed.
        recursive:
            Whether to return current number of subplots.
            Used for recursive calls.
    Returns:
        p:
            A list containing the ideal arrangement
            of subplots in the format of [nrows, ncolumns].
        n:
            Current number of subplots. Returned only
            when recursively calling the function.

    Ported to Python by Yunus Can Erol on Dec 2017
    from mathworks.com/matlabcentral/fileexchange/
    26310-numsubplots-neatly-arrange-subplots
    Original by Rob Campbell, Jan 2010

    Requires prime factorization and checking primality
    which is not provided by Python by default; therefore
    a custom package (primefac) is required.

    """
    import primefac

    while primefac.isprime(n) and n > 4:
        n += 1

    p = primefac.prime_factorize(n)
    if len(p) == 1:
        p = [1] + p
        if recursive:
            return p, n
        else:
            return p

    while len(p) > 2:
        if len(p) >= 4:
            p[0] = p[0] * p[-2]
            p[1] = p[1] * p[-1]
            del p[-2:]
        else:
            p[0] = p[0] * p[1]
            del p[1]
        p = sorted(p)

    while p[1] / p[0] > 2.5:
        N = n + 1
        p, n = numsubplots(N, recursive=True)

    if recursive:
        return p, n
    else:
        return p
Пример #2
0
def main():
    print 'starting {}'.format(__file__.split('/')[-1])
    startTime = time.time()

    numPrime = 0
    numCorners = 1

    for sideLength in count(3, 2):
        # if sideLength > 7:
        #     break
        oddSquare = sideLength**2
        corners = []
        for x in xrange(1, 4):
            corner = oddSquare - (x * (sideLength - 1))
            corners.append(corner)
            if primefac.isprime(corner):
                numPrime += 1
        numCorners += 4
        percent = numPrime/numCorners
        print '{} {} {}: {}/{} ~= {}'.format(
            sideLength, oddSquare, corners, numPrime, numCorners, percent)
        if percent < 0.1:
            break


    elapsedTime = time.time() - startTime
    print 'elapsedTime: {:.2f} s'.format(elapsedTime)
def num_primes(start, stop, coef):
	num_primes = 0

	for i in xrange(start, stop + 1):
		if primefac.isprime(coef[0] * i ** 2 + coef[1] * i + coef[2]):
			num_primes += 1

	return num_primes
Пример #4
0
def gen_jams(N):
    for p in itertools.count(2**(N - 1) + 1, 2):
        ok = True
        for base in xrange(2, 11):
            v = to_base(base, p)
            if primefac.isprime(v):
                ok = False
                break
        if ok:
            yield p
Пример #5
0
def test_num(potential_coin):
    proofs = []
    for x in range(2, 11):
        coin_in_base = int(potential_coin, x)
        if primefac.isprime(coin_in_base):
            return []
        else:
            if coin_in_base > 2**17:
                proofs.append(primefac.ecm(coin_in_base))
            else:
                proofs.append(primefac.pollardRho_brent(coin_in_base))
    return proofs
Пример #6
0
def prove_coin(string):
    proof = []
    for base in range(2, 11):
        number = int(string, base)
        factor = primefac.pollardRho_brent(number)
        if primefac.isprime(number):
            print("wrong number {} base {}: {}".format(
                string, base, number), file=sys.stderr)
            return None
        assert(number % factor == 0)
        assert(not (factor == 1 or factor == number))
        proof.append(factor)
    return proof
Пример #7
0
def PE_004_no_cheat(n=3):
    if n == 1:
        return 0

    en = 10**n
    en1 = 10**(n - 1)
    for p in generate_palindromes(n):
        p //= 11
        if isprime(p):
            continue
        # Generate numbers such that 11*i has length n
        for i in xrange(en // 11, max(p // en, en1 // 11), -1):
            if p % i == 0:
                return p * 11
Пример #8
0
def PE_004(n=3):
    if n == 1:
        return 0
    elif n % 2 == 0:
        fac_1, fac_2 = cheat_even(n)
        palindrome = fac_1 * fac_2
        if is_palindrome(palindrome):
            return palindrome

    en = 10**n
    en1 = 10**(n - 1)
    for p in generate_palindromes(n):
        p //= 11
        if isprime(p):
            continue
        # Generate numbers such that 11*i has length n
        for i in xrange(en // 11, max(p // en, en1 // 11), -1):
            if p % i == 0:
                return p * 11
Пример #9
0
def f(s, e):
    while s < e:
        if s % 2 == 0:
            s += 1
            continue

        st = '{0:b}'.format(s)
        ll = []
        for b in range(2, 11):
            cur = int(st, b)
            if primefac.isprime(cur):
                break
            ll.append(cur)
        else:
            for i in xrange(len(ll)):
                ll[i] = primefac.primefac(ll[i]).next()

            return (s, ll)

        s += 1
window.setworldcoordinates(-500, -500, 500, 500)

window.bgcolor("black")
pen.color("green")
pen.pensize(.000001)
pen.shape("blank")
pen.speed(0)

window.tracer(10000, 0)
pen.up()

# Generates the basic mapping, no prime distinction
# for x in range(0, 20):
#     for y in range(0, 25):
#         pen.goto(30*x - 500, 30*y - 500)
#         num = (x+y)*(x+y+1)/2 + x
#         pen.write(num)

# Generating the mapping, coloring in the primes green
for x in range(0, 20):
    for y in range(0, 25):
        pen.goto(30 * x - 500, 30 * y - 500)
        num = (x + y) * (x + y + 1) / 2 + x
        if primefac.isprime(num):
            pen.color("red")
        else:
            pen.color("gray")
        pen.write(num)

window.exitonclick()
Пример #11
0
# This works for lines of slope 1 and positive y intercept
primeRate = 10**3
slope = 1 / 5.
intercept = -50000
numLinesToCheck = 50000
data = []
countPrime = 0

for yInt in range(intercept, intercept + numLinesToCheck):
    print yInt
    # If slope is fractional, need to adjust x step
    if slope < 1:
        # Slope < 1, yInt is positive, we will start x at 0
        if yInt > 0:
            for x in range(0, int(1 / slope) * primeRate, int(1 / slope)):
                if primefac.isprime(F(x, slope, yInt)):
                    countPrime += 1
        # Slope < 1, yInt is negative, need to change where x starts
        else:
            for x in range(
                    int(math.ceil(-yInt / slope)),
                    int(1 / slope) * primeRate + int(math.ceil(-yInt / slope)),
                    int(1 / slope)):
                if primefac.isprime(F(x, slope, yInt)):
                    countPrime += 1
    # This is for slope > 0, and positive y intercept x starts at 0
    elif yInt > 0:
        for x in range(0, primeRate):
            if primefac.isprime(F(x, slope, yInt)):
                countPrime += 1
    # Case for slope > 0, negative yInt, needs to start at different x

def F(x):
    return int(
        c1(x) * G1(x / 2, f1(x / 2)) +
        c2(x) * G1(((x - 1) / 2) + 59, f2(((x - 1) / 2) + 59)))


xStart = 0
xEnd = 1000  # 0000
consecutivePrimes = []
consecutiveNonPri = []

while xStart <= xEnd:
    print xStart
    if primefac.isprime(F(xStart)):
        xOrig = xStart
        numOrig = F(xOrig)
        count = 0
        while primefac.isprime(F(xStart + 1)):
            xStart += 1
            count += 1
        consecutivePrimes += [(xOrig, numOrig, count)]
    else:
        xOrig = xStart
        numOrig = F(xOrig)
        count = 0
        while not primefac.isprime(F(xStart + 1)):
            xStart += 1
            count += 1
        consecutiveNonPri += [(xOrig, numOrig, count)]
Пример #13
0
import primefac

prime = 27069980337477312063989736381278370483690677357907699306131065656678733425695684947060303354457458241091454014638486537065289358861728019605161295338409906886748537364734486141476921614887958298856350589613539801894596967036237729050687140945720859628790402644729649385388400722968998897712174901586986975231173134834632125628485397462559804469886493759034717587307532698487503438303928741505094925458218337241314643985626010072923430651135424225336076513696386591203050360275608138155676021372455931252057161696259926417231458247671542381230131636295884921528066240130469518687193570425815779237312998644441596847806673554167605336157926510019984023419201479382321822094142061734407540608154162446762805913052544406395359607340984633107552267188939972847124823673033439329872423028121945519910721793540121199918703304078368472160846235298030714542838338515289930219081473839587333033680714717100087593953849955929528259382068963667386128085397960533247126801894155497648997450199371598222781622068110111595284191933920412173202188128655179841294960008892651604105819579181599439197423399892672807242249243068503974032271192377219773640424073995168768103008735276523110615926154053321745597533220025089944274289203494826927159863013069314047683090434242388069959175754415471246040527743522461862042458535871028320409266234548186807814214537347066332449976869134919857591747449424636865894644786526906915187862625045872345964203106031350015454078720493800908649977113102882227863012884731690418833924024407415596218642854559358233527871642881975332174639120974504513621478650648085914754079974083421278815719489107704173607153789447866041532860018711821142519399705267814105616021994633648003667350242199956177220058281338992511683459986918171733130119701998806438321125093922519780556766976328350151927614360290560422770081685753017611397154502346097155753451187735183809526115987926410889456356780523197876295021814507441635032664482274393544577892995204931926021251449876670243001962164967810755967095330465541259209865364263216071947875391290528016030631280220163626736048367146799620995749413165235878485826584211792788556018127496064381668789265384539816126663268451613545349693635782409216382379147412725378977982411599696295759088429284529810060779476532990284827875868853077537172555203982166674343901395040776286879904501680603582676843444440205508829393328164390353304144367156964850482504371163651562973279944332899121029418523822136573957218691010205012940582161409827368483029857142176289447627353012382584892841475907610731772742360644154688364495915232286664621261147290342381125484144722223569856161238532151354129337447488407307461906282283589202636955132206455267080222136334522899099861718048400545883289908063501262085706821025012487065763480445839591352284544519439895268569275949586403710289474352324616855434914169081120682535063914993052256887260959503189217407101425007218356238508834869946410946714743234436983058932393114273845314216148985121244617886098669329066781631130207009390307181786066525212837591302469836780953751203697254832631536682603919068302071775556636843064443906807018668499508324593429679905463849420053352209027681048797882403018817384194295015223195021989776294067375488308611628954714925691325575297792101866976534682810076450140721859305156096486104754005157085486678045345168224968644530780606347743380677167007989217720863456094063451053251791903840008387632347079439203414862424300236166348753528537544314666856688184391303022642779940448013042136433854208078750874502068634711886943328008981587626728854771118073339768498338626876913328618703632892746541520922522720899591899136408261478013397711383972773191472178651537066789321723185780243899210235285202472400050585289173935343378606298626182960261065143811977279283910276567082047940606236737402448363219078926543669679107407466733339825680690844449768394531485851119938094148279438742494878061036932224340422297248372981517683238993711246218018592301782331752394160847500024475764424059340645088636326459780004658591682571061736455413186238964709330741577091853817132556056382891184507615985793984841979960125233166617144524764287273158514895632053777131313339204305520340533772067780237838186059868797842765670997919936310588013699699130531408568452087496324546079936205247415430364634092157383052727185477589927691539923024386064234864159132202039980234464181510333026426173393880994652042155198197512539053493302283990982423452373958505565338375093711570097318846362890282275296213200512040538148498828076757629724057432465096394106453457273656514995096194696864713153790395105004184791402872692673268897249942183719771209980855820445588007021299971680071167122418921887911210252787279300831206217161819399239849951438361284561033857988634687137679518758683923440497963762411658318156252993977455262756356170321503711742702855442706381532238746699164636294580051957754649706348963255415921058368966035333457530894291989663843053150336234438728227999648829567195810902086289884654401476235327392841041862391498585772961661966534322134199425485330314555850097467717842092115146618739486937457123569732537002882542189656758102342918248074254499704416383589863517979176958814501908395016026893265304028090020707180573027224367131194227980139302756826842311277062864110736278903292554271196706567353665623003292852974530696586004855234670900424993157741753691783373517572423900215490884862389430603777749013719454475047230490737004404964646083642937998481449283596802460330398580515466439409411043893581999566605576202098436046477538273788777655138754229957327244113042531803239142740067342815991966010424479274714024862648124136141260500414652133401744793801113468827239125086476391478497502442559148500617034739360811286860455102083290344084139828757392567209164739100412866729350867857690461613708634327046267286007291549455137536410313371409021237062958886832720787557587917290252548320917752676726288201832857270195998800519599677475188829449518314183339769662246277661264091287959282658793796395059997951340161273257736540343275540655265365696814124466248140208364807858482489743709895372266384744984356565303913177357933261031717522552711561688027720968054197335042230819827975883992190044918401114400185794336532905444366129848732859377829561168404192852853569349446751051659780861222041803144521397327614404512524718005621251839459792469648570672644081316816703464362973106003050400335141918616491546334330966724406235209474960509342463042075687956249639674793923571304566872202721502916813470971100521183824141592305748710118149405960010741792790912129434777610974667671483516934282718019237608093103712058258847103891966425262153145353146888025866780672006851973544999790140591851438144241114040793754165576541327170539738371309900505680273315782577906094480072244776735296327613833030989567210112457257017986505391405476568400115303299212563767321285847908245042570495556909029952714636178339146449331875453594729684730783416458452784049056493234442396362097161148882911207652587357607742254068263729793903999387241423534918233120632953470664445893366814881046402911457111373625796896321038063861535934499472700886246620722351841723570840196670619677861128053565185383893333634976459046736843128793821022411760335987350389185185891948599952355354074819471955048309457573903582736148183508816453131878079295252633976852855169962213104779617901149150929033210287215387880787794056089469904184172819340655562016599034083945595617412265178040655365842571845863524128170100869514034152770848824092856236535891764940161571253750050554331221753412301729472992884927646600238744761023925358878880081026972701761187804989478886526513401305893060086200936505474765034105191562740355440969488632107365027278665564601984951003554176858830772075198166245984665949697780600851326042348537249493031305028484385004954742286055103970531262069973641798666878988165088788139875013079510281401015042367670923293826492732566082189241513075294531243853951205033167165548308259065523235024757440273575723963573923629500559174908520716038029331280690469430078953043023712681712869474593941773215658662981233345630290322241745614299043293909854546991711130499370162608011146684415554259838604830880719469823557629588037795934196093251592971585086133844105353030005637791228372115033738144109519201092467092180182798193176851429694986944036762486137103612241567265354845585365705340543388294802853960950815492327196414149695770199192318558290412933037380606104799970286305541882586978326762615978435489202076031479644484097692137423718684389482883136860919814581076213202797177821281524095400363984491083723438526078759704371360904387768555821441961682448686255750288366316293533680221438896667217040825740840623563666636631982909891627108526754221214884292159506111917126478813004899140329738193959985248547543154925023693067317502698433464565687519348892311234977195096496419193492915005418994468940009634893281928778941485472362712153521072674658808361272807338463950074416364408778702091956352593369163887823143056437678147019699210687311659378332860599844582159219633446608377023275474037247691389663950604064822438237046784735906593596462300407983668792070565476733897470050479737811589332503646396464413484268429476357523298645003043470193939191112700300214475520472594108577864955950801692887935853679089183225405690564896053851473741688879891674449478700605574570168497995529752950992151312788742440269245123765213877962637122562302967067308833484764748395987642849238599042432182920562218793323108586357075335362603309815928401644497417152254614976313407309606523422878869021807938744692528263317588716445095813540826652702176923297977939052184254945146312089814723936910351099404446899770264267468697404557335396391970166728847272012857820063874125702629186641872422767162066713280217510244373242207679842030350192455473553606973778501145748989892409757568107590268256969002834020326370297198139712439542048612701339357621997899420877468486113953051441968171122295478488754053666051110397203358081077203774710918893635559911055478226781411660936673962555348788797133362037238501433530612377131923498850135811192965514330747830879112770827235260646685421016907002540050904526095941106685204362607149452716068062478654985102825278078812176309894557972613301737864383434213902320750563223099158874485537239787555505878199620828066800862733865827433207120199618756090389113522366168077351625539021590411338308324751592609776528820527256187728099748569986170103779361434588663787907429652580824698255554038003660340479088454039367772371626325925925135415496755106357724412819034430354041255334638875537998128776163855695330382848484113526944422800016106046390388828417206342715324726795547297748879537819966114048064498190148990921998564347951524861849929923315966981446522091498996580990935776684730728403309995495867537126509467960903865275292613904619811788458982627825612884652377159112068326173026885138431459669164353179284363667598808983481876741317482580357024755559342475646458112321409046773498790356377830442072097130356745115220727236350155977416990572891370391842369322093770191679451338257099314707424303594845878761615839305822557858897069391154748527562255065529494707688107327153417726618803679075101131973008009008299310167157355077839947393972083131411088161710557099711048864334516914606596884028075050779455230802331456745179367738450607860882650830334641596284243181938663893002881318962016072711318425685172014007080412991662751966094432413618125789377562116343363410556234675036271807771257431297919750264492971860081793176683350092260749473200273125735018920711843834500745638001763866473241937494784314157961785858898086942144168125076198857156578419398085588498331185246390941612976082168655103418474547369822384129530573758092563043506169008756222157041144417194312880868910470801695134869699243995586540854165851716226050064610255238509485026538334192689949523757901269905486316718286496400216518265704210584816331343426068536935334003082999413787448560852836623946469292825760708464044418085773699243327811883769682860768162535433318871118062916225967694591916286840502504402940415349709502699434109478120960983142373567377476950929966778768636266183583198699302927464367124337363155291489862654175592642203734655758503740151023213716678096651847325987585389932905982809430887271293672334491224057031536405817271090876730092561311878435577871056057574155523914206172155360163695702822221076706309438324182468253549844039016309947260587753224962349317393664103788653530144680698932973205742852779920729121617063608516443330218105629679137838760469522494599664882677213450026284174228588748438064897324395277654816275676349506768176396086862236296855476957089338002776320612706393450083642047741945946269729991015823440949821443167113713120267187467205823392131544763651690925463927734466049929730017623048373790956654901793349436234836780051180092158641597614737935792532355773685569947972628627092470805057183158501300481449386466007361238862869640704039277707559414039923114153903676331218371455133496727624739280549608769424956324744717159302616407290495059125578573485019514157096865009590584071260975208469793220618788010089937961102398914249097826399711283877320215091555581932973135945306986908481948236146458477256109153948063426111030812373229081610789191355192273923092011064315349355944486854209126872675946519944977636309974804889464703630217709624953193709748602184133064674094013907855861624061878233403469484915096156786733882639495436916895738254314274593971418875920486668967828202826340233539306106432331453626767175852246147790499795147972444572339903192926356839924570815058413156478129133581744923582798584888955325287676263390900430521262200956283449214177649498824898887834590438028016720966179272699077136055877679913811256767511546955035552412013593642788540310787969966611023678666108020498798052009955272624445329390145344635650467467954306950427081261702665194583366506624338300984025502985547249986415032052509975556104654404679071996941932920709912403560626604247407184517862609644778535674889584630974797228346074295475031784745035289825691396131693185169686688221617387022846461293361479672569196972629100857239819663255094210178691958568019867076328127869255291270158287901897811099556587527369053677413573225684876897380468900412399147998694559554838477703701129565179343508161372011628362607809693027021680180963820795824234157339706744901871629780578604321323419092833892286527287263276029447535306406399520327341272626153537438585133563632200728212521149308904845628655809944234141173958501966301892358453708810375559436272073629466612274809027029342434810563448122885623691422557748918534854606753431176278443185031059390034293839763045593017205419113166764285767851938393374242042423550142761795232333607521612668871959226366683309031306765711503565429390043548777252234383504562860141635313015305506312352260660108406654877649168578515083285378223799470692230476864202644680579970692026060753962157541190350259779600489392961380545178708630263467241916794166604748269263492932669747611211480220693307697397666929932741859510368686190756987326296301395598808350831448542527065970172464053491860389102572296314383214978703389996846855942215406313717255362410115002523202427291137865533711787951523630151455279568134194862805835184252528443280359686776906405363465035570794970872741567867776576292776296535047520271528031524972434422502185801121855296713514753645156227409053601253324413689502823727108015145151460613589834581264150977368287377903026048609036928997907169589832435588502989870715745732332231830302785875703772535727465755024812179416523179098741882328106564264142564983955066370959866197677082217488856794843731601509967633808399486489415847697431101778301542941550065621085047409689057580670291102995221578924461407199217800284530887294795911769183982964854397660207001304629173707651308334207495703134201519834821589296391215177646820057691278888861357695452612891783174816008107831642225899272323993023150808710663240860270620713091751829790359327369228827413974008068815257810164323350456461671697499556901115262382075054322595857917304459925792784979365746809430326760061128805098357380361244946228994482732336156145634816905476179994459500860622927541662145778399344569673685928371530922980936971517861837027151612041837021304330883360999565153304652470205074210972264616026172080754703084599271488399342577487573708368843656244614691122226227516806748790251511765450889124738783848816334318746445458704494770326471742062412392248671537125523014704521866834443366945992108315022199403211478418288893547930304611655212811144469626127088097630402483771235485758565197107737910045636973321664390118633126159760131833178427610192729149477869843103980602941281588645164580731676793588520510166791438795061200201020323066879724240144351782241923578486863966941095821792639370634533152483791992258487994262037542475511628177324935805816837432491130413632864465530847951153061787237922239833757216780064438264753724034149655113945608880649947012867633631000134380596334308543022191117497190458775817476451932936068295345655821761774116951103531883065724963478426535676438104086001966463029779935593216428203815040420846895282826770552901456258452789946572040957302092869158424565420535189229660003578483884226993209206174446371259255458098003464726219971605067746031921854989820457836722712359600894898369475533834723453684697308674959509108973835927258389564771094042063257710685819520678972450059865879111766857498320940776773291994774328830901237232284346410081931424946943878735200299512653775467544314652558666800462085213483670823976507282977131324753379866804205958910182241982704942087196606271328590560133380206631638825521215406635511232919931533374417318491942017054759053388441537986387885650521188107497825980904866363798844623573476486810433416135193141677080084535867597070457904808489993951680639680803932078067372700386922140884363066686812295976314477440648688387679550454282073072317046003776420306430131366791121551427695160987692067749979658635751038284542463528571260289666644575316189693186608493117010005397777159666037175359183618020012660792623198231749670531184184870641331226357001996496195485625960084588083323391827020037192024632890800529876348923095805553169557204600987203262140777804382609415698996946910058039816414938645574490076831257064633511073757249129398876735625873435651421864472835610307880097587081800571051482827296821025720263887628410973040242823096180056265156174190417274004289738620767244338071792046910003277267130271738946222175469642087971806976570167581270145951473448153854116594038356592532121556299461718934919275985355619098615579205898854532955061207149482380271851625931374446877521457136383896380317841907220926428084910644983606498166601191572338732239586769990870499281688710863638806144459782644244459043195282105593082946311565716505273900829345921811388660326962354204019396591364194705956097785567694132306431416503339276121410568508837651396057519287130071235717933682072459410180669016613472836723953916013999513515448733800405226237826082094198438809355303916449403079571835150581908074803531738594133725619362289845997690706064330376947372320813669384747721164549435610589286192293474196195959814194522854448764066137267283972581139388299984163465335773646193655574060148104015521861968143976011901149277927919907738858083222651727596217789556733343014936216541569280731163532111099050745696639428703696283452826895495510014067197624442065794432051237202423883956110917758066543135528349907892379787215048424094070156290396347413972130950181143317699451364393470100290959103957459431379726533842611658842694674106919545010689482911156163368255686090188885490052756994211132070447163653846449108237895329328304263351734627936586056563269304824333811741040884059735783938321404439738364596880186616648023942250404558479356052101513262520908292504718362373789079430738938213623919145695444205171871644294974454693004668845091119103292968723286821857151284297209445551260823533774078614117259029893501290847073487479063795190738703121854781538903806670735206988207635415250310819943743827060233126320762109978243723624508717372526564353293605386532245527665221656918905655231506698942475482796351495618749672005510661485332896997217881470829971277893408562704695514109059467305828772628059677930471677432683363865218170653342137315324112226532364646368085928410314412324997651622523232166695576797017312395663063837664386785501017205908041036590940095668904533319142575020527079067131730543675826592601791317749600320159946061393365761044683368322707640516592716804818946334691212171942896329222397465350008327804137587620595398928012715709607548001213656321915150638893859416886015868842922610590928950825798104289286477292606002042750225832111919085493896567104953824240264174918867649311836807525403255224636571492600633739285821636197906873328695704838002670832590863367349480520509545361446347186329026090509907555236577549806833557562229081798511222880921411715363172802595960392907332198901364011954394164064107803704558401694541416311305176605810396471125370828419253181513711197047373211986574613342155323572721407184483026628817591238274305967750130214083827032590597890541143755642582417774081938531119194530324325652895111777907573853746877155058416236703493666308788890589669239427578388661682346208867379027631888331956149970980375307083260236545032955077912932633602766783673439079693786359033401897408273574326523564236991859243025121449985092673905277617958479739977748667306121092417805543935903446835962644446017453187475643677002587002532450089290759276259861343360668663308374855730570410292264439209731130352032768538744731119871273283816673042642372659257217935726695709207219646349103920387147672394409216904545810761156368989222771696979171456454695237257312749405742728015495187744133287467777970386616360198043663507297894091172156022933600886168608356642817875499543050689225557828531049814252396624780695975884018636501566660984378406424228385328177379706205541916128137239590717402192036809652464482001493238046732857591718122478565070776941656067397487464048594436394900502783142615719766467375041443596264949315547691926837681279387158175772149224513546418571601865418040014885357778222356075026028891638098597044593243147278598211786421709364548821453554796086890776679897501695486235844786841815586371233981320407606925359788767414121379284090495774674503898933902225436843712719288161981267929106017969515052860035086010252218574545157875394534387447066226907776442759107431591437795504363211402455834908198412603310095666736692202644765887448694176842527367255406344217864403917101836243878615991134932377476133994541964352924118581638379681676393421522361203082948914475620585271865481996577970728601802392069682898159750812574686935496402684024977382021847533216937378768085119840822549352475496248199635652792282153105169206802440200308639636380847074747383163335178320201975154837998197613820510318509563412367515686285770397260258280406694396695783267084476847841503040083013676872779478434185959277216514972854633230243095070574671168329850664777727361053668558927459040203064339137552641339356277327166974682340272834045198794538896217199851608939516771215809730866049937430658745343466726602917561382185674036440178671193984574686400559991383123498618981600609270440948848876626751495413203504275678385306219185007281160052150626078002571906303758848115798942609162661485793820584226818381365679941991974950060313871249282927578368305843184564400851239372291317949401423843285353452180478821332375833027307520140642114894531427723266576416307088266014345541346405274735975130044538140051046084189603467003978958415562957314554259770936857749366444872498508792682465232876687667571352049578475807385405487628195099127473561311075757855119535500030092150808526165800387456811803464726240388944638072300671277952278762336562371319316990263670252749566040089724972424479076408733606383180790122051235828402056824870683261647461786887627290031778300881903833665156459574712281791499130847471835501230447473800790442950694593849317085211809363190033183993027774390804965000291845820950820679788089826316996767961811261417318371200079054431365233281517799142393797963802375378553445064963762852191410948114842636338175825465437691844600391784687791598208014277124551511911044387452197960063989160977379661994678459300282984506468572499170546332874164835287678878797925480960491910854866936187273696897795533834211587862543849444477213216005109158387892406562183466101324804634588782024571124573455959175116833086586973942153103756188203089052989057194077961985036791979077856187997604291128685350268132946864063605864106803673572577208825071984632059646574578269056676175181676756149532882430826865856257743888624678646800208475736546549807319627805752224607859302157188206085798101925504143322487357803709280563825038792563823382596158388185569487948564397501708338907987306680133324586920663786589191548542056145695144947971864727303008068872136798214419077720912771501260310587895821349275688567653834916884766279388640193444997803565635485798302148023796476563307375351022431673469067471201770262166974381734922319279385813429342320843619727830554478860783719008706719951849755245238649975551331322772536552063977268654056728693859712301525105366754652055528538350713704715206559280308455293416686620577648751577077019276347162421915874834837649713431694719627976891734156962014488426678021243579288047038443749810104702210446079897767483205324691876817284282838337310621940457547035475275738159440081870381712441007896595505417759819925459174705758897646472749921857471642668171952166929786795738726811558606908668136950476929869010494296790190352341310905826067211042941794311979626504706192975704878506415631937868685813804896355332704275704119540931815013625897662309654773247178811238996301600070759017482145629302820143546154570988461425689248046852414343177265947968770295534139185004016745728152031607229744527493532714787479811378426070382171622636499406341177837119321088393697210534834707651562708699824060254765470591193054358125684387008165003286103868354619487044095220124795694260239401110883007578677500803495738860569692407670433483836544646953621002656369130119334582364760290739599558157822093975374325394471773128702044444082987062860918891481666477150296796398304607886330465323231037910188558567332385925079954846942477513916192121226896166749941718038759853032976087931832240837748320438763659789974864053527569163002210757427968729229642901571339908022301584474004051767586059644526565962474058029709088845514721242062412871332541022593810319188927715613003163653242935008082644243579591438662562315729675540157558925266905159678727693726046090361675968549407840401377243178565531501684598686136679853667389722882910558447395854187584384290324301941568615714449452520648973031987453099840616656589488332249076367859860650666064369746956896443963790832450333601444124839360521837056195602046028594260311024026119192655931254727004491560529585879580899544219959469872353963130571198900339489018454511255592680807588601695516034998621133223034101571727709075075321L
print primefac.isprime(prime)
import primefac


# Change the return value to the new function
def base(x):
    return x - 1


def mapping(x, y):
    return int(((x+y)*(x+y+1)/2) + x)


def integrated(x, shift):
    return int(mapping(x + shift, base(x + shift)))


primeRateFactor = 10 ** 6
shift = 1
numPrimes = 0

for x in range(0, primeRateFactor):
    print x
    if primefac.isprime(integrated(x, shift)):
        numPrimes += 1

print "Prime rate out of " + str(primeRateFactor)
print str(float(numPrimes*100)/primeRateFactor) + "%"
print numPrimes
Пример #15
0
def main():
    print 'starting {}'.format(__file__.split('/')[-1])
    startTime = time.time()


    # CIELING = 100
    # CIELING = 1000
    CIELING = 1e6
    # CIELING = 1e12

    # primes_list = primefac.primes(CIELING)
    primes_list = list(itertools.islice(primefac.primegen(), int(math.sqrt(CIELING))))

    print 'numPrimes: {}'.format(len(primes_list))
    print primes_list[:20]
    print primes_list[-10:]
    print

    # get numTerms of 0 to N, sum less than CIELING
    numTermsGuess = 0
    runningSum = 0
    for prime in primes_list:
        numTermsGuess += 1
        runningSum += prime
        if runningSum > CIELING:
            runningSum -= prime
            break
    print 'numTermsGuess = {}, runningSum = {}'.format(numTermsGuess, runningSum)



    done = False
    result = {}

    # maxNumTerms = len(primes_list)  # improve?
    numTerms = numTermsGuess
    while not done:
        numTerms -= 1

        maxStartIndex = len(primes_list) - numTerms
        for startIndex in xrange(0, maxStartIndex):
            endIndex = startIndex + numTerms  # actually one past end

            sumOfTerms = sum(primes_list[startIndex:endIndex])

            if sumOfTerms > CIELING:
                if numTerms % 25 == 0:
                    print 'hit CIELING: numTerms = {}, startIndex = {}'.format(numTerms, startIndex)
                if startIndex == 0:
                    done = True
                break  # go to next numTerms

            if primefac.isprime(sumOfTerms):
                # print 'RESULT: numTerms = {}, sumOfTerms = {}'.format(numTerms, sumOfTerms)
                result = {
                    'numTerms': numTerms,
                    'sumOfTerms': sumOfTerms,
                    'startIndex': startIndex,
                }
                done = True

    # end outer loop

    print
    print result
    if result['numTerms'] < 1e3:
        for i in range(result['startIndex'], result['startIndex'] + result['numTerms']):
            print primes_list[i],
            if i + 1 < result['startIndex'] + result['numTerms']:
                print '+',
            else:
                print '=',
        print result['sumOfTerms']
    print








    elapsedTime = time.time() - startTime
    print 'elapsedTime: {:.2f} s'.format(elapsedTime)
Пример #16
0
def IsPrime(n):
    return primefac.isprime(n)
Пример #17
0
import primefac

is_prime = []

for i in xrange(1, 1000001):
    last_digit = i % 10

    if last_digit in [4, 6, 8]:
        is_prime.append(
            (i, 3 * last_digit - 1, primefac.isprime(3 * last_digit - 1)))

    elif last_digit in [1, 3, 7, 9]:
        is_prime.append(
            (i, 3 * last_digit - 10, primefac.isprime(3 * last_digit - 10)))

    elif last_digit == 2:
        is_prime.append(
            (i, 3 * last_digit + 1, primefac.isprime(3 * last_digit + 1)))

    elif last_digit == 5:
        is_prime.append(
            (i, 3 * last_digit + 2, primefac.isprime(3 * last_digit + 2)))
        is_prime.append(
            (i, 3 * last_digit - 2, primefac.isprime(3 * last_digit - 2)))

    elif last_digit == 0:
        is_prime.append(
            (i, 3 * last_digit + 1, primefac.isprime(3 * last_digit + 1)))
        is_prime.append(
            (i, 3 * last_digit - 1, primefac.isprime(3 * last_digit - 1)))
Пример #18
0
import primefac

num_odd = 10000000
num_primes = 0

for i in xrange(1, num_odd, 2):
    if primefac.isprime(i):
        num_primes += 1

print(2.0 * num_primes) / num_odd
Пример #19
0
    C = 1
    for i in d_b:
        C = C**2 % n
        if (i == 1):
            C = (C * M) % n
    return C


if __name__ == "__main__":
    i = 10**7
    while i < 10**9:
        # Calculate time required to compute the numbers for enciphering
        nums_start_time = time.time()
        # Calculate p
        p = random.randint(i - 1000, i)
        while not (primefac.isprime(p)):
            p = random.randint(i - 1000, i)

        # Calculate q
        q = random.randint(i - 1000, i)
        while not (primefac.isprime(q)):
            q = random.randint(i - 1000, i)

        # Calculate n and phi(n)
        n = p * q
        phi = (p - 1) * (q - 1)

        # Calculate d
        d = max(p, q)
        while not primefac.isprime(d):
            d += 1
Пример #20
0
import primefac
from fibonacci import fibonacci
import math

num_primes = 0

for i in range(1, 10000):
    if primefac.isprime(int(round(math.log(fibonacci(i))))):
        num_primes += 1

print num_primes
# sortedData = sorted(data, key=lambda s: s[1], reverse=True)
# print slope
# print data[:20]
# print sortedData[:20]

consecutivePrimes = []
consecutiveNonPri = []
slope = 1
intercept = -254
numToCheck = 100000
xStart = 254
count = 0

while xStart <= numToCheck:
    print xStart
    if primefac.isprime(F(xStart, slope, intercept)):
        xOrig = xStart
        numOrig = F(xOrig, slope, intercept)
        count = 1
        while primefac.isprime(F(xStart + 1, slope, intercept)):
            count += 1
            xStart += 1
        consecutivePrimes += [(xOrig, numOrig, F(xStart, slope,
                                                 intercept), count)]
    else:
        xOrig = xStart
        numOrig = F(xOrig, slope, intercept)
        count = 1
        while not primefac.isprime(F(xStart + 1, slope, intercept)):
            count += 1
            xStart += 1
Пример #22
0
context.log_level = "critical"

host, port ="2018shell.picoctf.com", 18148

con = remote(host, port)




if context.log_level != 50: # "critical"
	print("#### Q1")
output = con.recvuntil("IS THIS POSSIBLE and FEASIBLE? (Y/N):")
p = int(re.findall(r"p : (.*)", output)[0])
q = int(re.findall(r"q : (.*)", output)[0])
is_feasible = "Y" if primefac.isprime(p) and primefac.isprime(q) else "N"
con.sendline(is_feasible)
if is_feasible == "Y":
	con.recv()
	n = int(p)*int(q)
	con.sendline(str(n))


# t.sleep(0.5)
if context.log_level != 50:
	print("#### Q2")
output = con.recvuntil("IS THIS POSSIBLE and FEASIBLE? (Y/N):")
p = int(re.findall(r"p : (.*)", output)[0])
n = int(re.findall(r"n : (.*)", output)[0])

# we already have p