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.

A Ciphers By Ritter Page


Terry Ritter

 


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


Contents

Related Pages


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 the alphabet as desired.

Input Value Base

Input Alphabet

Decimal Value

Output Alphabet

Output Value Base


Logs

x

ln(x) log2(x)


Powers

x n

xn ln(xn) log2(xn)


Factorials

The factorial of n is the product of all integers 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 of bits needed to express the value in binary. Cipher keyspace is also expressed in bits.

n

n! ln(n!) log2(n!)


Permutations

A permutation is 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 or bits.

n k

P(n,k) ln P(n,k) log2 P(n,k)


Combinations

A combination is 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 or bits.

n k

C(n,k) ln C(n,k) log2 C(n,k)