//ProjectEuler/Java/Problem29.java //Matthew Ellison // Created: 10-09-19 //Modified: 10-09-19 //How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* Copyright (C) 2019 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ import mattrixwv.Stopwatch; import java.util.ArrayList; import java.math.BigInteger; public class Problem29{ private static final Integer BOTTOM_A = 2; //The lowest possible value for a private static final Integer TOP_A = 100; //The highest possible value for a private static final Integer BOTTOM_B = 2; //The lowest possible value for b private static final Integer TOP_B = 100; //The highest possible value for b private static ArrayList unique; //Holds all unique values generated public static void main(String[] args){ //Setup the variables Stopwatch timer = new Stopwatch(); unique = new ArrayList(); //Start the time timer.start(); //Start with the first A and move towards the top for(Integer currentA = BOTTOM_A;currentA <= TOP_A;++currentA){ //Start with the first B and move towards the top for(Integer currentB = BOTTOM_B;currentB <= TOP_B;++currentB){ //Get the new number BigInteger currentNum = BigInteger.valueOf(currentA).pow(currentB); //If the current number is not in the array add it if(!unique.contains(currentNum)){ unique.add(currentNum); } } } //Stop the timer timer.stop(); //Print the results System.out.printf("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d\n", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.size()); System.out.println("It took " + timer.getStr() + " to run this algorithm"); } } /* Results: The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 It took 258.922 milliseconds to run this algorithm */