If you do:
println getFactors( 12 )*.class.name
You can see:
[java.lang.Integer, java.lang.Integer, java.math.BigDecimal, java.lang.Integer, java.math.BigDecimal]
Thus, 6they 4are instances BigDecimal, not Integerinstances.
So, containsit crashes (since you are looking for Integer(6)notBigDecimal(6)
If you change:
retList.add(number / i)
in
retList.add(number.intdiv( i ) )
Then your results will remain intact, and your statements will work :-)
By the way, just for fun, your function can be rewritten as:
List getFactors( int number ) {
(1..Math.sqrt(number)).findAll { i -> number % i == 0 }
.collectMany { i ->
if( i > 1 ) {
[ i, number.intdiv( i ) ]
}
else {
[ i ]
}
}
}
source
share