简体   繁体   中英

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

I am new to Java programming and have tried a few problems on Project Euler. I somehow came up with my own problem of printing sequence of exponents of 3 and 5 and limit the result to below 1000. I have researched for 3 days to find the best approach to this problem but I could not find relevant articles. I have come across algorithms on exponential series but those were too advanced for my capability right now.

I would appreciate any help in solving this problem. Please see the code I have tried

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));

               }

           }

       }

   } 

}

This code prints out the following result:

3.0
5.0
9.0
25.0
27.0
125.0
81.0
625.0

My problem is that it is very apparent that I am forcing the exponent to print below 1000 by limiting the base and exponent value inside the loop

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

I would like to somehow limit the result to below 1000 so not sure if this declaration is apt

int result = 1000; // result variable as 1000

Also, I want the code to print the output in alternates of 3 and 5 as shown below. My program prints the output in sequence of 3 and 5 respectively.

Desired output:

3.0
5.0
9.0
27.0
125.0
81.0
625.0
243.0
729.0

And stops there because the next value would exceed 1000.

I also wanted to know if there is any other approach instead of using Math.pow() method because it returns a double instead of an int. I would like to avoid the double value and just print as follows:

Without double:

3
5
9
27
81
125
243
625
729

Without using Math.pow() (and printing in different order ):

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

Why not check if the result is bigger than 1000, and just break out of the loop if it is?

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

Hint:

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

and 3 x is always smaller than 5 x . So a single loop (for the x part) with two computations in the body of the loop one for 3 and one for 5 should do the job. You just have to use some condition for the less than 1000 part to avoid printing 5 5 and 5 6 .

First create a double to store the result:

double result = 0;

Then create an infinite while loop which calculates the result using 3 and 5 and breaks out once result is above 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++;
}

Since exponents of 3 are smaller than 5 don't break out until the maximum exponent in 3 is hit. Since the break does not occur don't print the 5 unless it is valid.

Also to print the double as an int value just cast it to an int.

EDIT:

If you are really worried about efficiency here is a faster solution:

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;
    }
}

You could also make the 3 and 5 arguments to take it one step further.

You could use a single loop, checking exponents for both 3 and 5 on each iteration, and printing each result that is less than 1000.

You just want to make sure that you break the loop once your 3's exceed 1000.

To print integer values, you can simply cast the result of Math.pow() to an int.

There are many different ways that one could write an algorithm like this. Here's a very simple (untested) example:

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);
            }
        }
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM