User Tools

Site Tools


permutations

Back to: Arbitrary-Precision Math


Permutations: P(N, R)

The function defined here computes the total possible number of permutations there are within a population of N unique items selected in groups of R at a time. We often see this operation written something like:

When it comes to the number of permutations (P), the order or sequence of the R items, as extracted from a total population of N, is fundamentally important as the permutations equate to a total count of all the unique sequences that can be formed from the given population, if we only select lineups of R of them at a time.

Since these computations are done in arbitrary-precision, they could result in very large values extending out to hundreds or thousands of digits.

Permutations Calculator based on the function defined below.


The function used to perform the P(N,R) computation follows below.

/*
  ========================================================================
  This function returns the permutations of (N) items taken (R) at a time.
  Like the digits of a number or the letters in a word, the sequential order
  of the selected items is important.

  Input arguments must be positive integers.  Any decimal argument will be
  truncated to an integer, without rounding, rather than returning an error.

   ERRORS:
   FALSE is returned on error, if either argument is non-numeric or out
   of valid range.
  ========================================================================
*/

  function bcPerm_N_R ($IntN, $IntR)
{
// Read input arguments.
   $N = trim($IntN);
   $R = trim($IntR);

// Error if either argument is non-numeric.
   if (!is_numeric($N) or !is_numeric($R)) {return FALSE;}

// Truncate argument(s) to integer value(s) without rounding.
   $N = bcadd($N, 0);  $R = bcadd($R, 0);

// Error if either argument is out of valid range.
   if ($N < 0 or $R < 0 or $R > $N) {return FALSE;}

// Execute loop to compute P(N, R).
   $P=1;  for ($i=0;  $i < $R;  $i++) {$P = bcmul($P, $N-$i);}

// Done.
  return $P;
}

This is a test program to demonstrate the above function followed by the full source code.

Permutations of N=7972 distinct items if taken in groups of R=4965 at a time:

