Three classic cases for beginners in JS-palindrome number, daffodil number, Fibonacci sequence

Three classic cases for beginners in JS-palindrome number, daffodil number, Fibonacci sequence

Palindrome

Palindrome

  • "Palindrome" refers to a sentence that can be read through both front and back. It is a rhetorical method and word game in ancient and modern Chinese and foreign countries, such as "I am for everyone, and everyone is for me".

In mathematics, there is also such a type of numbers that have such characteristics and become palindrome numbers. (For example: 121,222,323, etc.)

Palindrome number example

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回文数</title>
</head>

<body>
    <script>
        window.onload = function () {
     
     
            for (var a = 1; a < 10; a++) {
     
     
                if (a == a % 10) {
     
     
                    document.write(a + "&nbsp;")
                }
            }
            document.write("<br/>")
            for (var b = 10; b < 100; b++) {
     
     
                if (parseInt(b / 10) == b % 10) {
     
     
                    document.write(b + "&nbsp;")
                }
            }
            document.write("<br/>")
            for (var b = 100; b < 1000; b++) {
     
     
                if (parseInt(b / 100) == b % 10) {
     
     
                    document.write(b + "&nbsp;")
                }
            }
            document.write("<br/>")
            for (var b = 1000; b < 10000; b++) {
     
     
                if (parseInt(b / 1000) == b % 10 && parseInt(b / 100) % 10 == parseInt((b % 100) / 10)) {
     
     
                    document.write(b + "&nbsp;")
                }
            }

        }
    </script>
</body>

</html>

Number of daffodils

  • The daffodil number is also called the super-complete number invariant number, narcissistic number, self-power number , Armstrong number or Armstrong number
  • The number of daffodils refers to a 3 digit number , and the sum of the powers of 3 of each digit is equal to itself (for example: 1^3 + 5^3+ 3^3 = 153)

The daffodil number is only a kind of self-power. Strictly speaking, the 3rd power of 3 digits is called the daffodil number.

Attachment: the names of the power numbers of other digits

  • One power number: single number
  • Two powers: none
  • Three power numbers: the number of daffodils
  • Four-digit power: the number of four-leaf roses
  • Five-digit power: number of five-pointed stars
  • Six powers: six
  • Seven-digit power number: Big Dipper number
  • Power of eight digits: eight cents
  • Nine-digit self-power number: Nine-Nine Double Ninth Number
  • Power of Tens: Perfect Number

Examples of daffodil numbers

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>水仙花数</title>
</head>

<body>
    <script>
        window.onload = function () {
     
     
            for (var a = 2; a < 1000; a++) {
     
     
                var i = parseInt(a / 100);
                var j = parseInt((a % 100) / 10);
                var n = parseInt((a % 100) % 10);
                if (a == (i * i * i) + (j * j * j) + (n * n * n)) {
     
     
                    document.write(a + "&nbsp;")
                }
            }


        }
    </script>
</body>

</html>

Fibonacci sequence

The Fibonacci sequence uses the idea of ​​recursive algorithm.

So what is recursion?

  • Recursion is a repetitive process in which a function calls itself
  • Therefore, the use of recursion to deal with problems and the use of loops to deal with problems have the same feature that they must be infinite recursion (infinite loop)

After understanding recursion, let's go back and see what is the Fibonacci sequence

  • Fibonacci sequence is also called rabbit sequence
  • The Fibonacci sequence is a sequence of numbers starting from the third term, the value of the third term is the sum of the first term and the second term and so on

Fibonacci sequence example

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>斐波那契</title>
</head>

<body>
    <!-- 斐波那契 -->
    <script>
        window.onload = function () {
     
     
            var num = prompt("请输入使用递归算法求指定位数的斐波那契数列的位数: ");
            function fbnq(i) {
     
     
                if (i == 1 || i == 2) {
     
     
                    return 2;
                } else {
     
     
                    return fbnq(i - 1) + fbnq(i - 2);
                }
            }
            for (n = 1; n <= num; n++) {
     
     
                document.write(fbnq(n) + "&nbsp;" + "&nbsp;")
            }
        }

    </script>
</body>

</html>

Guess you like

Origin blog.csdn.net/XVJINHUA954/article/details/110947915