mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
126 lines
3.9 KiB
Java
126 lines
3.9 KiB
Java
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java
|
|
//Matthew Ellison
|
|
// Created: 10-09-19
|
|
//Modified: 08-20-22
|
|
//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) 2022 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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
package com.mattrixwv.project_euler.problems;
|
|
|
|
|
|
import java.math.BigInteger;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
|
|
public class Problem29 extends Problem{
|
|
//Variables
|
|
//Static variables
|
|
private static final int BOTTOM_A = 2; //The lowest possible value for a
|
|
private static final int TOP_A = 100; //The highest possible value for a
|
|
private static final int BOTTOM_B = 2; //The lowest possible value for b
|
|
private static final int TOP_B = 100; //The highest possible value for b
|
|
//Instance variables
|
|
private ArrayList<BigInteger> unique; //Holds all unique values generated
|
|
|
|
//Functions
|
|
//Constructor
|
|
public Problem29(){
|
|
super(String.format("How many distinct terms are in the sequence generated by a^b for %d <= a <= %d and %d <= b <= %d?", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B));
|
|
unique = new ArrayList<>();
|
|
}
|
|
//Operational functions
|
|
//Solve the problem
|
|
@Override
|
|
public void solve(){
|
|
//If the problem has already been solved do nothing and end the function
|
|
if(solved){
|
|
return;
|
|
}
|
|
|
|
//Start the timer
|
|
timer.start();
|
|
|
|
|
|
//Start with the first A and move towards the top
|
|
for(int currentA = BOTTOM_A;currentA <= TOP_A;++currentA){
|
|
//Start with the first B and move towards the top
|
|
for(int 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();
|
|
|
|
//Throw a flag to show the problem is solved
|
|
solved = true;
|
|
}
|
|
//Reset the problem so it can be run again
|
|
@Override
|
|
public void reset(){
|
|
super.reset();
|
|
unique.clear();
|
|
}
|
|
//Gets
|
|
//Returns the result of solving the problem
|
|
@Override
|
|
public String getResult(){
|
|
solvedCheck("result");
|
|
return String.format("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.size());
|
|
}
|
|
//Returns the lowest possible value for a
|
|
public static int getBottomA(){
|
|
return BOTTOM_A;
|
|
}
|
|
//Returns the highest possible value for a
|
|
public static int getTopA(){
|
|
return TOP_A;
|
|
}
|
|
//Returns the lowest possible value for b
|
|
public static int getBottomB(){
|
|
return BOTTOM_B;
|
|
}
|
|
//Returns the highest possible value for b
|
|
public static int getTopB(){
|
|
return TOP_B;
|
|
}
|
|
//Returns a vector of all the unique values for a^b
|
|
public List<BigInteger> getUnique(){
|
|
solvedCheck("unique values for a^b");
|
|
return unique;
|
|
}
|
|
//Returns the number of unique values for a^b
|
|
public int getNumUnique(){
|
|
solvedCheck("number of unique values for a^b");
|
|
return unique.size();
|
|
}
|
|
}
|
|
|
|
|
|
/* Results:
|
|
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
|
|
It took an average of 118.773 milliseconds to run this problem through 100 iterations
|
|
*/
|