The exact value of P (N,R) = 
306778269987635709882068726341245868888254072790494114211889759013525762149074538707623057005954998635860015575768349181017856763465028519643629244701813216728257329272639489977402789501380331819236823950878245408424680928627810396186824191675829511128821702386106318777399320513102798535807876236028065429013995711078031250227228806703561071763549178212526513277339503397243891710534759692714700737983887112401469554685600051758132838743439214307877202973363449016595397641712393648946897831365760097367964824591357045979081366246607206482190491120064112204110178279257816357065988259223605423399913222481198835526480705329525162611361483835766177915844338888184548807419287631236404271790922227696246284203788451934085124820938231619304309070374528757496032534957527114528273644317992868113593205896479857396784410118639005240496026306028845868172033960629193344638193334165861511868176546426800813490048768761852373022334225077740836885606909381110986787055737215128151367431989965491757068201547950967199024612981824527494985879046717952993446660606327980416035854302037522608742049540475113006162763222367361101479330944092702412024352235662202097109080945245058032110869419853028119839818782283182501419615037926912824333961890554840871888023464148113607950477667116238867126605080084780736535108750620766080214342985580217441584605581686693175439802981804276805198457242526773872963787027878197778747347604774445912049732858062535837792500414663821222524784324457352737942645238507952469006831163723040616914340366342055633944570064673222257875486009939540132873396755173752758458055120518826910944945877304522558344478507782580398485658109246946239601782181861695526263765065026364956478974011056771274374415118486480620769618915125586048545004534734852876877610208187088774187528190316670670157509533248045629209999284903683123234395995636410504088271413925249193495484248812428381629320785502452310650514697355916417809523744675090956101573296700150238772765182145738077570243358687258713739778974532279131587317482683982612069885441750873225891796259630443256181843043504934224546959705614593817857449357760242469839556380705187150561594546365857990122186356284128827943724208695750015882476156634926512434565362842858318147629103215102681549876010208424441269246526595202483926770365813444118879309950257573107199695532319224700271745038198330376945108548493563280811631839576097850822512900028816555078434369263822279907082382011680599394244085207948553226515879003234648456448157870279661114601881810393583185373449735178452436813414282267714999557646860744652708519940500065889185598743802884399671865725017881490150575723748384746989000401829664086997377598940494556195870321006881848422184080222736832745412874761993954456489443934734432873524645195781738707960765828005904869284539938536598783883589049431641967675381450612347873890715554340762893498207635488363190863854535338894147522801569977072813268502017753136421691141505594332548397548758190848521101522177124883497915045657457824995335764764777659966903092110428657861199368208374654621896872867703003325398179222467732091756470048366684547802360415408736957190629571089751432928877745886013121081767911750390197149881462620264580045018741803441272006305173799396817948413726050175193160183549264614300980298885808144103589337573180668999499456071608953235692248557312590987146817452459410618906157906511267416868222619366874706796614916326881602918800178638269433747157774444758952264934886039646048540481650368667793123868538235064349705208837611455014348503888470021060649709068513547099866616314242945280462401627044039904508116952367287471568852124976014420908326738589437131727043352720060414767533853468460021443268615890880033194697593651892679371223278953668731933812207762013301666260730009146872942760106401811770735009184305837760792095832282768881219785855849145561169149100358535811159833572191847343746572522515416537487130715087965722527442515133806687660015033373985494937863750348431902730069059304271588842778298688323251569081624120977872980668918475740305935224628546756687135088134212576076617467378587808363493924490789967308252698026303266487503105040591379467762043643318801576383202299153930794621685968299802516361951871496090502798857337909481792466629720281766301224605987721132311050248498385493280202480041530615603723458880891873157334158548595200974106987960312497259582601976738300402779121234705903619697526606417900973848055346027866115093635199629331799039037527078914725182922892601189233719292036098257936658869778925744892277972273074285263013202302595205079193973597763749477918247831789925560744495132335257502286098998216381059706615566692629728625104732131906035137752059757062659432697592430476631067150117736411496119583661166118636594730795554824094326838227979067091139121855226507714567511283170032215035054436646723918129163221847922316723625404797062720633158982080391710414709394311900840014265260660675269518710261382391462818354075193163564827420196251747022200149203076044765850734138389101505014140154253473228219358211923106158196838655530784260625695397158459575485278863077599612018884418583114028120643083226679256231287568530044495849310477373636396251197927761711736118905011245013751519896652562526130258398567362991276311815873422599499032282415731772136088153162815207315855413488289380103743805062163547342774271611087482747590563511715575184296717115802736544634213860486338009722435608492965748370202601789233309022409762297690981533230743309754134320348327139505245877984297457768613605760866241175351872579648153505815886801194129745622410853489219597578993195523080370881985300534215213295738063796832683222313955009384550302970752788377810247004623566732011673674515791976311758820830351081985022502978437675525665090081555462804607191510186189954807322473783301318762402633491333823995862093153753511617933379733489270772245535130415048107587211409722974081365433998324537832757409470255921022558809362925172345975924412016479736804046639462284843397415079316678657741835095348970606851078487503500165149380380880856522370435883333812643192286126442118000141099616899753247848103801480083583834124168096364331851412093147317758230412878107779310249735002936232309657543846636196371239487436438989697636816306212380697244566566650951571002463711577522393540822071330413036430665630405618618853798195764583496903944772010908677730036772701345130929193804242939274425286410057892803211180406055241451825876111136944074658828104432545292552636576998436096035210072561887088301199143317762782893597483585872736329686515917142097172224910406761380006045917281987488126835221419604936512127955640741742930800463491001398506059987801288492284420422956407118021361191098799196452125838729506060935247167151815160980169449547710932044577104228480466703320973138263477724190248177690076512897438189743442148532413375607318742885680342808436783718406937120618688178327859093855749330363104057643749557340218820662744679547933680542085981306276387355089797660890997951375803907056384963276527737831956234910197126835979881940167538618431997653224882311787964287729002660763041448542270006040871030148919931487500016473736993548734241406965555081755103992317182782955374125802436740339259373214766369565989813593781425992341533495771125466027874823785435359214313823810993416601472884389613663636977087661456917312029529647287088406966232140650507969280664435202418474337123746815645853321943783130813302027489362576519470896865021280539953342978881783829211761713356766841896831096442975386028476769204046988045277284786836771711086692627671323324356908211385051049392859377226189206593394115839345714401482690631041522812597139586403136815743792659907209888642023101150724713220861236150263237697439345118550504226841932607595808084029367397000259433997515194326815688990003508479257326949156283222153302820173427015442848518356479863200060844098549238921636082379375893854697023704426485710992581559268383173107610432504737701898440452561037375824043406936712230702704129954986431377189210901436103437370392430890761253334823480543211323156803570174770689191632065636179138302356221526596127711533202810163086995141179767716299811824841372962333192506757872587565191812787346065157222815190631809705362148689238138750231997993073432477278280400397048257157634981228085860691001322826475726726150137280997104534369606866729582051235234224115911533349856905746316939653398840118436257371400459282056363842121144252365585420118955027596829681336319250719801635328850737510782640957256030968659414611171835083399553471244181866234389683729964186324540760739191664402613822047361056056128559046384339139489675313649331532790868455452776404999789431477147471521896365503095210735363701588817283737168296543018821963440160705438445616722342293226709125231203915279438577469903054330970882673249597804734862235716416470707731258940837349170382586082418201031986248953453578998355622254203147595741990405534831207742994656712449679953322251396515268662449008594908116792947076699146411342688330032079613369785721073425755848233032799914299514297075575886754292190645025333303572489481651829881316331351732257931425666714452188361559954571670420085521074976924825559096423308671202586157327972863696483956059406249737799776715627803243648634355506141919468494504290496402829304042898959294321423456646636752210528149879718461859572171927218271666307207275459406205874835546891677837531713557549216136270913586964207294008811965926207611582093363305227558284048382600252925750730935359076176518977924641740306703648298765033503024425571069962302800048740196806594336407805897677887582742221016927248789633744190079297474731895615757444182082884147844707274691281496220622582860279908158493782424755632859291465373610168411232858790548707321898518649737146479512181838391990038653645929461453619523893052776347599416580148614340711672797731479151461166145184482566783753701669307816527836129148362131155153499356519306367840028952116343038647826430219066498285981874133950657234234479309786112936112886554022883821940812749607867166089879812710171848877782196114923272038757699904432866995856156179095950909019768539110587913432966824447806529586259321988744571010859483470069032600599057036613565519022171335780018186512657183237509265823827613705162658058662442525525985611204157094416346968093706174336428768660638067701719961585020650355311778517500500309154131747604163476544120874317426858683682121561347889609407271299707494517060601558843730324134567658076806278155497858422711323151949841379442756797800420601685792675102582963205973395582235067041625973113475572963799821677043620652842719976832543547682114595163847819516837826469789925646860212990260086592496654705257008479465806647175246104458785728487369628984681280996005661826119735589291909650966825724199526743594306483709025597107501922569391316076672911195017963765662834133675965983891146348513537472939150141475605604498514610964590225197226914004079178586924542896566404183775365978057373453219076984048875142178484930858023788071501986569157246316966802955358365046105267740386805163689126999693819808950564406328091645935734356146625812213707992222587602522035304626454137827400626355733556997680961532787861077816260736221241241311968380740764824444263543375150367123896143432541025075654003003338926033093987223913480395034488656594076218514833237167917936878435461451117518159251169033340011546072916350972193121568753343572335071608653286793445452628954877047094290039497436835148753684231584693762515175331024804733595076119043056538286682217802771676414764018579414972623278252025666951474514703570389806028779085666325190459927194021377916133857308039633441814316407325248593292909591749900701854286932214100384555195624457908878718523218529390106856064277226037977410668173062480078886982738042109934227110893004946405821672825900448954611143727652874975144605464308223132762424501461574960373763483382880725508506640404931965058426305157981323639328294059496570156653738135994475375966946013977135516029186081234250576012772619022305631187818976675860900648181126104095831114184028008849553503867276687849013413943489774446327600228967586024729761772380093617299212894959153045052325587080758676611627761767972022202652933283384022233626148250094859075308677060375752712943933745524776966385402805052608672471927443615469836693733783330623032554941062980152322530625597157404272207918195776122294788201796171881652659287264539571476579425139935254890082015144739936174483154375471231263258171909920288069953291896687505417258691889432371419083249850033865720075965112152410440697393829177681590240987282162096035219765921041752419627112580192127083241325842106506246122684044509643721878625950433087377544874299816185766278225580496378876421766239540851580204535862182617020157635195929549435639122167257861056661631701549460025334267481705744472003128112394319404228390149995710892963953291626272202738277673050103221357811747631044274369299359679533940015300172303704341136493645571374509732286630036275886932519299035482862862771208016324253013406882851563943574253726773116179551805448261265680898331592087479337900583009112805719954780525220737777734940590921399252608001576825176203918526987298807820103382212317151232282109194761611223287758518404310920135953600182598192073603432513504014618004176880514960729803943074082108261542525809487769758981820097026073818823980253644812270389459138145777833886958454060054222704383866152663030961633165090160067262790135944617124635425937892381342642936628036507137953683389133993390698232645398129002497069551477880553055902633784841973319595020148936733531089280483831901464454249016567211055151044167273942610978458072120012780847245430879644219967188766261849694367553949042491315640733570972608560676850234517593465523550821784421478262690531006754777663223349334088970702928957819460454066823901580510612112402705257950778496916876796098246121058182797267369838058297155937958116070814449693517626912644021692254269029088237181921513266568807169230582231269491931808481546239654418304581145427932969793321100556731511689413245661766192866666293703324640319903666194987089645437656752369574680015826166488512081926977802722945674760745729677425240149387271584348195735575673723856323280082503129997394763510666749105476890186106730656361405695710978197500788335384285535376395383006008758731868019237162314278604871929910763688619805107794658511481088580707903423432991412431940271265759913413407856040874085646688426713666801039750393274957653541155097337035976716522071217517118139516902771759435371725059099797360349140035221544331128368474229696684548746420161352331897657590572775465108726419938420556773951194794331869129978656197884254575759560200309431733505715138871006466492083336707181353370507785437025135080925129153995770495144374917018977069448466194244964341605294314545603099670120722797156928326716833752649764734752851358595428382350974680049707476833961464973983097669342803092348408198571043783772081543722058524552724651460660779429357359268123367100815329227522694909972057036952038762738195855934942444002469973295911693993572319269520676035863898051902374772976817973754421250609290387638423383341600541878637928869378926861704203689574242155501749319220270551547139127866497088496171391783882743662569705303829762349097184661268019728505590063506755104597114409559309328059685673285104591818367945527825835192645486146824594822498621727457400962747895472500474157799814743793181260345047735695494677295627663685360379867384095679621152704613413439163734031318628764234119864740909144149550851678958769667050258122867639157792312279413012605748238870227186606784772591044725872902458413536004621120933958178450752802703153504900471055859436447401654270337208720527800926650405608713870384883651526100000752503173310233004407505118285673677579307248826275911804792459448266821621375833951457022013538191385153474203721588872830591386349246278227829379545539915119347734164127080387260641852082907669874072592127748959066983417856269977739549407104168792419519545659617336943282447667276982213526477628969079554925469889499997500634128635402834991163096820293186900755283121965370827435817759689235967471789955053635072368167505532676629464057126951399754805150225503732511910392016203055338514357919593358330111295571560419315382447373629523421353731966473675715495391519748655238570725273852216674504603578621677828284550608300844865002613428896567569615078304170817956158002288836178175752325478741493023036023331058650193686027040493790165644826063957628433515729990709407295552200973357049415344412382550307158766042748969207090765907688751346263848591443532691089562281287505698024277270984293130675624136598637191090900283599417621325137848622789740734745373857425907957213476425868207899024748376857616881942784491907300151102203738978287350728806444426395613699244734471927848878983368223890991866590226259433083443581479143423015018790314100860313779977571862778812787236439922792102151102112206880844170170043014369245528710861197729313266630695537148919916921108436347293137852730359526001519749619513183198151035448827037896633100903633147306514305938567705787763665068417589382762638876705206563662435692078554205299297467144396450495663240642560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Digits Count = 18489 

<?php

// Permutations: P = P(N, R)
// Generate random values for example.

$N = mt_rand(10, 9999); 
$R = mt_rand(1, $N); // Cannot exceed N value above.

$P_NR = bcPerm_N_R($N, $R);
$DigitsCount = strlen($P_NR);

print
"<pre>
Permutations of N=$N distinct items if taken in groups of R=$R at a time:

The exact value of P (N,R) = 
$P_NR
Digits Count = $DigitsCount 
</pre>";

// ==================================
   function bcPerm_N_R ($IntN, $IntR)
{
   $N = trim($IntN);
   $R = trim($IntR);

   if (!is_numeric($N) or !is_numeric($R)) {return FALSE;}

   $N = bcadd($N, 0);  $R = bcadd($R, 0);

   if ($N < 0 or $R < 0 or $R > $N) {return FALSE;}

   $P=1;  for ($i=0;  $i < $R;  $i++) {$P = bcmul($P, $N-$i);}

  return $P;
}

?>
permutations.txt · Last modified: 2014/04/08 01:56 (external edit)