Projecteuler.net – Problem 63 – Java

http://projecteuler.net/problem=63

This one seemed straight forward and doable with very little code. I like that. Simply solve the math then compare the length of the result to power value.

Josh


import java.math.BigInteger;

public class Euler63 {

	/**
	 * The 5-digit number, 16807=7^5, is also a fifth power. 
	 * Similarly, the 9-digit number, 134217728=8^9, is a ninth power.
	 * How many n-digit positive integers exist which are also an nth power?
	 */
	public static void main(String[] args) {

		int numberOfSolutions = 0;
		
		for (Integer number = 1; number <= 100; number++) {
			for (int power = 1; power <= 100; power++) {
				
				BigInteger value = new BigInteger(number.toString()).pow(power);
				int length = value.toString().length();
				
				if (power == length) {
					numberOfSolutions++;
				}
			}
		}
		
		System.out.println(numberOfSolutions);					
		
	}

}

This entry was posted in Java, Project Euler. Bookmark the permalink.

Leave a Reply