Finding prime numbers [duplicate] - primes

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Which is the fastest algorithm to find prime numbers?
What is the fastest way to check if a number is prime( large numbers). I have tried the standard method i.e running a loop till root(n) or (n/2) and checking if anything divides it. Also i have tried the sieve method. Is there anything better to implement in c++?

http://en.wikipedia.org/wiki/Primality_test has all you need.

One tip is that you can ignore any even numbers (so add 2 at a time when either looking for factors or checking values).

Related

How to increase the size of the int [duplicate]

This question already has answers here:
BigInteger in C?
(4 answers)
Closed 8 years ago.
I'm looking for a way so that I can get big numbers.
I want to calculate 38^n for n>4000
So is there any way to do this?
Kindly help me.
Look for a BigInt library like this one if the unsigned long long (64-bit integer) does not fit your needs.
Since 38^4000 =
13626567354997617056329313518572669871071170880583693359544272980041303981882434734538430960058767177295025923983566648755876881186325460368486197224167007060233768247052629486933889789012295518920202064862370213656621579461608833913900821509100620666110600996588831934295625624174881269739475099253543291971949351958375909705904924125614847402331728307491174234130043557765333856587852136763450074228033057943275251763474040244718003871446278851718437538753553972738188449864081284199350852720967506441771879828977604092893701331872025324453791540567000401652841385183230548306942426747811381344952202704995790135045091236882434632638273097622784399474964071260288840973524580293683712707126132726032058096115050808212994474874887195248086357178602097170972380906461711265125678088349923203375968414475787300592583696065816150079128755226670110739203817178406747936072376786260989121289233458431976862204647875012437031767751018704749869460607366074818959142212892198396374773980500350327658780222474479129859007194457457829138631699788091425513210781560184664312254850195910716334354324281366251167877851974205334475355985568256387781089509361002196647952275789359155111023451749451415734405096655278295174255960345304122475081165484445003142760913586534250626764015379905517010658444442626228678332925600616931303695695506161883152099899363859336729182377570281814058896416875807664402882868124355445335565905063833617714010822965856571393423962785717867701811630985000479945840574458836679738068978432208205049488474929993540051593774645379459275410621407737459680323962561140718740797900009352381301734433124145413190718954715078693891773766465543820020118333909425037946747920952112324716580163881088322718499979305631739834146130913408498900815005646996682256912339033205133816486824614258926495495812784036447453990264570960357479488597566262100857318557132063094971671138328081636423205300001440991841875093227157189180046979201235997836871115178473793331951908545162574227413951298558070125899512700390047047940487152449438682512124512374465180907633872133729735427824770312681013603101057078687642650433926086042263469811785972516780935001257979167301396531650578909144617556401661596286655000890845598227443074432224649650421444669509917690850588828467680365214374983571440413011029311538006314308009334022145676697106884765702897820392205002104976510511690662873648924905859206045090301538364495227369714240000327397892685270724477432224051710235161915341380166322505403047479921207485985826617144630062427543646106264207676267960138097995365712065699367252327692166917027162817512355112577817759333185119496357726652263574955692412766427791636621561003248143162088338810765811925034175243098253477183777981530119336600325069531470597361199726340530012493284901312472359938906404036536403970381273042347532022592036037374925652036435508211829648726060002645001171965651585268749073280713101532058063840348319740158848595441587678957522413199351039043113142029907129705744137232342011968704198807114940460676009450036521225461344010852618106239118154966331660057415602614793721826584529397251289749725656538096388399695143315928518563068548192735206544682378537520506418007926073285706849973274222076317373033059304200101911951067149937028894562722109537797411167898225856420348308410350603384055632474339567057703010340688068370821291318903177717596801407315734010878564418695310254546381520276678538725157608043309023441709322160712160377986536483006949768428055070012131321307517369398293559849470345086292046748890778373027131007888477361435703338650286281943866790521895536089587012314596482938225348353910513764482608946308253045592163757729525262326745493201953185303485264102739959029696399931572287438132772357749175882967079565029225741296582036140259021566923027054743911617278986639492312193679025969190891540930024189366778105054430929215092236023487686440492543667079519662985565417362492072592025055258819162704173072662013949276074302392094104918002731442385963738154377544394436144830710895941012427657480393526992952298836599116927265835864794883149357772532128926114623377650330127681469398680908197842808781573649462943403899309504315010053951097502630531047921616001572581816853481632903644705002543359747980637999901147726746920478281033260422246532219801930828506609799922586505464736354853965408705166237575268884142550705915838361403803760633861291439873689266312094985645627825768264532068068383940009347237832839084518620736293338032805140684318825609037052029320710069076942327055511619384729507413979366221173731875081583527606225142331708054058056316566268463694297799208229967327456031980737573803309723678915050699869715272912792497268191176053411510542893000088350086703903267923064151320399321810627158761446660416500169785103732189707448149719408203646028631800570100383545558186048239744484408082627734589940230907845416949939893008652775458505691974437891034384581573026445318317396853368414228897325531497788743229729964241347151442038312944349207021462075732646443668171786595400861104506016915734670330338318506940647492024589840270503991235013262662584958056139456884168786647861529375426433055592755996647363095606998572938173742273727647855395827384610580791486231441743966364638926656177574517783395990477645234920662029069130520169839954328852889679297698894440289360920229577366598674693945253544348773443008431294122902200275761539883366488016392721682618754525310713687607682663191813572550150753228158785842023491267134367643177590897108661282968536031908401081453990449167116430269199694437799218382709284611544863462398344387527482864615346690281888178491568067973361859477877403761036289855293359103434207477896715048195648273965669239199756276403324120343903889368419203819744433704771664793914154135622085625251617910720554457686952006577578494073537754699513783351789231152535131897837873522492614650699226752457363727673079692328753548477123197857803197559269831202493927130425819248457221653733937060671839872590344351995222910813966529070591698808960588220021008665427193485211880949161819748058663187983592657186472030052147851033490886803897240924656263389646409848702846770248392608384734172415681411506756891468425081693839740848739676180437868697906407065835441491240347225077345140389966302714596712069918261923483301431653997283019346596069376
You definitely need some kind of big int library. You can find one quite easily on Google or even (if you plan only specific operations), write one on your own. For instance, the above result comes from my own calculator, which supports big integer evaluations.

