Automorphic Numbers in Java – Java中的无定形数

最后修改: 2022年 3月 15日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this short tutorial, we’ll discuss automorphic numbers and learn a couple of ways to find them along with Java programs.

在这个简短的教程中,我们将讨论自动数,并学习几种与Java程序一起寻找自动数的方法。

2. What Is an Automorphic Number?

2. What Is an Automorphic Number?/span>

An automorphic number is a number whose square has the same digits in the end as the number itself.

一个自动形态的数字是一个数字,它的平方在末尾的数字与该数字本身相同。

For example, 25 is an automorphic number because the square of 25 is 625, which ends with 25. Similarly, 76 is an automorphic number because the square of 76 is 5776, which again ends with 76.

例如,25是一个自变量,因为25的平方是625,它以25结束。同样地,76是一个自变量,因为76的平方是5776,同样以76结尾。

In mathematics, an automorphic number is also referred to as a circular number.

在数学中,自动数也被称为循环数。

Some more examples of automorphic numbers are 0, 1, 5, 6, 25, 76, 376, 625, 9376, etc.

更多的自变量例子有:0,1,5,6,25,76,376,625,9376等等。

0 and 1 are called trivial automorphic numbers as they are automorphic numbers in every base.

0和1被称为琐碎的自动数,因为它们在每个基数中都是自动数。

3. Determine if a Number Is Automorphic

3.确定一个数字是否是自动形态的

There are many algorithms available to determine whether a number is automorphic or not. Next, we’ll see a couple of approaches.

有许多算法可用于确定一个数字是否是自动的。接下来,我们将看到几个方法。

3.1. Loop Over the Digits and Compare

3.1.在数字上循环并比较

Here’s one way to determine if a number is automorphic:

这里有一种方法可以确定一个数字是否是自动的。

  1. get the number and calculate its square
  2. get the last digit of the square and compare it with the last digit of the number
    • if the last digits are not equal, the number is not an automorphic number
    • if the last digits are equal, go to the next step
  3. remove the last digit of both number and square
  4. repeat steps 2 & 3 until all the digits of the number are compared

The above approach loops through the digits of the input number in a reverse way.

上述方法以相反的方式对输入数字的位数进行循环。

Let’s write a Java program to implement this approach. The isAutomorphicUsingLoop() method takes an integer as input and checks if it’s automorphic:

让我们写一个Java程序来实现这种方法。isAutomorphicUsingLoop()方法接收一个整数作为输入并检查它是否是自动的。

public boolean isAutomorphicUsingLoop(int number) {
    int square = number * number;

    while (number > 0) {
        if (number % 10 != square % 10) {
            return false;
        }
        number /= 10;
        square /= 10;
    }
    
    return true;
}

Here, we first calculate the square of number. Then, we iterate over the digits of number and compare its last digit with that of square, one by one.

这里,我们首先计算number的平方。然后,我们遍历number的数字,并将其最后一位数字与square的数字逐一比较。

At any stage, if the last digits are not equal, we return false and get out of the method. Otherwise, we get rid of the equal last digits and repeat the process for the remaining digits of number.

在任何阶段,如果最后的数字不相等,我们返回false并退出该方法。否则,我们就去掉最后的数字,对number的其余数字重复这个过程。

Let’s test this out:

让我们来测试一下。

assertTrue(AutomorphicNumber.isAutomorphicUsingLoop(76));
assertFalse(AutomorphicNumber.isAutomorphicUsingLoop(9));

3.2. Compare Digits Directly

3.2.直接比较数字

We can also determine whether a number is automorphic or not in a more straightforward way:

我们还可以用更直接的方式来确定一个数是否是自动的。

  1. get the number and count the number of digits (n)
  2. calculate the square of the number
  3. get the last digits from the square
    • if the last digits of the square make the original number, the number is automorphic
    • else it’s not an automorphic number

In this case, we need not loop through the digits of the number. Instead, we can use the existing libraries of the programming framework.

在这种情况下,我们不需要对数字的位数进行循环。相反,我们可以使用编程框架的现有库。

We can make use of the Math class to perform numeric operations such as counting the digits in the given number and getting those many last digits from its square:

我们可以利用Math类来进行数字运算,例如计算给定数字中的位数,并从其平方中获得最后的那几个数字:

public boolean isAutomorphicUsingMath(int number) {
    int square = number * number;

    int numberOfDigits = (int) Math.floor(Math.log10(number) + 1);
    int lastDigits = (int) (square % (Math.pow(10, numberOfDigits)));

    return number == lastDigits;
}

Similar to the first approach, we start with calculating the square of number. Then, instead of comparing the last digits of number and square one by one, we get the total numberOfDigits in number at once by using Math.floor(). After that, we extract as many digits from square by using Math.pow(). In the end, we compare the input number with the extracted number lastDigits.

与第一种方法类似,我们先计算number的平方。然后,我们不是逐一比较numbersquare的最后一位数字,而是通过使用Math.floor()一次性获得numberOfDigits的总数。之后,我们通过使用Math.pow()square中提取尽可能多的位数。最后,我们将输入的数字与提取的数字lastDigits进行比较。

If the number and lastDigits are equal, the number is automorphic and we return true, otherwise, we return false.

如果numberlastDigits相等,这个数字是自动的,我们返回true,否则,我们返回false

Let’s test this out:

让我们来测试一下。

assertTrue(AutomorphicNumber.isAutomorphicUsingMath(76));
assertFalse(AutomorphicNumber.isAutomorphicUsingMath(9));

4. Conclusion

4.总结

In this article, we explored automorphic numbers. We also looked at a couple of ways to determine if a number is an automorphic number along with the corresponding Java programs.

在这篇文章中,我们探讨了自定型数。我们还研究了几种确定一个数是否为自变量的方法,以及相应的Java程序。

As always, the code for these examples is available over on GitHub.

像往常一样,这些例子的代码可以在GitHub上找到over