Kotlin algorithm for getting started calculating Narcissus number __ algorithm

Source: Internet
Author: User
Tags pow
/* Print out all the "daffodils", the so-called "Narcissus number" refers to a three-digit number, whose numbers are cubic and equal to the number itself. For example: 153 is a "narcissus number", because the 153=1 three square +5 of the three +3-time side three of the square because of 100 200 300 500 400 The whole hundred is not a narcissus can be directly ruled out and 101 for the base is not a narcissus can be directly ruled out the direct skip calculation of these two rules */classNarcissus {/* Start run time 1524629170834 153 370 371 407 End-of-run time 1524629170838 time: 4 because there are 18 skipped meters The cubic method avoids the redundancy of the calculation steps so it runs faster */ FunPrintnarcissus () { for(Iinch102..998) {if(i% = = 0 | | I% 101 = = 0)ContinueValj = i/100Valk = i/10% 10Valz = i% 10if(i.todouble () = = Math.pow (j.todouble (), 3.0) + Math.pow (k.todouble (), 3.0) + Math.pow (z.todouble (), 3.0)) println (i)}}/** * Traditional algorithm Run result * * * start run time 1524629049929 * Daffodil number is: 153 * Narcissus number is : 370 * Narcissus number is: 371 * Narcissus number is: 407 * End of Run time 1524629049949 * Time: 21 * * FunSS () { for(numberinch100..999) {ValGewei = number% 10ValShiwei = number/10% 10ValBaiwei = number/100% 10if(Gewei * Gewei * gewei + Shiwei * Shiwei * shiwei + baiwei * Baiwei * Baiwei = = number) {println ("The number of daffodils is:$Number")
            }
        }
    }
}

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.