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=2925 distinct items if taken in groups of R=2840 at a time:

The exact value of P (N,R) = 
615423039392498684034834104032968205103234990899308229997599971425473240932725898168959759646035807010484089992920887763342235520936056664929262395357625442913432868033739150508074938068469912225527088798143643340794275105114101853296623076943894462574164848046936534580032120247093977108078090528892813477874835884459336779158000439819010150464644242570148850542939708261109886608014979911802025318320237541772308801663623668309032296253044288554527020619388433479668231032341114055738307721037722124952510949237988708423784703457432834863716306412647865850516076735098106980012940767344296337197522375766285160968034170237100719469217530066351907774294349789344905918168086123305332686303658127227417814410219677592757655790645508442282859273869976977132187340748061459908298144933428527831606159687691128795527009505792342277310764260068677978125074797966890771591353742435574939650345476014141029985321227023482345813510716392982113404970338910751743285180452162068604911767242296442141469978119812044708445058163039337000738629647781599250196536884161251978924478008668420887442540611540681724160372415038314798959023993044861746305728151267322646019809095231899958444293050504828041360572863111038039594345403606606929098180626530904043757209632570767000607539457268569510630432050438237400191286126212818034475499279869367721117520105390057194297112019758628543475996130690187941219163167980647623031793073059756757885595919019142324491457946174454307608713296769200609534119140055068425815843179792486342188133461341300370960164194643323909484394057559807849926337164871909144638912949406776587947187232121766098186704361060129289750631068396743787376056707766872625268136877708256849309955389244928106844503843790571398186390547736013875802487081565667270582778158080917755768841736269321325774315318423340492985687601674838576739305077307374788024185757710379547595932096414965523952459445627272017221039072864314845980161997504527567278083926999270761486162135831575033179116129778800344221570760956623462991871204999273137754193616175028071906569365428193048888999199132773490694299292866819268614858981942759358009470377053849018963082925324314486596457876553790974593263290792526913856436161632188452779602834454783172438053369767801683141772028324818488472532592578798071364972904577634634140340376201103017408247294259335401678618723154806530648668190169540679256885535155567926151598790578854067797502207481445280105881512467500620891457044624477133635011483756765851055473758962220722384088187315244288008246017169651016785540295997132941274478177708940702092704994288057115711456662476449373369199081759503952915758112786005862974896527449346144422596638500031988502230321760057087967531847451936557320493737984825967377024565836773629346865948331555029319011377244434011155053210913796185098130690272623074058357427569491661750720531696140373749894139817634995949237023694991229489284196352984676109638895317007128634279264640649916120599924404397931209065152220537247566884909417043269328363867953713408914210176066926884045707090432977704034607566601329973377992458441185263134390901902809677746411406029699330553976875682648719434499208992775204274415480106955458788833948684380640401636255704443792792845903683230647676614991179413088617059914521448009594685176850897570882863710325950843970209128718595217010673266852283249386774669193457751341085929097950497022229366947286736912466116065839196645715217916746556522366359159307360436522309076257506011541394131781266151231697706155079550061042951544844715659533819677740260464788712931966602402396145704734338383559403332213195892733873122269048172189271709311629001526340301165985440823866756049516219122593450563796907349192641157441810211073733246905209863401475869800582257947771851088879702621555072812633202525405165517994887243840766292481714198252761262629366603209542018812913083567029664127027463751039633543335786513775497691341268751597040545189687674859073599310345556363309655690821583390042479203256133448658346145181920112981332805646617274667680597051290748067964299460484114126762473408573592350179823961865791356110114251534136863519791370514111756645855695577719876912041448629876411446025087684146938978813490009330724794808997726785621664316856669519205909699740558181364805041721012498256603453974996116487395497278787796172289046163247035052687273274826669054302739528714349633876854324296631720261724665763477972098529877146554438314917383256306798488646713231696028961389350812848153093699633618089812369086402181161982912124532473781203811895932821303154362155889414647850508608498174639131506500985247368949380152458951213119518358428927763138623341719062822606619765287134936760513463236328502715819648121313928508220833949958545298008295598022869002304006899545363558043334010609485376750480818483576250479624715362965144352613861373627074770053469715232336874136539280750144759516735813181495020927804092475213416494498450559674314766550939113504405829771905137913698286048466011629319575826262265892986735361765013015044952356080590312449849671350458405482864241668816817902858884540032707514913365560704743731850370513727197101715916522964012430714915873403706582762770207524269960098560616408328241932179736770264595541833716942006245188338650525654455067149752256754958211983012130449680945619162882798853122056946279495894519185250862839638715573146989447136979311472298041502506926311208038446321994742300098061502637302015912818500659223916128265650285426414602128229780898653002844248463221939102249500312752470512579582327329552921373182174413731507504098540526232228343391977462371069113157920637636372011213804786342543376490739119325893309005429321643309631846495226391147239138573783581467793214935565397260978544224927215701917634097482571109038329613289245622887762153490422386756326491109184706136792085143739572443645661664115720395992376737087541057609631253683437611888555211994472866622331814942287466463689020440470438364599461923324719577729484543396239852386546991589292957415321868252274391083508236261220948911673826693921847715329623009465296585534138945753778328084414737513994698032980477496600466661364257128113074937523142955642222463097909260871530301473863486463583379231535870621382310570768327895715059501012690407564259358591645211831072394393817257418346023551018489220349130123996425604848920531576383589008534629763444770537886413694470496291323632069221040456547414860838840725076146921570200287426539642404228191796022060870816061329010610997546612460229658639781205377956947793266671103874070275230723966451311035021365678797704160755937362440859498940580725494871595021597788573501322739674088252831386409976594441084822510558996369067099302658657072611150632562910365221435019130899090617839987187667504577875912180574038495588349607863982958185110658710077375420802328396931903697133218743101375080199060380735311190423589522242099502616120889393255537458145902404775689912028702375730153485385059554388057158357937005999104923633615860850183538059222484934401554872329699330387210108187877632979046720227972977437385337922115246712937332043050548294995694321893943304891091356918592844490965991053880511679671596161230741393071370263467691393551312392694532008298876965267137150807765914492957247418102209487780666591034527538072127764202925211469328416153040515098971037173779955375509897252205207358897263095477665193107702265240901087037323959077124429398856673771777959438383465168412385481491716876959597683438436858021197463982433297876472444221628776586927212837510835151913205929789889111007361987058012511715702847751107636128105532122986055308050830608862226945820396725888936262001771888695336914098977676225418443844258694706111255892319989392087102172793109454839545704827600077644595045822712876872616682786666014208331022745683325288337471012089945642818070992830648225823142982338977278284251755437724013254258850900214344161015782273039738879538988546202275256181374872031860489125351970063108639167670908346210561481851909856472856177540692422541531461535745966080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Digits Count = 8742 

<?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)