JavaScript implements palindrome number, narcissus number judgment and output Fibonacci sequence

Source: Internet
Author: User

    //  judge if a number is a palindrome                   //  method One: First convert the number to a string, then the first and the last number, then the second and the penultimate number ... is equal to     function palindromenumber1 (num) {         var str = num.tostring ();         var flag  = true;        var len = str.length;         for (var i = 0; i <  (len -  1) ( / 2; i++) {            if (  str.charat (i)  != str.charat (len-i-1)  ) {                 flag = false;                 break;            }    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (flag) {             console.log (num +  "  is a palindrome number");         }else{             console.log (num +  "  not palindrome number");                     }    }     PalindromeNumber1 (123456321); // 123456321  is not a palindrome number              //  method Two: Another way of writing     function palindromenumber2 (num ) {        var str = num.tostring ();          Var flag = true;        var begin = 0,  end = str.length - 1;           while ( begin < end ) {             if (Str.charat (BEGIN)  == str.charat (end)) {                 begin ++;                 end --;             }else{                 flag = false;                 break;            }     &nbSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (flag) {             console.log (num +  "  is a palindrome number");         }else{             Console.log (num +  "  not palindrome number");                     }    }     PalindromeNumber2 (123456321); // 123456321  is not a palindrome number              //  method Three:  will judge the number inversion, and then determine whether the two numbers before and after the equality     function  Invertednumber (num) {         var nnum= 0;         while ( num != 0) {              nnum *= 10;            nnum =  nnum +  (nnum % 10);             num = math.floor ( num / 10 );          }        return nNum;    }     function palindromenumber3 (num) {        var  nnum = invertednumber (num);  //  a number in reverse order          if ( nnum == num) {                 //  determine if the inverse number is the same as the original number              console.log (num +  "  is a palindrome number");         } Else{    &nbsP;       console.log (num +  "  not palindrome number");          }    }    palindromenumber3 ( 123456321); // 123456321  not palindrome number                  //  judge whether a number is Narcissus number:  daffodil number refers to a  n  digit   ( n≥3 ), The sum of the numbers on each of its bits is equal to its own  n  power.         //   converts an array to a string, and then adds the number n power to each position, respectively      function narcissusnumber (num) {        var str =  num.tostring ();        var len = str.length;         var result = 0;         for (var i=0; i<len; i++) {          &Nbsp;  result += math.pow (Str.charat (i),  len);         }        if (Result == num) {             console.log (num +  "  is narcissus number");         }else{             console.log (num +  "  not daffodils");        }     }    narcissusnumber (153);                     //  Fibonacci sequence:  output before n number     /*         Fibonacci Series: 1, 1, 2, 3, 5, 8, 13, 21, 、......           function: Use formula F[n]=f[n-1]+f[n-2], recursive calculation, recursive end condition is f[1]=1,f[2]=1.     */        //  recursive:  the nth Fibonacci number   (  method one  )     function &NBSP;FB1 (n) {         if ( n==1 | | &NBSP;N==&NBSP;2) {            return 1;  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;FB1 (n-1 ) &NBSP;+&NBSP;FB1 (n-2);    }    //  output First n Fibonacci number     &NBSP;FUNCTION&NBSP;FIBONACCI1 (num) {        console.log (1);         for (var i=2; i <= num; i++) {             console.log (FB1 (i));      &NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;FIBONACCI1 (5);             //  non-recursive:  to find the nth Fibonacci number   (  method two  ) &NBSP;&NBSP;&NBSP;&NBSP;FUNCTION&NBSP;FB2 (n) {         var a =1, b =1, result=1;         for (var i =2; i<= n; i++) {             result = a + b;             a = b;             b = result;        }         return result;    }    //   Outputs the first n Fibonacci numbers     function fibonacci2 (num) {         console.log (1);         for (var i=1; i <  num; i++) {             console.log (FB2 (i));         }    }    fibonacci2 (5);            //  non-recursive: Find the nth Fibonacci number   (  method two  )     FUNCTION&NBSP;FB3 (n) {       var result = [1,1];        if ( n== 1 | |  n ==2) {            return 1;        }       for (var i = 2;  i < n; i++ ) {             result[i] = result[i-1] + result[i-2];       }        return result[n-1];  &nBSP;}    //  outputs the first n Fibonacci numbers    function fibonacci3 (num) {         console.log (1);         for (var i=1;  i <= num; i++) {             Console.log (FB3 (i));        }    }     fibonacci3 (5);

JavaScript implements palindrome number, narcissus number judgment and output Fibonacci sequence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.