Contents

What should you learn in this course

Number System

JavaScript Number Object

Number Algorithms

Random Number Generator

Summary

**목차**

What should you learn in this courseNumber SystemJavaScript Number ObjectInteger RoundingNumber.EPSLIONMaximumsNumber.MAX_SAFE_INTEGER Number.MAX_VALUEMinimumsNumber.MIN_SAFE_INTEGER Number.MIN_VALUEInfinitySize SummaryNumber AlgorithmsPrimality TestTime Complexity: O(n)How to improve the Time Complexity?After Time Complexity: O(sqrt(n))Prime FactorizationTime Complexity: O(sqrt(n))Random Number GeneratorTo get floating points higher than 1To get random integersSummary

What should you learn in this course

This chapter will focus on JavaScript number operations, number representation, Number objects, common number algorithms, and random number generation.

By the end of this chapter, you will understand how to work with numbers in JavaScript as well as how to implement prime factorization, which is fundamental for encryption.

Number operations of a programming language allow you to compute numerical values. Here are the number operators in JavaScript:

```
+ : addition
- : subtraction
/ : division
* : multiplication
% : modulus
```

Number System

JavaScript uses a 64-bit, double precision floating point numbers, as shown in the below.

- The sign bit (the 31st bit): positive is 0, negative is 1.

- The nex 8 bits: the exponent value.

- The remaining 23 bits represent the fraction value.

This results in the following:

With decimal fractions, this floating-point number system causes some rounding
errors in JavaScript. For example,

**0.1 and 0.2 cannot be represented precisely.**- 자바스크립트의 숫자 시스템은 다른 언어와 달리 32-bit floating-point representation을 사용하고 있다.

- 기본적으로 숫자는 컴퓨터에 저장될 때 2진수로 저장된다. 그러나, 숫자 10이 2진수로 저장될 때 1001로 저장되는 것과 달리, 소수점은 깔끔하게 떨어지지 않는다. 따라서 아래와 같은 결과가 나온다.

To really understand why 0.1 cannot be represented properly as a 32-bit floating-
point number, you must understand

**binary**. Representing many decimals in binary requires an infinite number of digits. This because binary numbers are represented by*where**n*is an integer. JavaScript Number Object

Integer Rounding

Since JavaScript uses floating point to represent all numbers, integer division does not work.

## What is integer division?

3/2 = 1, 항상 정수만 리턴하는 나눗셈. round down 값을 반환

Integer division in programming languages like Java simply evaluates division expressions to their quotient (몫).

For example,

`5/4`

is 1 in Java because the quotient is` 1 `

(although there is a remainder of 1 left). However, in JavaScript, it is a floating point. `5/4 = 1.25`

This is because Java requires you to explicitly (명시적으로, 확실하게) type the integer as an integer.

Hence, the result cannot be a floating point. However, if JavaScript developers want to implement integer division, they can do one of the following:

`Math.round - rounds to nearest integer`

`Math.floor - rounds down to nearest integer`

`Math.ceil - rounds up to nearest integer`

Number.EPSLION

`Number.EPSILON`

returns the smallest interval between two representable numbers. This is useful for the problem with floating-point approximation.This function works by checking whether the difference between the two numbers
are smaller than

`Number.EPSILON`

. Remember that Number.EPSILON is

**the smallest difference between two***representable***numbers.**The difference between 0.1+0.2 and 0.3 will be smaller than Number.EPSILON.⇒ For example, the smallest number among the bigger number in 0.1 will be 0.00000000001…. something. However, computer can recognize that small number so they decide epsilon which is the smallest number of JS to compare the size of numbers. Therefore, the smallest number among the bigger number in 0.1 will be

`0.1 + EPSILON`

Maximums

Number.MAX_SAFE_INTEGER

It returns

**the largest integer**.## what is integer?

정수. 정수는 양의정수, 음의정수, 그리고 0이 있다. (분수, 소수 등 포함 x)

- The value of the largest integer n such that n and n + 1 are both exactly representable as
a Number value. Therefore,
**it does not work for floating-point decimals.**

Number.MAX_VALUE

It returns

**the largest floating-point number**possible. Equal to approximately 1.79E+308.Therefore, this uses double-precision floating-point representation and works for floating points as well.

Minimums

Number.MIN_SAFE_INTEGER

It returns the smallest integer. It is equal to -9007199254740991.

Number.MIN_VALUE

Number.MIN_VALUE returns the smallest floating-point number possible.

OR T

**he closest number to zero**that can be represented in JavaScript.Number.MIN_VALUE is equal to 5e-324. This is

**not a negative number**since it is the smallest*floating-point*number possible and means that Number.MIN_VALUE is actually bigger than Number.MIN_- SAFE_INTEGER.Number.MIN_VALUE is also the closest floating point to zero.

Infinity

The only thing greater than Number.MAX_VALUE is Infinity, and the only thing smaller than Number.MAX_SAFE_INTEGER is -Infinity.

Size Summary

```
Infinity < Number.MIN_SAFE_INTEGER < Number.MIN_VALUE < 0 < Number.MAX_
SAFE_IN- TEGER < Number.MAX_VALUE < Infinity
```

Number Algorithms

Primality Test

A primality test can be done by iterating from 2 to

*n*, checking whether modulus division (remainder) is equal to zero.## What is prime number?

소수. It is a natural number greater than 1 that is not a product of two smaller natural numbers.

What is a product? ⇒ In mathematics, a

**product**is the result of multiplication, or an expression that identifies objects (numbers or variables) to be multiplied, called*factors.*For example, 30 is the product of 6 and 5 (the result of multiplication) Time Complexity: O(n)

The time complexity is O(

*n*) because this algorithm checks all numbers from 0 to*n*. How to improve the Time Complexity?

- Think about how this method iterates through 2 to
*n*. Is it possible to find a pattern and make the algorithm faster? First, any multiple of 2s can be ignored, but there is more optimization possible. This is difficult to notice, but all primes are of the form,`6k ± 1`

**with the exception of 2 and 3**where k is some integer. Here’s an example:

- Also realize that for testing the prime number
*n*, the loop only has to test until the**square root of**

. This is because if the square root of*n**n*is not a prime number,*n*is not a prime number by mathematical definition.

After Time Complexity: O(*sqrt*(*n*))

This improved solution cuts the time complexity down significantly.

Prime Factorization

Prime numbers are the basis of

**encryption**(covered in Chapter 4) and**hashing**(covered in Chapter 11), and*prime factorization*is the process of determining which prime numbers multiply to a given number. Given 10, it would print 5 and 2.**Time Complexity: **O(sqrt(n))

This algorithm works by printing any number that is divisible by i without a
remainder. In the case that a prime number is passed into this function, it would be
handled by printing whether

*n*is greater than 2. Random Number Generator

Random number generation is important to simulate conditions. JavaScript has a built-in
function for generating numbers:

`Math.random()`

Itreturns a float between 0 and 1.

To get floating points higher than 1

Simply multiply Math.random() by the range. Add or subtract from it to set the base.

To get random integers

Simply use Math.floor(), Math.round(), or Math.ceil() to round to an integer.

Summary

Recall that all numbers in JavaScript are in 64-bit floating point format.

To get the smallest possible floating point increment, you should use Number.EPILSON. The
maximum and minimum numbers of JavaScript can be summarized by the following inequality:

Prime number validation and prime factorization are concepts used in various computer science applications such as encryption, as covered in Chapter 4. Finally, random number generation in JavaScript works via Math.random().

Share article