Home >Java >javaTutorial >Example of Java implementation of narcissus number algorithm between 10-100000

Example of Java implementation of narcissus number algorithm between 10-100000

黄舟
黄舟Original
2017-10-18 10:16:052042browse

This article mainly introduces the Java algorithm for finding the narcissus number between 10 and 100,000. It analyzes the concept of narcissus number and the corresponding Java algorithm implementation skills in the form of examples. Friends who need it can refer to it

The example in this article describes the Java algorithm for finding the narcissus number between 10 and 100,000. Share it with everyone for your reference, the details are as follows:

Daffodil number:

Concept: The daffodil number refers to an n-digit number ( n≥3), the sum of the nth power of the numbers on each digit is equal to itself. (For example: 1^3 + 5^3+ 3^3 = 153)

Algorithm idea analysis: We perform this algorithm in two steps; first: we make a method to find the number of digits in a number Function; second: we call this function to calculate prime numbers between 10 and 100000!

The specific code is given below (for reference only):


package javastudy;
public class Testit1 {
  public static void main(String[] args) {
    for (int number = 100; number <= 100000; number++) { //求10到10000之间所有的素数,一个大的循环
      int temp = number; //这步代码是为了让Getlength(number)不变
      int sum = 0;
      for (int i = 0; i < Getlength(number); i++) {
        sum += Math.pow(temp % 10, Getlength(number)); //次方和相加
        temp = temp / 10;
      }
      if (sum == number) { //判断是否相等
        System.out.print(number+" ");
      }
    }
  }
  static int Getlength(int number) { //这个函数是为了求一个数字的位数
    int i = 0;
    while (number / 10 > 0) {
      i++;
      number /= 10;
    }
    return i + 1;
  }
}

operation result:

The above is the detailed content of Example of Java implementation of narcissus number algorithm between 10-100000. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn