Added javadoc comments

This commit is contained in:
2024-08-11 21:31:00 -04:00
parent ae1346dbcd
commit 3feefdb7dd
12 changed files with 825 additions and 60 deletions

View File

@@ -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;