Updated libraries and increased test coverage

This commit is contained in:
2023-04-13 19:12:33 -04:00
parent 783fc7009c
commit f4dbf4e4dc
20 changed files with 560 additions and 227 deletions

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/NumberAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 06-25-22
//Modified: 04-13-23
//This class contains algorithms for numbers that I've found it useful to keep around
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -277,7 +277,7 @@ public class NumberAlgorithms{
}
public static List<BigInteger> getFactors(BigInteger goalNumber) throws InvalidResult{
//You need to get all the primes that could be factors of this number so you can test them
BigInteger topPossiblePrime = goalNumber.divide(BigInteger.TWO);
BigInteger topPossiblePrime = goalNumber.sqrt().add(BigInteger.ONE);
List<BigInteger> primes = getPrimes(topPossiblePrime);
ArrayList<BigInteger> factors = new ArrayList<>();
@@ -303,7 +303,12 @@ public class NumberAlgorithms{
//If for some reason the goalNumber is not 1 throw an error
if(!goalNumber.equals(BigInteger.ONE)){
throw new InvalidResult("The factor was not 1: " + goalNumber);
if(isPrime(goalNumber)){
factors.add(goalNumber);
}
else{
throw new InvalidResult("The factor was not 1: " + goalNumber);
}
}
//Return the list of factors

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/Stopwatch.java
//Matthew Ellison (Mattrixwv)
// Created: 03-01-19
//Modified: 06-26-22
//Modified: 04-13-23
//This file contains a class that is used to time the execution time of other programs
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -122,6 +122,10 @@ public class Stopwatch{
}
}
if(duration < 0){
resolution = TIME_RESOLUTION.ERROR;
}
//Turn the number into a string
int durationFraction = (int)Math.round(((duration % 1) * 1000));
String time = String.format("%d.%03d", duration.intValue(), durationFraction);
@@ -143,11 +147,6 @@ public class Stopwatch{
@Override
public String toString(){
try{
return getStr();
}
catch(InvalidResult error){
return "There was an error in getStr(): " + error;
}
return getStr();
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/StringAlgorithms.java
//Matthew Ellison
// Created: 07-03-21
//Modified: 10-11-21
//Modified: 04-13-23
//This class contains algorithms for strings that I've found it useful to keep around
/*
Copyright (C) 2021 Matthew Ellison
Copyright (C) 2023 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
@@ -33,7 +33,7 @@ public class StringAlgorithms{
public static List<String> getPermutations(String master){
return getPermutations(master, 0);
}
private static ArrayList<String> getPermutations(String master, int num){
protected static ArrayList<String> getPermutations(String master, int num){
ArrayList<String> perms = new ArrayList<>();
//Check if the number is out of bounds
if((num >= master.length()) || (num < 0)){

View File

@@ -1,17 +1,24 @@
//JavaClasses/src/main/java/mattrixwv/Exceptions/InvalidResult.java
//Matthew Ellison
// Created: 08-24-20
//Modified: 08-24-20
//Modified: 04-13-23
//This is an exception for an invalid result out of one of my algorithms
package com.mattrixwv.exceptions;
public class InvalidResult extends RuntimeException{
private static final long serialVersionUID = 1L;
public InvalidResult(){
super();
}
public InvalidResult(String msg){
super(msg);
}
public InvalidResult(Throwable cause){
super(cause);
}
public InvalidResult(String msg, Throwable cause){
super(msg, cause);
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/HexagonalNumberGenerator.java
//Matthew Ellison
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
//This class generates hexagonal numbers
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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,7 +27,7 @@ import java.util.NoSuchElementException;
public class HexagonalNumberGenerator implements Iterator<Long>{
private Long num;
protected long num;
public HexagonalNumberGenerator(){
num = 1L;
@@ -35,23 +35,25 @@ public class HexagonalNumberGenerator implements Iterator<Long>{
@Override
public boolean hasNext(){
return (2 * num * num) > 0;
return (2 * num * num) > num;
}
@Override
public Long next(){
Long newNum = ((2 * num * num) - num);
++num;
if(num > 0){
return newNum;
}
else{
//If the number will result in an overflow throw an exception
if(!hasNext()){
throw new NoSuchElementException("Number overflow");
}
//Generate and return the hexagonal number
Long hexNum = ((2L * num * num) - num);
++num;
return hexNum;
}
public static boolean isHexagonal(Long x){
Long n = Math.round((Math.sqrt(1.0 + (8 * x)) + 1) / 4);
return ((2 * n * n) - n) == x;
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) + 1L) / 4L);
return ((2L * n * n) - n) == x;
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
//This class generates pentagonal numbers
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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,7 +27,7 @@ import java.util.NoSuchElementException;
public class PentagonalNumberGenerator implements Iterator<Long>{
private Long num;
protected long num;
public PentagonalNumberGenerator(){
num = 1L;
@@ -35,23 +35,22 @@ public class PentagonalNumberGenerator implements Iterator<Long>{
@Override
public boolean hasNext(){
return (3 * num * num) > 0;
return (3L * num * num) > num;
}
@Override
public Long next(){
long newNum = ((3 * num * num) - num) / 2;
++num;
if(num > 0){
return newNum;
}
else{
if(!hasNext()){
throw new NoSuchElementException("Number overflow");
}
long pentNum = ((3L * num * num) - num) / 2L;
++num;
return pentNum;
}
public static boolean isPentagonal(Long x){
Long n = Math.round((Math.sqrt(1.0 + (24 * x)) + 1) / 6);
return (((3 * n * n) - n) / 2) == x;
Long n = Math.round((Math.sqrt(1.0D + (24L * x)) + 1L) / 6L);
return (((3L * n * n) - n) / 2L) == x;
}
}

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-25-22
//Modified: 04-13-23
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -31,37 +31,36 @@ import java.util.NoSuchElementException;
public class SieveOfEratosthenes implements Iterator<Long>{
long possiblePrime;
private Map<Long, ArrayList<Long>> dict;
protected long possiblePrime;
protected Map<Long, ArrayList<Long>> dict;
public SieveOfEratosthenes(){
dict = new HashMap<>();
possiblePrime = 2;
}
@Override
public boolean hasNext(){
return true;
}
@Override
public Long next(){
long prime;
//If this is the first run just return 2
if(possiblePrime <= 2){
if(possiblePrime == 2){
prime = possiblePrime++;
return prime;
}
//Loop until you find a prime number
for(;dict.containsKey(possiblePrime);possiblePrime += 2){
if(possiblePrime < 0){
throw new NoSuchElementException("the next prime cannot be described by a long");
}
//Create the next entry for all entries in the map
for(long num : dict.get(possiblePrime)){
if(!dict.containsKey(possiblePrime + num + num)){
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(num));
dict.put(possiblePrime + num + num, tempArray);
dict.put(possiblePrime + num + num, new ArrayList<>(Arrays.asList(num)));
}
else{
dict.get(possiblePrime + num + num).add(num);
@@ -70,16 +69,14 @@ public class SieveOfEratosthenes implements Iterator<Long>{
//Delete the current entry
dict.remove(possiblePrime);
}
//Protect against overflows
if(possiblePrime < 0){
throw new NoSuchElementException("the next prime cannot be described by a long");
}
//Save that the number is a prime
prime = possiblePrime;
//Add the next entry to the prime
if(!dict.containsKey(prime * 3)){
ArrayList<Long> tempArray = new ArrayList<>(Arrays.asList(prime));
dict.put(prime * 3, tempArray);
}
else{
dict.get(prime * 3).add(prime);
}
//Add the next entry to the prime dictionary
dict.put(prime * 3, new ArrayList<>(Arrays.asList(prime)));
//Move on to the next possible prime
possiblePrime += 2;

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java
//Matthew Ellison
// Created: 06-30-21
//Modified: 06-25-22
//Modified: 04-13-23
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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
@@ -31,17 +31,20 @@ import java.util.Map;
public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
BigInteger possiblePrime;
private Map<BigInteger, ArrayList<BigInteger>> dict;
protected BigInteger possiblePrime;
protected Map<BigInteger, ArrayList<BigInteger>> dict;
public SieveOfEratosthenesBig(){
dict = new HashMap<>();
possiblePrime = BigInteger.TWO;
}
@Override
public boolean hasNext(){
return true;
}
@Override
public BigInteger next(){
BigInteger prime;
@@ -71,14 +74,8 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
}
//Save that the number is a prime
prime = possiblePrime;
BigInteger loc = prime.multiply(BigInteger.valueOf(3));
if(!dict.containsKey(loc)){
ArrayList<BigInteger> tempArray = new ArrayList<>(Arrays.asList(prime));
dict.put(loc, tempArray);
}
else{
dict.get(loc).add(prime);
}
//Add the next entry to the prime dictionary
dict.put(prime.multiply(BigInteger.valueOf(3)), new ArrayList<>(Arrays.asList(prime)));
//Move on to the next possible prime
possiblePrime = possiblePrime.add(BigInteger.TWO);

View File

@@ -1,10 +1,10 @@
//JavaClasses/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//Modified: 04-13-23
//This class generates triangular numbers
/*
Copyright (C) 2022 Matthew Ellison
Copyright (C) 2023 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,7 +27,8 @@ import java.util.NoSuchElementException;
public class TriangularNumberGenerator implements Iterator<Long>{
private Long num;
protected long num;
public TriangularNumberGenerator(){
num = 1L;
@@ -35,23 +36,22 @@ public class TriangularNumberGenerator implements Iterator<Long>{
@Override
public boolean hasNext(){
return (num * num) > 0;
return ((num * num) + num) > num;
}
@Override
public Long next(){
Long newNum = ((num * num) + num) / 2;
++num;
if(num > 0){
return newNum;
}
else{
if(!hasNext()){
throw new NoSuchElementException("Number overflow");
}
Long newNum = ((num * num) + num) / 2L;
++num;
return newNum;
}
public static boolean isTriangular(Long x){
Long n = Math.round((Math.sqrt(1.0 + (8 * x)) - 1) / 2);
return (((n * n) + n) / 2) == x;
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) - 1L) / 2L);
return (((n * n) + n) / 2L) == x;
}
}