Updated for performance

This commit is contained in:
2020-06-17 23:19:26 -04:00
parent 64f43e0a4d
commit bac055495e

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem29.java
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java
//Matthew Ellison
// Created: 10-09-19
//Modified: 10-09-19
//Modified: 06-17-20
//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
Copyright (C) 2020 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
@@ -29,13 +29,13 @@ import java.math.BigInteger;
public class Problem29 extends Problem{
//The lowest possible value for a
private static final Integer BOTTOM_A = 2;
private static final int BOTTOM_A = 2;
//The highest possible value for a
private static final Integer TOP_A = 100;
private static final int TOP_A = 100;
//The lowest possible value for b
private static final Integer BOTTOM_B = 2;
private static final int BOTTOM_B = 2;
//The highest possible value for b
private static final Integer TOP_B = 100;
private static final int TOP_B = 100;
//Holds all unique values generated
private ArrayList<BigInteger> unique;
@@ -46,13 +46,13 @@ public class Problem29 extends Problem{
//Setup the variables
unique = new ArrayList<BigInteger>();
//Start the time
//Start the timer
timer.start();
//Start with the first A and move towards the top
for(Integer currentA = BOTTOM_A;currentA <= TOP_A;++currentA){
for(int 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){
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
@@ -72,5 +72,5 @@ public class Problem29 extends Problem{
/* Results:
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
It took 125.133 milliseconds to solve this problem.
It took 111.346 milliseconds to solve this problem.
*/