RosettaCodeData/Task/Prime-decomposition/Scala/prime-decomposition-3.scala
Ingy döt Net 776bba907c Sync
2013-10-27 22:24:23 +00:00

25 lines
544 B
Scala

class PrimeFactors[N](n: N)(implicit num: Integral[N]) extends Iterator[N] {
import num._
val two = one + one
var currentN = n
var divisor = two
def next = {
if (!hasNext)
throw new NoSuchElementException("next on empty iterator")
while(currentN % divisor != zero) {
if (divisor == two)
divisor += one
else
divisor += two
if (divisor * divisor > currentN)
divisor = currentN
}
currentN /= divisor
divisor
}
def hasNext = currentN != one && currentN > zero
}