Added number generators

This commit is contained in:
2022-08-20 13:46:58 -04:00
parent e0825fe96e
commit 8f35397177
21 changed files with 441 additions and 17 deletions

View File

@@ -19,7 +19,7 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv;
package com.mattrixwv;
import java.math.BigInteger;

View File

@@ -19,7 +19,7 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv;
package com.mattrixwv;
import java.math.BigInteger;
@@ -29,7 +29,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import mattrixwv.exceptions.InvalidResult;
import com.mattrixwv.exceptions.InvalidResult;
public class NumberAlgorithms{

View File

@@ -19,10 +19,10 @@ Copyright (C) 2022 Matthew Ellison
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv;
package com.mattrixwv;
import mattrixwv.exceptions.InvalidResult;
import com.mattrixwv.exceptions.InvalidResult;
public class Stopwatch{

View File

@@ -19,7 +19,7 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv;
package com.mattrixwv;
import java.util.ArrayList;

View File

@@ -0,0 +1,67 @@
//JavaClasses/src/main/java/com/mattrixwv/Triple.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//This class implements a triplet of variables
/*
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv;
public class Triple<T, U, V>{
private T a;
private U b;
private V c;
public Triple(T a, U b, V c){
this.a = a;
this.b = b;
this.c = c;
}
public T getA(){
return a;
}
public U getB(){
return b;
}
public V getC(){
return c;
}
@Override
public boolean equals(Object o){
if(this == o){
return true;
}
else if(o instanceof Triple<?, ?, ?>){
Triple<?, ?, ?> rightSide = (Triple<?, ?, ?>)o;
return (a.equals(rightSide.a) && b.equals(rightSide.b) && c.equals(rightSide.c));
}
else{
return false;
}
}
@Override
public int hashCode(){
return a.hashCode() + b.hashCode() * c.hashCode();
}
@Override
public String toString(){
return "[" + a.toString() + ", " + b.toString() + ", " + c.toString() + "]";
}
}

View File

@@ -3,7 +3,7 @@
// Created: 08-24-20
//Modified: 08-24-20
//This is an exception for an invalid result out of one of my algorithms
package mattrixwv.exceptions;
package com.mattrixwv.exceptions;
public class InvalidResult extends Exception{

View File

@@ -0,0 +1,57 @@
//JavaClasses/src/main/java/mattrixwv/HexagonalNumberGenerator.java
//Matthew Ellison
// Created: 08-20-22
//Modified: 08-20-22
//This class generates hexagonal numbers
/*
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.generators;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class HexagonalNumberGenerator implements Iterator<Long>{
private Long num;
public HexagonalNumberGenerator(){
num = 1L;
}
@Override
public boolean hasNext(){
return (2 * num * num) > 0;
}
@Override
public Long next(){
Long newNum = ((2 * num * num) - num);
++num;
if(num > 0){
return newNum;
}
else{
throw new NoSuchElementException("Number overflow");
}
}
public static boolean isHexagonal(Long x){
Long n = Math.round((Math.sqrt(1 + (8 * x)) + 1) / 4);
return ((2 * n * n) - n) == x;
}
}

View File

@@ -0,0 +1,57 @@
//JavaClasses/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//This class generates pentagonal numbers
/*
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.generators;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class PentagonalNumberGenerator implements Iterator<Long>{
private Long num;
public PentagonalNumberGenerator(){
num = 1L;
}
@Override
public boolean hasNext(){
return (3 * num * num) > 0;
}
@Override
public Long next(){
long newNum = ((3 * num * num) - num) / 2;
++num;
if(num > 0){
return newNum;
}
else{
throw new NoSuchElementException("Number overflow");
}
}
public static boolean isPentagonal(Long x){
Long n = Math.round((Math.sqrt(1 + (24 * x)) + 1) / 6);
return (((3 * n * n) - n) / 2) == x;
}
}

View File

@@ -19,7 +19,7 @@ Copyright (C) 2022 Matthew Ellison
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv;
package com.mattrixwv.generators;
import java.util.ArrayList;

View File

@@ -19,7 +19,7 @@ Copyright (C) 2022 Matthew Ellison
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv;
package com.mattrixwv.generators;
import java.math.BigInteger;

View File

@@ -0,0 +1,57 @@
//JavaClasses/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java
//Mattrixwv
// Created: 08-20-22
//Modified: 08-20-22
//This class generates triangular numbers
/*
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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.generators;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class TriangularNumberGenerator implements Iterator<Long>{
private Long num;
public TriangularNumberGenerator(){
num = 1L;
}
@Override
public boolean hasNext(){
return (num * num) > 0;
}
@Override
public Long next(){
Long newNum = ((num * num) + num) / 2;
++num;
if(num > 0){
return newNum;
}
else{
throw new NoSuchElementException("Number overflow");
}
}
public static boolean isTriangular(Long x){
Long n = Math.round((Math.sqrt(1 + (8 * x)) - 1) / 2);
return (((n * n) + n) / 2) == x;
}
}