Factorials, Permutations and Combinations in JavaScript (original) (raw)

Base Conversion, Logs, Powers, Factorials, Permutations and Combinations in JavaScript

Numerical Computations for Cryptography

Computations of combinatoric and statistics functions and inverses which deliver good accuracy over a wide range of values. Accuracy tests allow the functions to be checked in any computing environment.

ACiphers By Ritter Page

Terry Ritter

Last Update: 2000 July 17

Please send comments and suggestions for improvement to: ritter@ciphersbyritter.com. You may wish to help support this work by patronizingRitter's Crypto Bookshop.


Contents


Base Conversion

Numeric values are usually represented as a sequence of digits, each of which implies some amount of a base to a particular power. We thus interpret "256" in "decimal" (base 10 )as being two_hundreds_ (base 10 to the second power) plus five tens(base 10 to the first power) plus six ones. But that value also can be seen as five 49's plus one 7 plus four 1's, or "514" in base 7. This is just a different way to represent the exact same value.

The base conversion routines supplied here support base values from 2 to 64. For bases above 10 it is necessary to use more than our usual 10 numeric digits, and it is convenient to use alphabetic letters. We can adjust thealphabetas desired.


Logs


Powers


Thefactorialof n is the product of allintegers from_n_ down to 2. While any scientific calculator will deliver a result for a small n, I have often needed values for large_n_ beyond any calculator I have seen. And while such values may be too large for convenient representation, their base-2 logarithm tells us the number ofbitsneeded to express the value inbinary.Cipher keyspaceis also expressed in bits.


Permutations

Apermutationis an ordering, a re-arrangement of symbols or objects. Here we calculate the number of arrangements possible given n symbols, with k of these being used in each arrangement. Again we expect to see some large values, and so also report results as base-2 logs orbits.


Combinations

Acombinationis a particular set of symbols, independent of their ordering or arrangment. Here we calculate the number of combinations of n things taken k at a time. Again we also report results as base-2 logs orbits.


Terry Ritter, hiscurrent address, and histop page.