## Number Theory in Javascript

A quick and short primer to Number Theory with Javascript snippets. Shout out to Kurt Braget for being my homie and inspiring me to write this small book.

## Table of Contents

## Even and Odd Numbers

### Even numbers are when division by 2 the remainder is 0

### Odd Numbers are when division by 2 the remainder is 1

Code snippet to identify Even and Odd Numbers

### What does this mean?

Based on the parity of both functions. This means we can separate them into separate sets. Where The Set of Even Numbers has a Parity of 0. Where The Set of Oddd Numbers has a Parity of 1.

## Divisibility Rules

### A number is divisible by 2 if it's last digit is even.

### A number is divisible by 3 if it's a multiple of 3.

### A number is divisible by 4 if it's a multiple of 4.

### A number is divisible by 5 if it's last digit is 0 or 5.

### A number is divisible by 6 if it's divisible by both 2 and 3.

### A number is divisible by 7 if subtracting the last digit twice from the other digits is a multiple of 7.

### A number is divisible by 8 if it's a multiple of 8.

### A number is divisible by 9 if it's a multiple of 9.

### A number is divisible by 10 if it's last digit is 0.

Code snippet to identify divisibility through modulation. Keep in mind the implementation in Javascript does not represent the theoretical postulations presented above. You would need to disect numbers in a more granular approach to achieve such. However identifying divisors through modulation is probably the most practical divisibility rule you could possibly use.

## Remainder and Modulation

You've probably already observed us using modulus to find even and odd numbers and divisibility. The following snippet iterates how to get the remainder from a number and it's divisor.

## Prime and Composite Numbers

Prime numbers is a natural number (not a decimal) that has no divisors other then one and itself. Composite numbers on the otherhand represent numbers that have divisors other then one and itself. The following code snippet shows how to identify a prime or composite number.

## Mersenne Prime Numbers

Mersenne Prime Numbers are when 2^n - 1 is a prime number then n is also a prime number. We can compute whether a number is a Mersenne Prime Number with the following code snippet.

## Prime Factorization

Prime Factors are a fundamental theroem in arithmetic. Essentially any number greater than 1 is either a prime number or can be written as a product of prime numbers. The following snippet of code figures out both the prime factors and the multiples required to make the product value.

## Perfect Squares

Perfect Squares and Cubes are pretty straight forward. They are literally just exponential formulas. 2^2 = 2 * 2 which is a perfect square. 3 ^ 3 = 3 * 3 * 3 which is a perfect cube. This can be notated by any number that can be multipled accordingly. Surprisingly, Perfect Squares and Prime Factors are directly linked in that they can be determined through prime factorization.

## Factors

Factors are similar to Prime Factors, except we find any resultant multiple with composite and prime numbers. For example the factors of 12 are: 1,2,3,4,6,12 since all those numbers can multiply one or the other to create 12.

## Division Algorithm

The division algorithm is an additive or subtractive method (and thus can be productized) methodology to divide numbers. It should give some perspective on how division works. Reviewing the Javascript algorithm is especially fascinating since we use recursion through while loops.

## Euclidean Algorithm

The Euclidean Algorithm is a great way to find the greatest common divisor without doing factorization of each respective number. There are two versions of Euclidean's algorithm. The normal and extended version.

## Logarithmic Bases

Normal natural numbers are in log base 10. Binary which only uses 1 and 0's is log base 2. Hexadecimals are log base 16. The following Javascript changes regular integers to different logarithmic bases.

## Factorials

Factorials are a bit different then factors in that we multiply it by each smaller number then it's respective value. The concept is fairly straight forward though.