So I'm creating a program that calculates the “Sum of Powers” after the user inputs two integers N and M. The sum of powers is computed as N^M+ N^(M-1)+ N^(M-2)….+N^0.

But I don't know how to finish it. I'm kinda confused using return method, loops, etc.
Here's what I have so far:

import java.util.Scanner;

public class SumOfPow
{
	public static int exponent(int n,int m)
	{
		int nProd = 1;
		// COMPLETE THE CODE THAT MAKES THE FUNCTION RETURN n^m

		return nProd; 
	}

	public static void main(String args[])
	{
		int N, M;

		int Power = 0;

		// Complete the code with the necessary statements. Make use of the method “exponent”

		System.out.println ("Sum of Powers: "+Power);			
	}

I know what I'm supposed to do, but I can't code it. I got the logic down, but I don't know how to code it.

public class SumOfPow
{
	public static int exponent(int n,int m)
	{
		int nProd = n;
		// COMPLETE THE CODE THAT MAKES THE FUNCTION RETURN n^m
                for(int i=1;i<m;i++)
                    nProd=nProd*n;

		return nProd; 
	}

	public static void main(String args[])
	{
		int N=7, M=4;

		int Power = 0;

		// Complete the code with the necessary statements. Make use of the method “exponent”
                Power=exponent(N,M);

		System.out.println ("Sum of Powers: "+Power);			
	}
}

Another easy way to do is by using builtin method...

public class SumOfPow
{
    	public static void main(String args[])
	{
		double N=7, M=4;

		double Power = 0;

		// Complete the code with the necessary statements. Make use of the method “exponent”
                Power=Math.pow(N,M);

		System.out.println ("Sum of Powers: "+Power);			
	}
}

To do N^M+ N^(M-1)+ N^(M-2)…. series using builtin method

public class SumOfPow
{
    	public static void main(String args[])
	{
		double N=2, M=3;

		double Power = 0;
                int next=0;
                int series=2;

                for(int i=0;i<series;i++){
                Power=Power+Math.pow(N,(M-next));
                next=next+1;
                }

		System.out.println ("Sum of Powers: "+Power);			
	}
}

Let's think this one through:

You want to compute the value of N^M + N^(M-1) + N^(M-2) + … + N^2 + N^1 + N^0 .

This suggest computing a total as some value varies from M down to zero. Have you computed a total with a loop before?

Consider this: Mathematically, N^0 + N^1 + N^2 + … + N^(M-2) + N^(M-1) + N^M produces the same result. So you could loop from zero to M . It might be easier for you to loop that way.

As pointed out above, Java provides the Math.pow method to do exponents. But consider this: If you loop up from zero to M instead of down from M to zero you might think of a simpler and faster way to compute the exponents of N . ;->

I believe the best way to get what you want is to use the Math.pow(double, double) method, but make sure that the exponent is added by one and the expression should also be added to one.
For instance if you wanted to calculate the sum of powers for 2^3 you would call the method like this:

Math.pow(2,4) + 1

.

Mr Chips' idea works well when N=2.
Try this code:

public class Powers {
	public static void main(String[] args) {
		for (int N = 2; N < 5; ++N) {
			for (int M = 1; M < 5; ++M) {

				System.out.println("Computing sum for N=" + N + ", M=" + M);

				int sum = 0;
				for (int i = M; i >= 0; --i) {
					int value = (int) Math.pow(N, i);
					System.out.println(N + "^" + i + " = " + value);
					sum = sum + value;
				}

				System.out.println("Sum of Powers for N=" + N + ", M=" + M
						+ "; resulting sum = " + sum);
				int quickGuess = (int) (Math.pow(N, M + 1) - 1);
				if (quickGuess != sum)
					System.out.println("   This is not " + quickGuess);
				System.out.println();
			}
		}
	}
}

Just out of interest, here is a recursive method ( which I don't claim to have rigorously tested by any means)

public static int sumPowers( int n, int m )
{    
    if( m > 0 )
    {
     int sum = n;
     sum *= sumPowers( n, --m );
     
     return sum+1;
    }
  
  return 1; 
          
}

I'd suggest a recursive method, something like

public int sumPower(int n, int m) {
    return (m > 0) ? (int)Math.pow(n, m) + sumPower(n, m-1) : 1;
}

Edit: Just saw there was a recursive method proposed already. Oh well, they differ a little bit I guess.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.