Optimizing pow algorithm [duplicate]

This question already has answers here:
Optimizations for pow() with const non-integer exponent?
(10 answers)
Closed 9 years ago.
I have to raise a number to the power of 1/2.2 which is 0.45454545... many times. Actually I have to do this in a loop. Simple pow/powf is very slow (when I comment out this pow from my loop code it's a loot faster). Is there any way to optimize such operation?
You might give a look at: Optimized Approximative pow() in C / C++
It also includes a benchmark.

In c++ , how would i generate a random number between 2 and 14? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random integer from a range
I am just starting to learn c++, and for a class assignment i'm supposed to use rand() and get a random number between 2 and 14. I know to get like between 1 and 10 i would do rand()%10+1
In my case would I need to do rand()%14+2?
I understand that others have posted similar questions, but they appear to all do between even multiples, such as 1 and 10, or 10 and 20 etc.
rand()%12+2
notice there is a gap of 12 number in between and your lower bound is 2.

C++ Suggesting wrong spelled words [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What algorithm gives suggestions in a spell checker?
What algorithm should be used in a C++ program whose aim is to read from a text file and give suggestions for the wrong spelled words ?
Use the Levenshtein distance:
http://thetechnofreak.com/technofreak/levenshtein-distance-implementation-c/
http://murilo.wordpress.com/2011/02/01/fast-and-easy-levenshtein-distance-using-a-trie-in-c/

How does the regex expression that checks for prime numbers work? [duplicate]

This question already has answers here:
How does this regular expression work?
(2 answers)
Closed 7 years ago.
I found a nice piece of regex code that checks for a prime number. I think I understand it but i'm still a little confused. Here's the code: /^1?$|^(11+?)\1+$/
Can someone explain (step by step) exactly what is happening both with the regex code and how it actually relates to knowing if a number is prime or not?
The basic premise is that this regular expression examines a ones representation of the number (e.g. 5 = 11111). By checking for the presence of ones (1) in certain positions or groupings it can identify the number as prime.
Additional References:
Credit where credit is due -
http://montreal.pm.org/tech/neil_kandalgaonkar.shtml
Great explanation - http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/