Calculate the number of daffodils between 100 and 999, and the number of 100999 daffodils.

Source: Internet
Author: User

Calculate the number of daffodils between 100 and 999, and the number of 100999 daffodils.


Question: print out all the numbers of daffodils. The so-called Number of daffodils refers to a three-digit number. Each digit is equal to the number itself.

For example, 153 is a daffodils because 153 = the power of 1 + the power of 5 + the power of 3.


Public static void main (String [] args) {System. out. println (Arrays. toString (getNarcissisticNumber (100,999);}/*** print out all the daffodils. The so-called daffodils refer to a three-digit number, each of which is equal to the number itself. * For example, 153 is a daffodils because 153 = the power of 1 + the power of 5 + the power of 3. * @ Param start * @ param end * @ return */public static int [] getNarcissisticNumber (int start, int end) {// defines the result array, used to store the number int [] arr = new int [end-start] that complies with the rules. // index is used as the counter to record the number of elements saved in the arr array. int index = 0; // define an array that stores hundreds and tens of digits respectively. int [] args = new int [3]; for (int I = start; I <end; I ++) {args [0] = I/100; args [1] = I % 100/10; args [2] = I % 100% 10; // calculate the cube of each number and // double sum = Math. pow (args [0], 3) + Math. pow (args [1], 3) + Math. pow (args [2], 3 ); int sum = args [0] * args [0] * args [0] + args [1] * args [1] * args [1] + args [2] * args [2] * args [2]; // determine whether each cube is equal to the current number if (sum = I) {arr [index] = I; index ++ ;}} // copy the number of daffodils stored to remove the initial 0 return Arrays. copyOf (arr, index );}

 

A distorted statement, defining a method, and determining whether a number is a daffodils number

/*** Determine whether the number is a daffodils Number * @ param num into the number * @ return true is a daffodils number; false is not */public static boolean isNarcissisticNumber (int num) {// define an array that stores hundreds and ten digits respectively. int [] args = new int [3]; args [0] = num/100; args [1] = num % 100/10; args [2] = num % 100% 10; // calculate the cube and int sum of numbers = args [0] * args [0] * args [0] + args [1] * args [1] * args [1]] + args [2] * args [2] * args [2]; return sum = num ;}



Call the isNarcissisticNumber (int num) method in the Method

public static void main(String[] args) {for (int i=100; i<=999; i++) {if (isNarcissisticNumber(i)) {System.out.println(i);}}}




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.