Kotlin algorithm Introduction to palindrome number algorithm optimization two number generation rule __ algorithm

Source: Internet
Author: User
Tags pow
classPalindromeNumber2 {/* generates a single-digit palindrome number */Private FunAdigit (Parentnumber:long, Tenid:long) {ValTenidresult = Math.pow (10.0, Tenid.todouble ()). Tolong () for(Iinch0..9) {println (parentnumber + i * tenidresult)}/* generates two digits */Private FunTwodigit (Parentnumber:long, Tenid:long, Iszero:boolean) {vari = 0if(iszero) i = 0Elsei = 1ValTenidresult = Math.pow (10.0, Tenid.todouble ()). Tolong () while(I <= 9)

    {println (parentnumber + one * i.tolong () * tenidresult) i++}} /* Generate three-digit number */Private FunThreedigit (Parentnumber:long, Tenlid:long, Isthree:boolean, Tenid:long) {varI:long = 1if(!isthree) i = 0 while(I <= 9) {varresult = Parentnumberif(Isthree) {Adigit ((i * MATH.POW (10.0, 2.0) + i). Tolong (), Tenid)}Else{result + = (I * MATH.POW (10.0, (2 * tenid-tenlid). ToDouble ()) + I * MATH.POW (10.0, Tenlid.todouble ())).
                Tolong ()//System.out.println ("3-digit" +i+ "P ..." +result+ "i>>>>>" +i); Adigit (result, Tenid)} ++i}/* generates four-digit digits */Private FunFourdigit (Parentnumber:long, Tenfid:long, Tenlid:long, Isfour:boolean, Tenid:long) {varI:long = 1if(!isfour) i = 0 while(I <= 9) {if(Isfour) {Twodigit ((i * MATH.POW (10.0, 3.0) + i). Tolong (), Tenid,true)
            }Else{Twodigit ((i * MATH.POW (10.0, tenfid.todouble ()) + I * MATH.POW (10.0, tenlid.todouble ()) + parentnumber.t Odouble ()). Tolong (), TenID-1,true)} i++}}/* Specifies a palindrome digital print from single digit to n digits */ FunPalindromenumber (N:long) {if(n <= 0)return for(Iinch1..N) {if(i = = 1L) {adigit (0, 0)Continue }else if(i = = 2L) {twodigit (0, 0,false)Continue }else if(i = = 3L) {threedigit (0, 0,true, 1)Continue }else if(i = = 4L) {fourdigit (0, 0, 0,true, 1)Continue } odddigitpalindromenumber (i, 0,false, 0, 0)}} FunOdddigitpalindromenumber (N:long, Tenid:long, Iszero:boolean, Pn:long, Lasttenid:long) {varTenid = TenidvarPN = PNvarLasttenid = Lasttenidvari = 0/* Determine if the opening is 0*/if(!iszero) {Tenid = N/2 i = 1}/* greater than and equal to 5 + odd-digit palindrome number generation method */if(n = = 3L)
            {//System.out.println ("NT" +nexttenid); Threedigit (PN, Lasttenid,false, Tenid)return }else if(n = = 4L)
            {//System.out.println ("LId:::" + (2*tenid-lasttenid-1)); Fourdigit (PN, 2 * tenid-lasttenid-1, Lasttenid,false, Tenid)return }/* greater than and equal to 6 or more even number Palindrome number Generation method */ while(I <= 9) {varresult = PNif(!iszero) {PN = (i * MATH.POW (10.0, (n-1). ToDouble ()) + i). Tolong () result = PN L Asttenid = 0}Else{result + = (I * MATH.POW (10.0, (2 * tenid-lasttenid). ToDouble ()) + I * MATH.POW (10.0, LASTTENID.TODOUBL
            E ())). Tolong ()//System.out.println ("res" +result);
            }//if (!iszero) System.out.println ("I" +i); ++i Odddigitpalindromenumber (n-2, Tenid,true, result, Lasttenid + 1)}}//test to generate a palindrome number method for n alone FunFire () {odddigitpalindromenumber (6, 0,false, 0, 0)}}

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.