Added number generators
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 06-25-22
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
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.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
long possiblePrime;
|
||||
private 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){
|
||||
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);
|
||||
}
|
||||
else{
|
||||
dict.get(possiblePrime + num + num).add(num);
|
||||
}
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//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);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime += 2;
|
||||
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 06-25-22
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
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.math.BigInteger;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
BigInteger possiblePrime;
|
||||
private 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;
|
||||
|
||||
if(possiblePrime.compareTo(BigInteger.TWO) <= 0){
|
||||
//Return 2 and move to 3
|
||||
prime = possiblePrime;
|
||||
possiblePrime = possiblePrime.add(BigInteger.ONE);
|
||||
return prime;
|
||||
}
|
||||
|
||||
//Loop until you find a prime number
|
||||
for(;dict.containsKey(possiblePrime);possiblePrime = possiblePrime.add(BigInteger.TWO)){
|
||||
//Create the next entry for all entries in the map
|
||||
for(BigInteger num : dict.get(possiblePrime)){
|
||||
BigInteger loc = possiblePrime.add(num).add(num);
|
||||
if(!dict.containsKey(loc)){
|
||||
ArrayList<BigInteger> tempArray = new ArrayList<>(Arrays.asList(num));
|
||||
dict.put(loc, tempArray);
|
||||
}
|
||||
else{
|
||||
dict.get(loc).add(num);
|
||||
}
|
||||
}
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//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);
|
||||
}
|
||||
//Move on to the next possible prime
|
||||
possiblePrime = possiblePrime.add(BigInteger.TWO);
|
||||
|
||||
return prime;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user