Home > Web Front-end > JS Tutorial > body text

Calculate the factors of a positive integer using JavaScript

藏色散人
Release: 2021-08-13 17:37:41
Original
4019 people have browsed it

In "Detailed explanation of how to multiply and divide two numbers through javascript", we introduce how to multiply and divide two numbers through javascript. Interested friends can Learn and learn~

The topic of this article is "How to write a JavaScript function to calculate the factors of a positive integer"?

So what is the factor? This belongs to elementary school knowledge. Everyone should know that a factor means that the quotient of an integer a divided by an integer b (b≠0) is exactly an integer without a remainder. We say that b is a factor of a.

After understanding what a factor is, we can easily calculate the factor of a positive integer through js code.

The complete implementation code is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
</head>
<body>

<script>
    function factors(n)
    {
        var num_factors = [], i;

        for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
            if (n % i === 0)
            {
                num_factors.push(i);
                if (n / i !== i)
                    num_factors.push(n / i);
            }
        num_factors.sort(function(x, y)
        {
            return x - y;});  // 数字排序
        return num_factors;
    }
    console.log(factors(15));  // [1,3,5,15]
    console.log(factors(16));  // [1,2,4,8,16]
    console.log(factors(17));  // [1,17]
</script>
</body>
</html>
Copy after login

We still use console.log() to view the output information, as follows:

Calculate the factors of a positive integer using JavaScript

Obviously the factors of 15, 16, and 17 are [1,3,5,15], [1,2,4,8,16], [1,17] respectively.

In the above code, the methods that everyone needs to know are:

sqrt()Method: can return the square root of a number;

→Note: The Math.pow() method can calculate any root of a number.

floor()Method: A number can be rounded down;

→Note: The floor() method performs downward rounding Rounding calculation, it returns the nearest integer that is less than or equal to the function parameter.

push()Method: One or more elements can be added to the end of the array and the new length is returned;

→Note: This method will change the length of the array. To add one or more elements to the beginning of the array, use the unshift() method.

sort()Method: used to sort the elements of the array.

→Note: The array is sorted on the original array and no copy is generated.

Finally, I would like to recommend "JavaScript Basics Tutorial" ~ Welcome everyone to learn ~

The above is the detailed content of Calculate the factors of a positive integer using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template