Java 打印数字 3 和 5 的指数级数并将结果限制在 1000 以下的程序

Java program to print exponential series for numbers 3 and 5 and limit the result to below 1000

我是 Java 编程新手,在 Project Euler 上尝试过一些问题。我不知何故想出了自己的打印 3 和 5 指数序列的问题,并将结果限制在 1000 以下。我研究了 3 天以找到解决此问题的最佳方法,但我找不到相关文章。我遇到过指数级数的算法,但这些算法对于我现在的能力来说太先进了。

对于解决此问题的任何帮助,我将不胜感激。请看我试过的代码

public class Exponent {        

  public static void main (String[] args) {

    // Declared integers for base and exponent

       int i = 0;  /* for base */      
       int n = 0;  /* for exponent */

       for (n=1; n<5; n++) {

           for (i=1; i<=5; i++) {

               if (i%3 == 0 || i%5 == 0) {

                    System.out.println(Math.pow(i,n));

               }

           }

       }

   } 

}

此代码打印出以下结果:

3.0
5.0
9.0
25.0
27.0
125.0
81.0
625.0

我的问题是,很明显我通过限制循环内的底数和指数值来强制指数打印低于 1000

for (n=1; n<5; n++) //because n<=5 would print result for 5 power 5 which is 3125

我想以某种方式将结果限制在 1000 以下,所以不确定此声明是否合适

int result = 1000; // result variable as 1000

此外,我希望代码以 3 和 5 的交替方式打印输出,如下所示。我的程序分别按 3 和 5 的顺序打印输出。

Desired output:

3.0
5.0
9.0
27.0
125.0
81.0
625.0
243.0
729.0

然后停在那里,因为下一个值将超过 1000。

我也想知道是否有任何其他方法而不是使用 Math.pow() 方法,因为它 returns 是双精度而不是整数。我想避免 double 值并按如下方式打印:

Without double:

3
5
9
27
81
125
243
625
729

为什么不检查结果是否大于 1000,如果是就跳出循环?

if(Math.pow(i,n)>=1000)
break;

不使用 Math.pow()(并以不同的顺序打印):

  int[] bases = { 3, 5 };
  long maxval = 1000L;
  for (int base : bases) {
     long value = base;
     do {
        System.out.println( value );
        value *= base;
     } while (value < maxval);
  }

提示:

3.0   = 3^1
5.0   = 5^1
9.0   = 3^2
25.0  = 5^2 // I assume you forgot it
27.0  = 3^3
125.0 = 5^3
81.0  = 3^4
625.0 = 5^4
243.0 = 3^5
729.0 = 3^6

和3x总是小于5x。因此,一个循环(对于 x 部分)在循环主体中有两个计算,一个用于 3,一个用于 5 应该可以完成这项工作。您只需要为小于 1000 的部分使用一些条件,以避免打印 55 和 56.

首先创建一个double来存储结果:

double result = 0;

然后创建一个无限循环,使用 3 和 5 计算结果,并在结果超过 1000 时中断。

while(true)
{
    result = Math.pow(3, n);
    if(result > 1000)
    {
        break;
    }
    System.out.println(((int)result));
    result = Math.pow(5, n);
    if(result < 1000)
    {
        System.out.println((int)result);
    }          
    n++;
}

由于 3 的指数小于 5,因此在达到 3 的最大指数之前不会爆发。由于没有发生中断,除非有效,否则不要打印 5。

还要将 double 打印为 int 值,只需将其转换为 int。

编辑:

如果您真的很担心效率,这里有一个更快的解决方案:

public void calcExponents(int max)
{
    int resultThree = 3;
    int resultFive = 5;

    while(resultThree < max)
    {
        System.out.println(resultThree);
        if(resultFive < max)
        {
            System.out.println(resultFive);
        }

        resultThree *= 3;
        resultFive *= 5;
    }
}

您还可以使第 3 个和第 5 个参数更进一步。

您可以使用单个循环,在每次迭代中检查 3 和 5 的指数,并打印每个小于 1000 的结果。

您只想确保在您的 3 超过 1000 时打破循环。

要打印整数值,您只需将 Math.pow() 的结果转换为 int。

有许多不同的方法可以编写这样的算法。这是一个非常简单(未经测试)的示例:

public class Exponent {        

    public static void main (String[] args) {

        int i = 1; // or start at 0 if you prefer
        // set the max value (could also be parsed from args)
        int maxValue = 1000; 

        // the break condition also increments:
        while (Math.pow(3, i++) < maxValue) { 
            int x3 = (int) Math.pow(3, i);
            int x5 = (int) Math.pow(5, i);

            if (x3 < maxValue) {
                System.out.println(x3);
            } 

            if (x5 < maxValue) {
                System.out.println(x5);
            }
        }
    } 
}