Java 检查给定的数列是算术级数还是几何级数或和声级数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22132399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:48:39  来源:igfitidea点击:

To check whether the given number series is in Arithmetic progression or Geometric progression or Harmonic progression

java

提问by ash1sh

I am writing a program to check whether the given number series is in AP, GP or HP. I know that AP is based on common difference and GP is based on common ratio. My code works for AP and GP but fails for HP. What corrections should be made in the following code, also is there a way to optimize my code. Following is the program:

我正在编写一个程序来检查给定的数字系列是否在 AP、GP 或 HP 中。我知道AP是基于公差,GP是基于公比。我的代码适用于 AP 和 GP,但不适用于 HP。下面的代码应该做哪些更正,还有没有优化我的代码的方法。以下是程序:

import java.util.*;
class APGPHP 
{
public static void main(String[] args) 
{
    int n;
    System.out.println("Enter the number of elements in the series");
    Scanner s = new Scanner(System.in);
    n = s.nextInt();
    int[] a = new int[n];
    System.out.println("Enter the numbers");
    for (int i = 0;i<n ; i++)
    {
        System.out.println("Enter the number ["+(i+1)+"] :");
        a[i] = s.nextInt();
    }
    CheckAPGPHP(a); 
}

public static void CheckAPGPHP(int[] a)
{
    if(a.length<3)
    {
        System.out.println("Array should contain atleast 3 elements ");
        return;
    }
    if(CheckAP(a) == 1)
    {
        System.out.println("AP");
        if (CheckHP(a) == 1)
            System.out.println("HP");
    }

    if(CheckGP(a) == 1)
        System.out.println("GP");
}

public static int CheckAP(int[] a)
{
    int iDiff=a[1]-a[0];
    for(int i=0;i<a.length-1;i++)
    {
        if((a[i+1]-a[i])!=iDiff)
            return -1;
    }
    return 1;
}

public static int CheckGP(int[] a)
{
    int iRatio=a[1]/a[0];
    for(int i=0;i<a.length-1;i++)
    {
        if((a[i+1]/a[i])!=iRatio)
            return -1;
    }
    return 1;
}

public static int CheckHP(int[] a)
{
    float[] b = new float[a.length];
    for (int i =0;i<=b.length-1 ;i++ )
    {
        b[i] = 1.0f/a[i];
    }
    float iDiff=b[1]-b[0];
    for(int i=0;i<b.length-1;i++)
    {
        if((b[i+1]-b[i])!=iDiff)
            return -1;
    }
    return 1;
}
}

回答by Tyler Brabham

First, instead of checking equality, check if the numbers are within some small tolerance of each other, such as 1e-6 or so. The reason is that float division is not exact.

首先,不是检查相等性,而是检查数字是否在彼此的小容差范围内,例如 1e-6 左右。原因是浮点除法不准确。

For example, in python (5.0 - 4.9) returns 0.09999999999999964, instead of .1. I know this is not division, but it's a simple example to show that floating point arithmetic is not exact.

例如,在 python (5.0 - 4.9) 中返回 0.09999999999999964,而不是 .1。我知道这不是除法,但这是一个简单的例子,表明浮点算术并不准确。

回答by Dawood ibn Kareem

I see threemajor issues with your code. There may be other minor issues.

我发现您的代码存在三个主要问题。可能还有其他小问题。

  • You have put your call to CheckHPinside the branch that only runs if CheckAPreturns 1. So you'll only ever check that the numbers are in harmonic progression if they turn out to be in arithmetic progression. So it's never going to return true (since the numbers can only be both in arithmetic progression and harmonic progression if they're all equal).

  • You're going to have floating point precision problems. It's safest to check the harmonic progression using only integers, and multiplication instead of division. Use the fact that a, b, cis a harmonic progression if and only if a * b, a * c, b * cis an arithmetic progression. Then check this using only integer arithmetic.

  • The integer divisions in your CheckGPmethod are going to give you positive results, when you have numbers that are not in geometric progression. Integer divisions round down, and you don't want to do that. The best way to check whether a, b, cis a geometric progression is to compare a * cto b * b.

  • 你已经把你的调用CheckHP放在只在CheckAP返回 1 时运行的分支内。所以你只会检查这些数字是否在等差级数中。所以它永远不会返回真(因为如果它们都相等,数字只能在等差级数和调和级数中)。

  • 您将遇到浮点精度问题。仅使用整数和乘法而不是除法来检查谐波级数是最安全的。a, b, c当且仅当a * b, a * c, b * c是等差数列时,才使用调和数列这一事实。然后仅使用整数算术来检查。

  • CheckGP当您的数字不是几何级数时,您的方法中的整数除法将为您带来积极的结果。整数除法四舍五入,你不想这样做。检查是否最好的办法a, b, c是一个几何级数是比较a * cb * b

Incidentally, your various checking functions return 1or -1to mean true or false. Why not just make their return types boolean, and return trueor false?

顺便说一句,您的各种检查函数返回1-1表示真或假。为什么不让他们的返回类型boolean,返回truefalse

Also, you need to think about integer overflow. Think about how large are the numbers you'll be dealing with are. Would it be safer for you to use longor even BigIntegerin place of int?

此外,您需要考虑整数溢出。想想你要处理的数字有多大。您使用long甚至BigInteger代替它会更安全int吗?