Created
September 3, 2014 08:16
-
-
Save dejan/627d862a519b97d048b0 to your computer and use it in GitHub Desktop.
prime factors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.mutable.ListBuffer | |
object Factorization { | |
implicit class FancyInt(val n: Int) extends AnyVal { | |
def primeFactors(): List[Int] = { | |
var factor = 2 | |
var number = n | |
var factors = new ListBuffer[Int]() | |
do { | |
if (number%factor == 0) { | |
number /= factor | |
factors += factor | |
} else { | |
factor += 1 | |
} | |
} while (number > 1) | |
factors.toList | |
} | |
} | |
def main(args: Array[String]) { | |
println(315.primeFactors) // List(3, 3, 5, 7) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment