mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated classes plugin
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -27,7 +27,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>mattrixwv</groupId>
|
<groupId>mattrixwv</groupId>
|
||||||
<artifactId>myClasses</artifactId>
|
<artifactId>myClasses</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// 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?
|
//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
|
//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
|
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
|
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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import mattrixwv.NumberAlgorithms;
|
import mattrixwv.NumberAlgorithms;
|
||||||
|
|
||||||
@@ -35,7 +36,7 @@ public class Problem12 extends Problem{
|
|||||||
//Instance variables
|
//Instance variables
|
||||||
private long sum; //The sum of the numbers up to counter
|
private long sum; //The sum of the numbers up to counter
|
||||||
private long counter; //The next number to be added to sum
|
private long counter; //The next number to be added to sum
|
||||||
private ArrayList<Long> divisors; //Holds the divisors of the triangular number sum
|
private List<Long> divisors; //Holds the divisors of the triangular number sum
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//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));
|
super(String.format("What is the value of the first triangle number to have over %d divisors?", GOAL_DIVISORS));
|
||||||
sum = 1;
|
sum = 1;
|
||||||
counter = 2;
|
counter = 2;
|
||||||
divisors = new ArrayList<Long>();
|
divisors = new ArrayList<>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
@@ -82,6 +83,7 @@ public class Problem12 extends Problem{
|
|||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
sum = 1;
|
sum = 1;
|
||||||
@@ -106,7 +108,7 @@ public class Problem12 extends Problem{
|
|||||||
return counter - 1;
|
return counter - 1;
|
||||||
}
|
}
|
||||||
//Returns the list of divisors of the requested number
|
//Returns the list of divisors of the requested number
|
||||||
public ArrayList<Long> getDivisorsOfTriangularNumber(){
|
public List<Long> getDivisorsOfTriangularNumber(){
|
||||||
solvedCheck("divisors of the triangular number");
|
solvedCheck("divisors of the triangular number");
|
||||||
return divisors;
|
return divisors;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 07-03-21
|
//Modified: 07-30-22
|
||||||
//The sum of the even Fibonacci numbers less than 4,000,000
|
//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
|
//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
|
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
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.List;
|
||||||
|
|
||||||
import mattrixwv.NumberAlgorithms;
|
import mattrixwv.NumberAlgorithms;
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ public class Problem2 extends Problem{
|
|||||||
|
|
||||||
|
|
||||||
//Get a list of all fibonacci numbers <= TOP_NUM
|
//Get a list of all fibonacci numbers <= TOP_NUM
|
||||||
ArrayList<Integer> fibNums = NumberAlgorithms.getAllFib(TOP_NUM);
|
List<Integer> fibNums = NumberAlgorithms.getAllFib(TOP_NUM);
|
||||||
//Step through every element in the list checking if it is even
|
//Step through every element in the list checking if it is even
|
||||||
for(int num : fibNums){
|
for(int num : fibNums){
|
||||||
//If the number is even add it to the running tally
|
//If the number is even add it to the running tally
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-18-19
|
// Created: 03-18-19
|
||||||
//Modified: 07-03-21
|
//Modified: 07-30-22
|
||||||
//Evaluate the sum of all the amicable numbers under 10000
|
//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
|
//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
|
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
|
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.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class Problem21 extends Problem{
|
public class Problem21 extends Problem{
|
||||||
@@ -42,8 +43,8 @@ public class Problem21 extends Problem{
|
|||||||
//Constructor
|
//Constructor
|
||||||
public Problem21(){
|
public Problem21(){
|
||||||
super(String.format("Evaluate the sum of all the amicable numbers under %d", LIMIT));
|
super(String.format("Evaluate the sum of all the amicable numbers under %d", LIMIT));
|
||||||
divisorSum = new ArrayList<Integer>();
|
divisorSum = new ArrayList<>();
|
||||||
amicable = new ArrayList<Integer>();
|
amicable = new ArrayList<>();
|
||||||
reserveArray();
|
reserveArray();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//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
|
//Generate the divisors of all numbers < 10000, get their sum, and add it to the list
|
||||||
for(int cnt = 1;cnt < LIMIT;++cnt){
|
for(int cnt = 1;cnt < LIMIT;++cnt){
|
||||||
ArrayList<Integer> divisors = NumberAlgorithms.getDivisors(cnt); //Get all the divisors of a number
|
List<Integer> divisors = NumberAlgorithms.getDivisors(cnt); //Get all the divisors of a number
|
||||||
if(divisors.size() > 1){
|
if(divisors.size() > 1){
|
||||||
divisors.remove(divisors.get(divisors.size() - 1)); //Remove the last entry because it will be the number itself
|
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
|
@Override
|
||||||
public String getResult(){
|
public String getResult(){
|
||||||
solvedCheck("result");
|
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){
|
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)));
|
result.append(String.format("The sum of all of these amicable numbers is %d", ArrayAlgorithms.getSum(amicable)));
|
||||||
|
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
//Returns a vector with all of the amicable numbers calculated
|
//Returns a vector with all of the amicable numbers calculated
|
||||||
public ArrayList<Integer> getAmicable(){
|
public List<Integer> getAmicable(){
|
||||||
solvedCheck("amicable numbers");
|
solvedCheck("amicable numbers");
|
||||||
return amicable;
|
return amicable;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-22-19
|
// 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
|
//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
|
//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
|
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
|
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 mattrixwv.NumberAlgorithms;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class Problem23 extends Problem{
|
public class Problem23 extends Problem{
|
||||||
@@ -41,7 +42,7 @@ public class Problem23 extends Problem{
|
|||||||
//Constructor
|
//Constructor
|
||||||
public Problem23(){
|
public Problem23(){
|
||||||
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
|
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
|
||||||
divisorSums = new ArrayList<Integer>();
|
divisorSums = new ArrayList<>();
|
||||||
reserveArray();
|
reserveArray();
|
||||||
sum = 0;
|
sum = 0;
|
||||||
}
|
}
|
||||||
@@ -68,7 +69,7 @@ public class Problem23 extends Problem{
|
|||||||
|
|
||||||
//Get the sum of the divisors of all numbers < MAX_NUM
|
//Get the sum of the divisors of all numbers < MAX_NUM
|
||||||
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
||||||
ArrayList<Integer> div = NumberAlgorithms.getDivisors(cnt);
|
List<Integer> div = NumberAlgorithms.getDivisors(cnt);
|
||||||
//Remove the last element, which is the number itself. This gives us the propper divisors
|
//Remove the last element, which is the number itself. This gives us the propper divisors
|
||||||
if(div.size() > 1){
|
if(div.size() > 1){
|
||||||
div.remove(div.size() - 1);
|
div.remove(div.size() - 1);
|
||||||
@@ -77,7 +78,7 @@ public class Problem23 extends Problem{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Get the abundant numbers
|
//Get the abundant numbers
|
||||||
ArrayList<Integer> abund = new ArrayList<Integer>();
|
ArrayList<Integer> abund = new ArrayList<>();
|
||||||
for(int cnt = 0;cnt < divisorSums.size();++cnt){
|
for(int cnt = 0;cnt < divisorSums.size();++cnt){
|
||||||
if(divisorSums.get(cnt) > cnt){
|
if(divisorSums.get(cnt) > cnt){
|
||||||
abund.add(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
|
//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<Integer> abund, int num){
|
private boolean isSum(final ArrayList<Integer> abund, int num){
|
||||||
int sum = 0;
|
int tempSum = 0;
|
||||||
//Pick a number for the first part of the sum
|
//Pick a number for the first part of the sum
|
||||||
for(int firstNum = 0;firstNum < abund.size();++firstNum){
|
for(int firstNum = 0;firstNum < abund.size();++firstNum){
|
||||||
//Pick a number for the second part of the sum
|
//Pick a number for the second part of the sum
|
||||||
for(int secondNum = firstNum;secondNum < abund.size();++secondNum){
|
for(int secondNum = firstNum;secondNum < abund.size();++secondNum){
|
||||||
sum = abund.get(firstNum) + abund.get(secondNum);
|
tempSum = abund.get(firstNum) + abund.get(secondNum);
|
||||||
if(sum == num){
|
if(tempSum == num){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if(sum > num){
|
else if(tempSum > num){
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-24-19
|
// 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?
|
//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
|
//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
|
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
|
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 mattrixwv.StringAlgorithms;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public class Problem24 extends Problem{
|
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 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
|
private static String nums = "0123456789"; //All of the characters that we need to get the permutations of
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private ArrayList<String> permutations; //Holds all of the permutations of the string nums
|
private List<String> permutations; //Holds all of the permutations of the string nums
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem24(){
|
public Problem24(){
|
||||||
super(String.format("What is the millionth lexicographic permutation of the digits %s?", nums));
|
super(String.format("What is the millionth lexicographic permutation of the digits %s?", nums));
|
||||||
permutations = new ArrayList<String>();
|
permutations = new ArrayList<>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problems
|
//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));
|
return String.format("The 1 millionth permutation is %s", permutations.get(NEEDED_PERM - 1));
|
||||||
}
|
}
|
||||||
//Returns an ArrayList with all of the permutations
|
//Returns an ArrayList with all of the permutations
|
||||||
public ArrayList<String> getPermutationsList(){
|
public List<String> getPermutationsList(){
|
||||||
solvedCheck("permutations");
|
solvedCheck("permutations");
|
||||||
return permutations;
|
return permutations;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 07-03-21
|
//Modified: 07-30-22
|
||||||
//The largest prime factor of 600851475143
|
//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
|
//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
|
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
|
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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import mattrixwv.NumberAlgorithms;
|
import mattrixwv.NumberAlgorithms;
|
||||||
import mattrixwv.exceptions.InvalidResult;
|
import mattrixwv.exceptions.InvalidResult;
|
||||||
@@ -34,13 +35,13 @@ public class Problem3 extends Problem{
|
|||||||
//Static variables
|
//Static variables
|
||||||
private static final long GOAL_NUMBER = 600851475143L; //The number that needs factored
|
private static final long GOAL_NUMBER = 600851475143L; //The number that needs factored
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private ArrayList<Long> factors; //Holds the factors of goalNumber
|
private List<Long> factors; //Holds the factors of goalNumber
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem3(){
|
public Problem3(){
|
||||||
super(String.format("What is the largest prime factor of %d?", GOAL_NUMBER));
|
super(String.format("What is the largest prime factor of %d?", GOAL_NUMBER));
|
||||||
factors = new ArrayList<Long>();
|
factors = new ArrayList<>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//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));
|
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
|
//Returns the list of factors of the number
|
||||||
public ArrayList<Long> getFactors(){
|
public List<Long> getFactors(){
|
||||||
solvedCheck("factors");
|
solvedCheck("factors");
|
||||||
return factors;
|
return factors;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 06-05-21
|
// Created: 06-05-21
|
||||||
//Modified: 07-03-21
|
//Modified: 07-30-22
|
||||||
//How many circular primes are there below one million?
|
//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
|
//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
|
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
|
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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import mattrixwv.NumberAlgorithms;
|
import mattrixwv.NumberAlgorithms;
|
||||||
|
|
||||||
@@ -33,12 +34,12 @@ public class Problem35 extends Problem{
|
|||||||
//Static variables
|
//Static variables
|
||||||
private static final int MAX_NUM = 999999; //The largest number that we are checking for primes
|
private static final int MAX_NUM = 999999; //The largest number that we are checking for primes
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private ArrayList<Integer> primes; //The primes below MAX_NUM
|
private List<Integer> primes; //The primes below MAX_NUM
|
||||||
private ArrayList<Integer> circularPrimes; //The circular primes below MAX_NUM
|
private ArrayList<Integer> circularPrimes; //The circular primes below MAX_NUM
|
||||||
//Functions
|
//Functions
|
||||||
//Returns a list of all rotations of a string passed to it
|
//Returns a list of all rotations of a string passed to it
|
||||||
private ArrayList<String> getRotations(String str){
|
private ArrayList<String> getRotations(String str){
|
||||||
ArrayList<String> rotations = new ArrayList<String>();
|
ArrayList<String> rotations = new ArrayList<>();
|
||||||
rotations.add(str);
|
rotations.add(str);
|
||||||
for(int cnt = 1;cnt < str.length();++cnt){
|
for(int cnt = 1;cnt < str.length();++cnt){
|
||||||
str = str.substring(1) + str.substring(0, 1);
|
str = str.substring(1) + str.substring(0, 1);
|
||||||
@@ -49,8 +50,8 @@ public class Problem35 extends Problem{
|
|||||||
//Constructor
|
//Constructor
|
||||||
public Problem35(){
|
public Problem35(){
|
||||||
super("How many circular primes are there below one million?");
|
super("How many circular primes are there below one million?");
|
||||||
primes = new ArrayList<Integer>();
|
primes = new ArrayList<>();
|
||||||
circularPrimes = new ArrayList<Integer>();
|
circularPrimes = new ArrayList<>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//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
|
//Get all of the rotations of the prime and see if they are also prime
|
||||||
ArrayList<String> rotations = getRotations(Integer.toString(prime));
|
ArrayList<String> rotations = getRotations(Integer.toString(prime));
|
||||||
for(String rotation : rotations){
|
for(String rotation : rotations){
|
||||||
int p = Integer.valueOf(rotation);
|
int p = Integer.parseInt(rotation);
|
||||||
if(!primes.contains(p)){
|
if(!primes.contains(p)){
|
||||||
allRotationsPrime = false;
|
allRotationsPrime = false;
|
||||||
break;
|
break;
|
||||||
@@ -92,6 +93,7 @@ public class Problem35 extends Problem{
|
|||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
primes.clear();
|
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());
|
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
|
//Returns the ArrayList of primes < MAX_NUM
|
||||||
public ArrayList<Integer> getPrimes(){
|
public List<Integer> getPrimes(){
|
||||||
solvedCheck("list of primes");
|
solvedCheck("list of primes");
|
||||||
return primes;
|
return primes;
|
||||||
}
|
}
|
||||||
//Returns the ArrayList of circular primes < MAX_NUM
|
//Returns the ArrayList of circular primes < MAX_NUM
|
||||||
public ArrayList<Integer> getCircularPrimes(){
|
public List<Integer> getCircularPrimes(){
|
||||||
solvedCheck("list of circular primes");
|
solvedCheck("list of circular primes");
|
||||||
return circularPrimes;
|
return circularPrimes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 07-03-21
|
//Modified: 07-30-22
|
||||||
//What is the 10001th prime number?
|
//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
|
//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
|
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
|
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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import mattrixwv.NumberAlgorithms;
|
import mattrixwv.NumberAlgorithms;
|
||||||
|
|
||||||
@@ -33,13 +34,13 @@ public class Problem7 extends Problem{
|
|||||||
//Static variables
|
//Static variables
|
||||||
private static final long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get
|
private static final long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private ArrayList<Long> primes;
|
private List<Long> primes;
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem7(){
|
public Problem7(){
|
||||||
super(String.format("What is the %dth prime number?", NUMBER_OF_PRIMES));
|
super(String.format("What is the %dth prime number?", NUMBER_OF_PRIMES));
|
||||||
primes = new ArrayList<Long>();
|
primes = new ArrayList<>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
|||||||
Reference in New Issue
Block a user