Updated libraries and increased test coverage
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//JavaClasses/src/main/java/mattrixwv/HexagonalNumberGenerator.java
|
||||
//Matthew Ellison
|
||||
// Created: 08-20-22
|
||||
//Modified: 08-20-22
|
||||
//Modified: 04-13-23
|
||||
//This class generates hexagonal 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,7 @@ import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
public class HexagonalNumberGenerator implements Iterator<Long>{
|
||||
private Long num;
|
||||
protected long num;
|
||||
|
||||
public HexagonalNumberGenerator(){
|
||||
num = 1L;
|
||||
@@ -35,23 +35,25 @@ public class HexagonalNumberGenerator implements Iterator<Long>{
|
||||
|
||||
@Override
|
||||
public boolean hasNext(){
|
||||
return (2 * num * num) > 0;
|
||||
return (2 * num * num) > num;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long next(){
|
||||
Long newNum = ((2 * num * num) - num);
|
||||
++num;
|
||||
if(num > 0){
|
||||
return newNum;
|
||||
}
|
||||
else{
|
||||
//If the number will result in an overflow throw an exception
|
||||
if(!hasNext()){
|
||||
throw new NoSuchElementException("Number overflow");
|
||||
}
|
||||
|
||||
//Generate and return the hexagonal number
|
||||
Long hexNum = ((2L * num * num) - num);
|
||||
++num;
|
||||
|
||||
return hexNum;
|
||||
}
|
||||
|
||||
public static boolean isHexagonal(Long x){
|
||||
Long n = Math.round((Math.sqrt(1.0 + (8 * x)) + 1) / 4);
|
||||
return ((2 * n * n) - n) == x;
|
||||
Long n = Math.round((Math.sqrt(1.0D + (8L * x)) + 1L) / 4L);
|
||||
return ((2L * n * n) - n) == x;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user