August 27, 2024

P36 - Determine the prime factors of a given positive integer (2).

Construct a list containing the prime factors and their multiplicity.

scala

scala> 315.primeFactorMultiplicity
res0: List[(Int, Int)] = List((3,2), (5,1), (7,1))

Alternately, use a Map for the result.

scala

scala> 315.primeFactorMultiplicity
res0: Map[Int,Int] = Map(3 -> 2, 5 -> 1, 7 -> 1)
Be first to comment
Leave a reply