Java 如何检查一个数是否为奇异数

Java 如何检查一个数是否为奇异数

如果一个数能同时整除每个数字的平方和和所有数字的乘积的平方,则称为奇异数。为了更清楚起见,我们需要找到每个数字的平方和,然后将给定数字的每个数字相乘,然后计算其平方值。如果给定数字能同时整除两个结果值,那么我们可以说这个给定数字是一个奇异数。

本文将介绍如何使用Java编程语言来检查一个数是否为奇异数。

为了给您一些实例

实例1

输入数字为1122112。

让我们使用奇异数的逻辑来检查一下。

1122112的每个数字的平方和为=

= (1^2) + (1^2) ++ (2^2) + (2^2) + (1^2) + (1^2) + (2^2)
= 1 + 1 + 4 + 4 + 1 + 1 + 4 = 16

全部数字1122112的所有位数的生产总值的平方 =

=(1 * 1 * 2 * 2 * 1 * 1 * 2 ) ^ 2 = 64

1122112可以被16和64整除。

因此,1122112是一个奇特的数字。

实例2

输入数字是123123。

让我们使用奇特数字的逻辑来检查它。

123123的每个数字的平方的加法为=

=(1^2) + (2^2) ++ (3^2) + (1^2) + (2^2) + (3^2) 
= 1 + 4 + 9 + 1 + 4 + 9 = 28

123123的所有数字的整体生产值的平方=

=(1 * 2 * 3 * 1 * 2 * 3) ^ 2 = 1296

123123既不能被28整除,也不能被1296整除。

因此,123123不是一个奇异数。

实例3

输入数字为121212。

让我们使用奇异数的逻辑来检查它。

121212的每个数字的平方的和为=

= (1^2) + (2^2) ++ (1^2) + (2^2) + (1^2) + (2^2) 
= 1 + 4 + 1 + 4 + 1 + 4 = 15

整个121212的所有数字的生产总值的平方=

= (1 * 2 * 1 * 2 * 1 * 2) ^ 2 = 64

121212不能同时被15和64整除。

因此,123123不是一个Insolite数。

语法

要在Java中获得一个数的另一个数的幂,我们有内置的 java.lang.Math.pow() 方法。

以下是使用该方法获得2的幂的语法。

double power = Math.pow(inputValue,2)

步骤

  • 步骤1 - 通过初始化或用户输入获取一个整数。

  • 步骤2 - 然后循环直到二进制数为零。

  • 步骤3 - 计算每位数字的平方的和与乘积。

  • 步骤4 - 最后,检查结果值是否可被输入的整数整除。

  • 步骤5 - 如果可以整除,则判断该数字是一个不寻常的数字;否则不是。

多种方法

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

  • 使用静态输入值

  • 使用用户定义的方法

让我们一起看看程序及其逐步输出。

方法1:使用静态输入值

在这种方法中,程序中会初始化一个整数值,然后通过使用算法来检查一个数字是否是不寻常的数字。

示例

import java.util.*;
import java.lang.Math;
public class Main{
   //main method 
   public static void main (String[] args){

      //declare a variable with a static input value
      int inputNumber = 1122112 ;

      //store the input number into an another variable
      int temp = inputNumber;

      // for storing the addition value declare a variable
      int sum = 0;

      // for storing the multiplication value declare a variable
      int prod = 1;

      //loop to calculate both sum and product value
      while (temp != 0){

         // take digits from unit place
         int i = temp % 10;

         //calculate the sum value
         sum = sum + (int)Math.pow(i, 2);

         //calculate the multiplication value
         prod = prod * (int)Math.pow(i, 2);

         //removing that unit place value
         temp = temp / 10;
      }
      //check the condition
      //whether both resultant values are divisible by inputNumberor not
      if((inputNumber % sum == 0) && (inputNumber % prod == 0))
         System.out.print(inputNumber + " is an Insolite Number.");
      else 
         System.out.print(inputNumber + " is not an Insolite Number.");
   }
}

输出

1122112 is an Insolite Number.

方法2:使用用户定义

在此方法中,用户将被要求输入一个整数值,然后我们将通过将此输入数字作为参数来调用一个用户定义的方法。

在方法内部,我们将使用算法来检查一个数是否为奇特数。

示例

public class Main{
   //main method
   public static void main (String[] args){
      //declare an int variable and initialize a number
      int inputNumber =126317;
      System.out.println("Given number: "+inputNumber);

      //call the user defined method inside conditional statement
      if (checkInsolite(inputNumber)){
         //if its true
         System.out.println(inputNumber + " is an Insolite Number.");
      } else {
         //if its true
         System.out.println(inputNumber + " is not an Insolite Number.");
      }
   }     
   // define the user defined method
   // check for Insolitenumber
   static boolean checkInsolite(int inputNumber){
      //store the input number into an another variable
      int temp = inputNumber;
      // for storing the addition value declare a variable
      int sum = 0;
      // for storing the multiplication value declare a variable
      int prod = 1;
      //loop to calculate both sum and product value
      while (temp != 0){

         // take digits from unit place
         int i = temp % 10;
         //calculate the sum value
         sum = sum + i * i;
         //calculate the multiplication value
         prod = prod * i * i;
         //removing that unit place value
         temp = temp / 10;
      }
      //check the condition
      //whether both resultant values are divisible by inputNumberor not
      if((inputNumber % sum == 0) && (inputNumber % prod == 0))
         return true;
      else 
         return false;
   }
}

输出

Given number: 126317
126317 is not an Insolite Number.

在本文中,我们探讨了如何使用不同的方法在Java中检查一个数字是否为不常见的数字。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程