Added javadoc comments
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/HexagonalNumberGenerator.java
|
||||
//Matthew Ellison
|
||||
// Created: 08-20-22
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class generates hexagonal numbers
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 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,18 +26,48 @@ import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* A generator for hexagonal numbers, which implements the {@link Iterator} interface.
|
||||
*
|
||||
* <p>
|
||||
* Hexagonal numbers are figurate numbers that represent hexagons. The n-th hexagonal number is given by
|
||||
* the formula: H(n) = 2n^2 - n.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This generator allows iteration over hexagonal numbers starting from the first.
|
||||
* </p>
|
||||
*/
|
||||
public class HexagonalNumberGenerator implements Iterator<Long>{
|
||||
/**
|
||||
* The number to generate the next hexagonal number from.
|
||||
*/
|
||||
protected long num;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new HexagonalNumberGenerator starting from the first hexagonal number.
|
||||
*/
|
||||
public HexagonalNumberGenerator(){
|
||||
num = 1L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is a next hexagonal number that can be generated without overflow.
|
||||
*
|
||||
* @return {@code true} if the next hexagonal number can be generated, {@code false} otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return (2 * num * num) > num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next hexagonal number.
|
||||
*
|
||||
* @return the next hexagonal number
|
||||
* @throws NoSuchElementException if the next hexagonal number would cause overflow
|
||||
*/
|
||||
@Override
|
||||
public Long next(){
|
||||
//If the number will result in an overflow throw an exception
|
||||
@@ -52,6 +82,12 @@ public class HexagonalNumberGenerator implements Iterator<Long>{
|
||||
return hexNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given number is a hexagonal number.
|
||||
*
|
||||
* @param x the number to check
|
||||
* @return {@code true} if the number is hexagonal, {@code false} otherwise
|
||||
*/
|
||||
public static boolean isHexagonal(Long x){
|
||||
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) + 1L) / 4L);
|
||||
return ((2L * n * n) - n) == x;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/com/mattrixwv/generators/PentagonalNumberGenerator.java
|
||||
//Mattrixwv
|
||||
// Created: 08-20-22
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class generates pentagonal numbers
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 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,18 +26,47 @@ import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* A generator for pentagonal numbers, which implements the {@link Iterator} interface.
|
||||
*
|
||||
* <p>
|
||||
* Pentagonal numbers are figurate numbers that represent pentagons. The n-th pentagonal number is given by
|
||||
* the formula: P(n) = (3n^2 - n) / 2.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This generator allows iteration over pentagonal numbers starting from the first.
|
||||
* </p>
|
||||
*/
|
||||
public class PentagonalNumberGenerator implements Iterator<Long>{
|
||||
/**
|
||||
* The number to generate the next pentagonal number from.
|
||||
*/
|
||||
protected long num;
|
||||
|
||||
/**
|
||||
* Constructs a new PentagonalNumberGenerator starting from the first pentagonal number.
|
||||
*/
|
||||
public PentagonalNumberGenerator(){
|
||||
num = 1L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is a next pentagonal number that can be generated without overflow.
|
||||
*
|
||||
* @return {@code true} if the next pentagonal number can be generated, {@code false} otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return (3L * num * num) > num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next pentagonal number.
|
||||
*
|
||||
* @return the next pentagonal number
|
||||
* @throws NoSuchElementException if the next pentagonal number would cause overflow
|
||||
*/
|
||||
@Override
|
||||
public Long next(){
|
||||
if(!hasNext()){
|
||||
@@ -49,6 +78,12 @@ public class PentagonalNumberGenerator implements Iterator<Long>{
|
||||
return pentNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given number is a pentagonal number.
|
||||
*
|
||||
* @param x the number to check
|
||||
* @return {@code true} if the number is pentagonal, {@code false} otherwise
|
||||
*/
|
||||
public static boolean isPentagonal(Long x){
|
||||
Long n = Math.round((Math.sqrt(1.0D + (24L * x)) + 1L) / 6L);
|
||||
return (((3L * n * n) - n) / 2L) == x;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenes.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 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
|
||||
@@ -30,21 +30,62 @@ import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* A generator for prime numbers using the Sieve of Eratosthenes algorithm, which implements the {@link Iterator} interface.
|
||||
*
|
||||
* <p>
|
||||
* This implementation generates prime numbers in an incremental fashion using a modified version of the Sieve of Eratosthenes algorithm.
|
||||
* The algorithm uses a map to keep track of the multiples of found prime numbers to efficiently determine the next prime.
|
||||
* </p>
|
||||
*/
|
||||
public class SieveOfEratosthenes implements Iterator<Long>{
|
||||
/**
|
||||
* The next possible prime to be found
|
||||
*/
|
||||
protected long possiblePrime;
|
||||
/**
|
||||
* A dictionary of the primes that have been found and their next multiple
|
||||
*/
|
||||
protected Map<Long, ArrayList<Long>> dict;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new SieveOfEratosthenes instance with an empty prime dictionary
|
||||
* and starts the search from the first possible prime number, 2.
|
||||
*/
|
||||
public SieveOfEratosthenes(){
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether there is a next prime number available.
|
||||
*
|
||||
* <p>
|
||||
* This method always returns {@code true} as the iterator is designed to
|
||||
* generate primes indefinitely.
|
||||
* </p>
|
||||
*
|
||||
* @return {@code true}, as the iterator can generate an infinite number of primes
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next prime number.
|
||||
*
|
||||
* <p>
|
||||
* The method generates the next prime number by checking and updating the
|
||||
* internal map of known multiples. The first prime returned is 2, and subsequent
|
||||
* primes are found by incrementing the possible prime number and checking its
|
||||
* primality using the map.
|
||||
* </p>
|
||||
*
|
||||
* @return the next prime number
|
||||
* @throws NoSuchElementException if the next prime cannot be represented by a {@code long}
|
||||
*/
|
||||
@Override
|
||||
public Long next(){
|
||||
long prime;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/SieveOfEratosthenesBig.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-30-21
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class uses to Sieve of Eratosthenes to generate an infinite number of primes
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 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,23 +28,65 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* A generator for prime numbers using the Sieve of Eratosthenes algorithm, which implements the {@link Iterator} interface.
|
||||
*
|
||||
* <p>
|
||||
* This implementation generates prime numbers in an incremental fashion using a modified version of the Sieve of Eratosthenes algorithm.
|
||||
* The algorithm uses a map to keep track of the multiples of found prime numbers to efficiently determine the next prime.
|
||||
* </p>
|
||||
*/
|
||||
public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
/**
|
||||
* The next possible prime to be found
|
||||
*/
|
||||
protected BigInteger possiblePrime;
|
||||
/**
|
||||
* A dictionary of the primes that have been found and their next multiple
|
||||
*/
|
||||
protected Map<BigInteger, ArrayList<BigInteger>> dict;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new SieveOfEratosthenes instance with an empty prime dictionary
|
||||
* and starts the search from the first possible prime number, 2.
|
||||
*/
|
||||
public SieveOfEratosthenesBig(){
|
||||
dict = new HashMap<>();
|
||||
possiblePrime = BigInteger.TWO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether there is a next prime number available.
|
||||
*
|
||||
* <p>
|
||||
* This method always returns {@code true} as the iterator is designed to
|
||||
* generate primes indefinitely.
|
||||
* </p>
|
||||
*
|
||||
* @return {@code true}, as the iterator can generate an infinite number of primes
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next prime number.
|
||||
*
|
||||
* <p>
|
||||
* The method generates the next prime number by checking and updating the
|
||||
* internal map of known multiples. The first prime returned is 2, and subsequent
|
||||
* primes are found by incrementing the possible prime number and checking its
|
||||
* primality using the map.
|
||||
* </p>
|
||||
*
|
||||
* @return the next prime number
|
||||
* @throws NoSuchElementException if the next prime cannot be represented by a {@code long}
|
||||
*/
|
||||
@Override
|
||||
public BigInteger next(){
|
||||
BigInteger prime;
|
||||
@@ -72,6 +114,10 @@ public class SieveOfEratosthenesBig implements Iterator<BigInteger>{
|
||||
//Delete the current entry
|
||||
dict.remove(possiblePrime);
|
||||
}
|
||||
//Protect against overflows
|
||||
if(possiblePrime.compareTo(BigInteger.ZERO) < 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 dictionary
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/com/mattrixwv/generators/TriangularNumberGenerator.java
|
||||
//Mattrixwv
|
||||
// Created: 08-20-22
|
||||
//Modified: 04-13-23
|
||||
//Modified: 08-11-24
|
||||
//This class generates triangular numbers
|
||||
/*
|
||||
Copyright (C) 2023 Matthew Ellison
|
||||
Copyright (C) 2024 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,19 +26,48 @@ import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
/**
|
||||
* A generator for triangular numbers, which implements the {@link Iterator} interface.
|
||||
*
|
||||
* <p>
|
||||
* Triangular numbers are figurate numbers that represent triangles. The n-th triangular number is given by
|
||||
* the formula: T(n) = n(n + 1) / 2.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* This generator allows iteration over triangular numbers starting from the first.
|
||||
* </p>
|
||||
*/
|
||||
public class TriangularNumberGenerator implements Iterator<Long>{
|
||||
/**
|
||||
* The number to generate the next triangular number from
|
||||
*/
|
||||
protected long num;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a new TriangularNumberGenerator starting from the first triangular number.
|
||||
*/
|
||||
public TriangularNumberGenerator(){
|
||||
num = 1L;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is a next triangular number that can be generated without overflow.
|
||||
*
|
||||
* @return {@code true} if the next triangular number can be generated, {@code false} otherwise
|
||||
*/
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return ((num * num) + num) > num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the next triangular number.
|
||||
*
|
||||
* @return the next triangular number
|
||||
* @throws NoSuchElementException if the next triangular number would cause overflow
|
||||
*/
|
||||
@Override
|
||||
public Long next(){
|
||||
if(!hasNext()){
|
||||
@@ -50,6 +79,12 @@ public class TriangularNumberGenerator implements Iterator<Long>{
|
||||
return newNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given number is a triangular number.
|
||||
*
|
||||
* @param x the number to check
|
||||
* @return {@code true} if the number is triangular, {@code false} otherwise
|
||||
*/
|
||||
public static boolean isTriangular(Long x){
|
||||
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) - 1L) / 2L);
|
||||
return (((n * n) + n) / 2L) == x;
|
||||
|
||||
Reference in New Issue
Block a user