Java computing Three-digit narcissus implementation and optimization method

Source: Internet
Author: User
Tags pow
/* Print out all the "Narcissus number", the so-called "Narcissus number" refers to a three-digit, its members of the digital cubic and equal to the number itself. For example: 153 is a "narcissus number", because of the three of the 153=1 +5 of the three square +3 of three times because of 100 200 300 500 400 The whole hundred is not a narcissus can be directly excluded and 101 is not the basis of the Narcissus can be directly excluded from these two laws to eliminate the direct skip calculation * *Public classNarcissus {/* Start run time 1524629170834 153 370 371 407 End Run time 1524629170838 time-consuming: 4 because there are 18 skipped meters The cubic method avoids the redundancy of the computational steps, so it runs faster.Public voidPrintnarcissus () { for(inti = 102; I < 999; i++) {if(i% = 0 | | | I% 101 = 0)Continue;intj = i/100, k = i/10%, z = i% 10;if(i = = Math.pow (j, 3) + Math.pow (k, 3) + Math.pow (z, 3)) System. out. println (i); /** * Traditional algorithm Run results * <p> * Start run time 1524629049929 * Narcissus number is: 153 * Narcissus number is: 370 * Narcissus number is: 371 * Narcissus number is: 407 * End Run time 1524629049949 * Time-consuming: 21 *Public voidSS () { for(intNumber = 100; Number <= 999; number++) {intGewei = number% 10;intShiwei = number/10% 10;intBaiwei = number/100% 10;if(Gewei * Gewei * gewei + Shiwei * Shiwei * shiwei + baiwei * Baiwei * Baiwei = number) {System. out. println ("The number of daffodils is:"+ number); }
        }
    }
}
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.