Java 如何检查一个数字是否是Keith数

Java 如何检查一个数字是否是Keith数

如果一个数字被安排了一个特殊的数字序列,该序列最初由数字创建。

之后,序列的数字将通过添加其前面的数字生成,直到该数字超过输入数字。结果将决定最后一个数字是否与输入数字相同。

在生成序列时,记住只添加第n个数字,其中n指的是原始输入数字中的数字个数。

在本文中,我们将使用Java编程语言来检查一个数字是否是Keith数。

为了向您展示一些实例

实例1

输入数字为19

让我们通过使用Keith数的逻辑进行检查 −

通过分离19的数字,我们得到第一个序列= 1, 9。

现在添加所有数字= 1 + 9 = 10。

现在新的序列是1, 9, 10。

现在添加最后两个数字= 9 + 10 = 19。

所以新的序列是1, 9, 10, 19。

在这里我们可以看到序列的最后一个数字与我们的输入数字或原始数字相同。

因此,19是一个Keith数。

实例2

输入数字为197

让我们通过使用Keith数的逻辑进行检查 −

通过分离197的数字,我们得到第一个序列= 1, 9, 7。

现在添加所有数字= 1 + 9 + 7 = 17。

现在新的序列是1, 9, 7, 17。

现在添加最后三个数字= 9 + 7 + 17 = 33。

新的序列是1, 9, 7, 17, 33。

现在添加最后三个数字= 7 + 17 + 33 = 57。

新的序列是1, 9, 7, 17, 33, 57

如果我们按照相同的步骤进行,我们将得到序列= 1, 9, 7, 17, 33, 57, 107, 197。

在这里我们可以看到序列的最后一个数字与我们的输入数字或原始数字相同。

因此,197是一个Keith数。

实例3

输入数字为152

让我们通过使用Keith数的逻辑进行检查 −

通过分离152的数字,我们得到第一个序列= 1, 5, 2。

现在添加所有数字= 1 + 5 + 2 = 8。

现在新的序列是1, 5, 2, 8。

现在添加最后三个数字= 5 + 2 + 8 = 15。

新的序列是1、5、2、8、15。

现在将最后三个数字相加= 2 + 8 + 15 = 25。

新的序列是1、5、2、8、15、25。

如果我们按照相同的步骤进行,我们将得到序列= 1、5、2、8、15、25、48、88、161。

在这里,我们可以看到序列的最后一个数字超过了我们的输入数字或原始数字。

因此,152不是一个Keith数。

其他一些Keith数的示例包括19、197、742、1537等。

步骤

  • 第1步 - 获取一个整数,可以通过初始化或用户输入获得。

  • 第2步 - 声明一个用于存储计算数字的数组。

  • 第3步 - 初始时找到输入数字的位数。

  • 第4步 - 然后使用我们的算法在循环中计算下一个值,直到超过输入数字。

  • 第5步 - 最后,将最后计算的数字与输入数字进行比较,如果两者相同,则我们将得出结论,输入数字是Keith数,否则输入数字不是Keith数。

方法

我们以不同的方法提供了解决方案。

  • 通过使用静态输入值

让我们逐个查看程序及其输出。

方法:通过使用静态输入值

在这种方法中,用户将被要求输入输入数字,然后使用算法可以检查该数字是否是Keith数。

示例

public class Main{
   //main method
   public static void main(String[] args){

      //declared an integer variable and initialized the value
      int inputNumber = 14;

      //print the given input number
      System.out.println("Given number: "+inputNumber);

      //store it to an another temporary variable
      int temporaryNumber = inputNumber;

      //type casting it to string
      String str = Integer.toString(inputNumber);

      //find the length and store the length value into a variable
      int len =str.length();

      //declare a array which store the input number
      int store[]=new int[inputNumber];
      int i, sum;

      //initiate the looping for break the input number into single digits
      for(i=len-1; i>=0; i--){

         // store the digits into the same array
         store[i]=temporaryNumber % 10;
         temporaryNumber = temporaryNumber/10;
      }
      i=len; sum=0;

      //start iteration for calculating the next numbers
      while(sum<inputNumber){
         sum = 0;
         for(int j=1; j<=len; j++){
            sum=sum+store[i-j];
         }

         //store the calculated numbers into the array
         store[i]=sum;
         i++;
      }
      //check the resultant number is matched to the input number or not
      if(sum==inputNumber)
         System.out.println(inputNumber + " is a Keith Number.");
      else
         System.out.println(inputNumber + " is not a Keith Number.");
   }
}

输出

Given number: 14
14 is a Keith Number.

在这篇文章中,我们探讨了如何使用不同的方法在Java中检查一个数字是否是Keith数字。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程