diff --git a/pom.xml b/pom.xml index 2641c28..1729b50 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ mattrixwv myClasses - 1.0-SNAPSHOT + 1.0.1 diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java index 5e43da9..4d5430a 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java //Matthew Ellison // Created: 03-04-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //What is the value of the first triangle number to have over five hundred divisors? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems; import java.util.ArrayList; +import java.util.List; import mattrixwv.NumberAlgorithms; @@ -35,7 +36,7 @@ public class Problem12 extends Problem{ //Instance variables private long sum; //The sum of the numbers up to counter private long counter; //The next number to be added to sum - private ArrayList divisors; //Holds the divisors of the triangular number sum + private List divisors; //Holds the divisors of the triangular number sum //Functions //Constructor @@ -43,7 +44,7 @@ public class Problem12 extends Problem{ super(String.format("What is the value of the first triangle number to have over %d divisors?", GOAL_DIVISORS)); sum = 1; counter = 2; - divisors = new ArrayList(); + divisors = new ArrayList<>(); } //Operational functions //Solve the problem @@ -82,6 +83,7 @@ public class Problem12 extends Problem{ solved = true; } //Reset the problem so it can be run again + @Override public void reset(){ super.reset(); sum = 1; @@ -106,7 +108,7 @@ public class Problem12 extends Problem{ return counter - 1; } //Returns the list of divisors of the requested number - public ArrayList getDivisorsOfTriangularNumber(){ + public List getDivisorsOfTriangularNumber(){ solvedCheck("divisors of the triangular number"); return divisors; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java index 3256e39..8ab7449 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java //Matthew Ellison // Created: 03-01-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //The sum of the even Fibonacci numbers less than 4,000,000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -23,7 +23,7 @@ package mattrixwv.ProjectEuler.Problems; -import java.util.ArrayList; +import java.util.List; import mattrixwv.NumberAlgorithms; @@ -55,7 +55,7 @@ public class Problem2 extends Problem{ //Get a list of all fibonacci numbers <= TOP_NUM - ArrayList fibNums = NumberAlgorithms.getAllFib(TOP_NUM); + List fibNums = NumberAlgorithms.getAllFib(TOP_NUM); //Step through every element in the list checking if it is even for(int num : fibNums){ //If the number is even add it to the running tally diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java index 6dc902f..a48b469 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java //Matthew Ellison // Created: 03-18-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //Evaluate the sum of all the amicable numbers under 10000 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -28,6 +28,7 @@ import mattrixwv.NumberAlgorithms; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class Problem21 extends Problem{ @@ -42,8 +43,8 @@ public class Problem21 extends Problem{ //Constructor public Problem21(){ super(String.format("Evaluate the sum of all the amicable numbers under %d", LIMIT)); - divisorSum = new ArrayList(); - amicable = new ArrayList(); + divisorSum = new ArrayList<>(); + amicable = new ArrayList<>(); reserveArray(); } //Operational functions @@ -69,7 +70,7 @@ public class Problem21 extends Problem{ //Generate the divisors of all numbers < 10000, get their sum, and add it to the list for(int cnt = 1;cnt < LIMIT;++cnt){ - ArrayList divisors = NumberAlgorithms.getDivisors(cnt); //Get all the divisors of a number + List divisors = NumberAlgorithms.getDivisors(cnt); //Get all the divisors of a number if(divisors.size() > 1){ divisors.remove(divisors.get(divisors.size() - 1)); //Remove the last entry because it will be the number itself } @@ -116,16 +117,16 @@ public class Problem21 extends Problem{ @Override public String getResult(){ solvedCheck("result"); - StringBuilder result = new StringBuilder(String.format("All amicable numbers less than %d are\n", LIMIT)); + StringBuilder result = new StringBuilder(String.format("All amicable numbers less than %d are%n", LIMIT)); for(int cnt = 0;cnt < amicable.size();++cnt){ - result.append(String.format("%d\n", amicable.get(cnt))); + result.append(String.format("%d%n", amicable.get(cnt))); } result.append(String.format("The sum of all of these amicable numbers is %d", ArrayAlgorithms.getSum(amicable))); return result.toString(); } //Returns a vector with all of the amicable numbers calculated - public ArrayList getAmicable(){ + public List getAmicable(){ solvedCheck("amicable numbers"); return amicable; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java index 8f57b87..7438161 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java //Matthew Ellison // Created: 03-22-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -27,6 +27,7 @@ import mattrixwv.ArrayAlgorithms; import mattrixwv.NumberAlgorithms; import java.util.ArrayList; +import java.util.List; public class Problem23 extends Problem{ @@ -41,7 +42,7 @@ public class Problem23 extends Problem{ //Constructor public Problem23(){ super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers"); - divisorSums = new ArrayList(); + divisorSums = new ArrayList<>(); reserveArray(); sum = 0; } @@ -68,7 +69,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){ - ArrayList div = NumberAlgorithms.getDivisors(cnt); + List div = NumberAlgorithms.getDivisors(cnt); //Remove the last element, which is the number itself. This gives us the propper divisors if(div.size() > 1){ div.remove(div.size() - 1); @@ -77,7 +78,7 @@ public class Problem23 extends Problem{ } //Get the abundant numbers - ArrayList abund = new ArrayList(); + ArrayList abund = new ArrayList<>(); for(int cnt = 0;cnt < divisorSums.size();++cnt){ if(divisorSums.get(cnt) > cnt){ abund.add(cnt); @@ -100,16 +101,16 @@ public class Problem23 extends Problem{ } //A function that returns true if num can be created by adding two elements from abund and false if it cannot private boolean isSum(final ArrayList abund, int num){ - int sum = 0; + int tempSum = 0; //Pick a number for the first part of the sum for(int firstNum = 0;firstNum < abund.size();++firstNum){ //Pick a number for the second part of the sum for(int secondNum = firstNum;secondNum < abund.size();++secondNum){ - sum = abund.get(firstNum) + abund.get(secondNum); - if(sum == num){ + tempSum = abund.get(firstNum) + abund.get(secondNum); + if(tempSum == num){ return true; } - else if(sum > num){ + else if(tempSum > num){ break; } } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java index 3671a2e..b72e9a6 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java //Matthew Ellison // Created: 03-24-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -26,6 +26,7 @@ package mattrixwv.ProjectEuler.Problems; import mattrixwv.StringAlgorithms; import java.util.ArrayList; +import java.util.List; public class Problem24 extends Problem{ @@ -34,13 +35,13 @@ public class Problem24 extends Problem{ 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 //Instance variables - private ArrayList permutations; //Holds all of the permutations of the string nums + private List 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)); - permutations = new ArrayList(); + permutations = new ArrayList<>(); } //Operational functions //Solve the problems @@ -79,7 +80,7 @@ public class Problem24 extends Problem{ return String.format("The 1 millionth permutation is %s", permutations.get(NEEDED_PERM - 1)); } //Returns an ArrayList with all of the permutations - public ArrayList getPermutationsList(){ + public List getPermutationsList(){ solvedCheck("permutations"); return permutations; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java index 89b3066..4a1561d 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java //Matthew Ellison // Created: 03-01-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //The largest prime factor of 600851475143 //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems; import java.util.ArrayList; +import java.util.List; import mattrixwv.NumberAlgorithms; import mattrixwv.exceptions.InvalidResult; @@ -34,13 +35,13 @@ public class Problem3 extends Problem{ //Static variables private static final long GOAL_NUMBER = 600851475143L; //The number that needs factored //Instance variables - private ArrayList factors; //Holds the factors of goalNumber + private List factors; //Holds the factors of goalNumber //Functions //Constructor public Problem3(){ super(String.format("What is the largest prime factor of %d?", GOAL_NUMBER)); - factors = new ArrayList(); + factors = new ArrayList<>(); } //Operational functions //Solve the problem @@ -80,7 +81,7 @@ public class Problem3 extends Problem{ return String.format("The largest factor of the number %d is %d", GOAL_NUMBER, factors.get(factors.size() - 1)); } //Returns the list of factors of the number - public ArrayList getFactors(){ + public List getFactors(){ solvedCheck("factors"); return factors; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java index a69e10a..612b543 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java //Matthew Ellison // Created: 06-05-21 -//Modified: 07-03-21 +//Modified: 07-30-22 //How many circular primes are there below one million? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems; import java.util.ArrayList; +import java.util.List; import mattrixwv.NumberAlgorithms; @@ -33,12 +34,12 @@ public class Problem35 extends Problem{ //Static variables private static final int MAX_NUM = 999999; //The largest number that we are checking for primes //Instance variables - private ArrayList primes; //The primes below MAX_NUM + private List primes; //The primes below MAX_NUM private ArrayList circularPrimes; //The circular primes below MAX_NUM //Functions //Returns a list of all rotations of a string passed to it private ArrayList getRotations(String str){ - ArrayList rotations = new ArrayList(); + ArrayList rotations = new ArrayList<>(); rotations.add(str); for(int cnt = 1;cnt < str.length();++cnt){ str = str.substring(1) + str.substring(0, 1); @@ -49,8 +50,8 @@ public class Problem35 extends Problem{ //Constructor public Problem35(){ super("How many circular primes are there below one million?"); - primes = new ArrayList(); - circularPrimes = new ArrayList(); + primes = new ArrayList<>(); + circularPrimes = new ArrayList<>(); } //Operational functions //Solve the problem @@ -72,7 +73,7 @@ public class Problem35 extends Problem{ //Get all of the rotations of the prime and see if they are also prime ArrayList rotations = getRotations(Integer.toString(prime)); for(String rotation : rotations){ - int p = Integer.valueOf(rotation); + int p = Integer.parseInt(rotation); if(!primes.contains(p)){ allRotationsPrime = false; break; @@ -92,6 +93,7 @@ public class Problem35 extends Problem{ solved = true; } //Reset the problem so it can be run again + @Override public void reset(){ super.reset(); primes.clear(); @@ -104,12 +106,12 @@ public class Problem35 extends Problem{ return String.format("The number of all circular prime numbers under %d is %d", MAX_NUM, circularPrimes.size()); } //Returns the ArrayList of primes < MAX_NUM - public ArrayList getPrimes(){ + public List getPrimes(){ solvedCheck("list of primes"); return primes; } //Returns the ArrayList of circular primes < MAX_NUM - public ArrayList getCircularPrimes(){ + public List getCircularPrimes(){ solvedCheck("list of circular primes"); return circularPrimes; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java index 52f3e67..7bda97b 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java @@ -1,11 +1,11 @@ //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java //Matthew Ellison // Created: 03-01-19 -//Modified: 07-03-21 +//Modified: 07-30-22 //What is the 10001th prime number? //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses /* - Copyright (C) 2021 Matthew Ellison + 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 @@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems; import java.util.ArrayList; +import java.util.List; import mattrixwv.NumberAlgorithms; @@ -33,13 +34,13 @@ public class Problem7 extends Problem{ //Static variables private static final long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get //Instance variables - private ArrayList primes; + private List primes; //Functions //Constructor public Problem7(){ super(String.format("What is the %dth prime number?", NUMBER_OF_PRIMES)); - primes = new ArrayList(); + primes = new ArrayList<>(); } //Operational functions //Solve the problem