mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated long running problem to test smaller dataset
This commit is contained in:
@@ -26,15 +26,15 @@ package com.mattrixwv.project_euler.problems;
|
||||
public class Problem15 extends Problem{
|
||||
//Variables
|
||||
//Static vaiables
|
||||
private static final int WIDTH = 20; //The width of the box to traverse
|
||||
private static final int LENGTH = 20; //The height of the box to traverse
|
||||
protected static int gridWidth = 20; //The width of the box to traverse
|
||||
protected static int gridLength = 20; //The height of the box to traverse
|
||||
//Instance variables
|
||||
private long numOfRoutes; //The number of routes from 0, 0 to 20, 20
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem15(){
|
||||
super(String.format("How many routes from the top left corner to the bottom right corner are there through a %dx%d grid if you can only move right and down?", WIDTH, LENGTH));
|
||||
super(String.format("How many routes from the top left corner to the bottom right corner are there through a %dx%d grid if you can only move right and down?", gridWidth, gridLength));
|
||||
numOfRoutes = 0;
|
||||
}
|
||||
//Operational functions
|
||||
@@ -65,18 +65,18 @@ public class Problem15 extends Problem{
|
||||
//It moves right first, then down
|
||||
private void move(int currentX, int currentY){
|
||||
//Check if you are at the end and act accordingly
|
||||
if((currentX == WIDTH) && (currentY == LENGTH)){
|
||||
if((currentX == gridWidth) && (currentY == gridLength)){
|
||||
++numOfRoutes;
|
||||
return;
|
||||
}
|
||||
|
||||
//Move right if possible
|
||||
if(currentX < WIDTH){
|
||||
if(currentX < gridWidth){
|
||||
move(currentX + 1, currentY);
|
||||
}
|
||||
|
||||
//Move down if possible
|
||||
if(currentY < LENGTH){
|
||||
if(currentY < gridLength){
|
||||
move(currentX, currentY + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import java.util.List;
|
||||
public class Problem23 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final int MAX_NUM = 28123; //The largest number to be checked
|
||||
protected static int maxNum = 28123; //The largest number to be checked
|
||||
//Instance variables
|
||||
private ArrayList<Integer> divisorSums; //This gives the sum of the divisors at subscripts
|
||||
private long sum; //The sum of all the numbers we are looking for
|
||||
@@ -49,9 +49,9 @@ public class Problem23 extends Problem{
|
||||
//Operational functions
|
||||
//Reserve the size of the array to speed up insertion
|
||||
private void reserveArray(){
|
||||
divisorSums.ensureCapacity(MAX_NUM); //It is faster to reserve the appropriate amount of ram now
|
||||
divisorSums.ensureCapacity(maxNum); //It is faster to reserve the appropriate amount of ram now
|
||||
//Make sure every element has a 0 in it's location
|
||||
while(divisorSums.size() <= MAX_NUM){
|
||||
while(divisorSums.size() <= maxNum){
|
||||
divisorSums.add(0);
|
||||
}
|
||||
}
|
||||
@@ -68,7 +68,7 @@ public class Problem23 extends Problem{
|
||||
|
||||
|
||||
//Get the sum of the divisors of all numbers < MAX_NUM
|
||||
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
||||
for(int cnt = 1;cnt < maxNum;++cnt){
|
||||
List<Integer> div = NumberAlgorithms.getDivisors(cnt);
|
||||
//Remove the last element, which is the number itself. This gives us the propper divisors
|
||||
if(div.size() > 1){
|
||||
@@ -86,7 +86,7 @@ public class Problem23 extends Problem{
|
||||
}
|
||||
|
||||
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
||||
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
||||
for(int cnt = 1;cnt < maxNum;++cnt){
|
||||
if(!isSum(abund, cnt)){
|
||||
sum += cnt;
|
||||
}
|
||||
|
||||
@@ -32,15 +32,15 @@ import java.util.List;
|
||||
public class Problem24 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final int NEEDED_PERM = 1000000; //The number of the permutation that you need
|
||||
private static String nums = "0123456789"; //All of the characters that we need to get the permutations of
|
||||
protected static int neededPerm = 1000000; //The number of the permutation that you need
|
||||
protected static String nums = "0123456789"; //All of the characters that we need to get the permutations of
|
||||
//Instance variables
|
||||
private List<String> permutations; //Holds all of the permutations of the string nums
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem24(){
|
||||
super(String.format("What is the millionth lexicographic permutation of the digits %s?", nums));
|
||||
super(String.format("What is the %dth lexicographic permutation of the digits %s?", neededPerm, nums));
|
||||
permutations = new ArrayList<>();
|
||||
}
|
||||
//Operational functions
|
||||
@@ -77,7 +77,7 @@ public class Problem24 extends Problem{
|
||||
@Override
|
||||
public String getResult(){
|
||||
solvedCheck("result");
|
||||
return String.format("The 1 millionth permutation is %s", permutations.get(NEEDED_PERM - 1));
|
||||
return String.format("The %dth permutation is %s", neededPerm, permutations.get(neededPerm - 1));
|
||||
}
|
||||
//Returns an ArrayList with all of the permutations
|
||||
public List<String> getPermutationsList(){
|
||||
@@ -87,7 +87,7 @@ public class Problem24 extends Problem{
|
||||
//Returns the requested permutation
|
||||
public String getPermutation(){
|
||||
solvedCheck("1,000,000th permutation");
|
||||
return permutations.get(NEEDED_PERM - 1);
|
||||
return permutations.get(neededPerm - 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user