mirror of
https://bitbucket.org/Mattrixwv/javaclasses.git
synced 2025-12-07 07:23:57 -05:00
Updated libraries and increased test coverage
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user