Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Not Working

843810Oct 26 2006 — edited Nov 28 2006
Okay, I am trying to solve an answer to a question. The question is

What is the smallest integer x >2006, such that x^7 and x terminate in the same five digits?

I came up with this program for some reason its not working. I think I am getting a problem with the long long npow. I am not reall sure. When I put in the result of this program in my calculator, I get a diffrent answer. I mean, when I put x^7 and x in my calcultor, the last 5 digits are no the same.
import java.lang.Math; 
 
public class Number21
{
	public static void main (String[] args)
	{
		int result = Solve21(2006);
		System.out.println("Number21 is: " + result);
		System.out.println("Number21 rised to 7th power is: " + Math.pow(result,7));
	}
	
	public static int Solve21(int x)
    {

		int ANSWER =0;
		boolean found = false;
		int n = x;
		
		while (!found)
		{
		
		long npow = (long)Math.pow(n,7);
		String nStr = String.valueOf(n);
		String npowStr = String.valueOf(npow);
		
		if (n <= 9999)
		{
			nStr = "0"+nStr;
		}
		
		int nStrLength = nStr.length();
		int npowStrLength = npowStr.length();
		
		String nStrSub = nStr.substring((nStrLength-5),(nStrLength-1));
		String npowStrSub = npowStr.substring((npowStrLength-5),(npowStrLength-1));
		
		
		if (nStrSub.equals(npowStrSub))
		{
			ANSWER = n;
			found = true;
		}

		n++;
			
		}
		
		return ANSWER;

    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2006
Added on Oct 26 2006
4 comments
87 views