diff --git a/micro/s99/jaranda/.cache b/micro/s99/jaranda/.cache new file mode 100644 index 0000000..6dbf3e5 Binary files /dev/null and b/micro/s99/jaranda/.cache differ diff --git a/micro/s99/jaranda/.gitignore b/micro/s99/jaranda/.gitignore index bd60b44..61ccb68 100644 --- a/micro/s99/jaranda/.gitignore +++ b/micro/s99/jaranda/.gitignore @@ -10,5 +10,8 @@ project/boot/ project/plugins/project/ # Scala-IDE specific -.idea -.idea_modules +.scala_dependencies + +.settings +.classpath +.project diff --git a/micro/s99/jaranda/.worksheet/src/s99.dummy.scala b/micro/s99/jaranda/.worksheet/src/s99.dummy.scala new file mode 100644 index 0000000..c4b580a --- /dev/null +++ b/micro/s99/jaranda/.worksheet/src/s99.dummy.scala @@ -0,0 +1,10 @@ +package s99 + +import s99.Arithmetic._ + +object dummy {;import org.scalaide.worksheet.runtime.library.WorksheetSupport._; def main(args: Array[String])=$execute{;$skip(81); + + println(315.primeFactors);$skip(111); val res$0 = + + List(3,3,4,5).groupBy(f => f).map(fl => (fl._1, (fl._1, fl._2.size))).values.toList.sortWith(_._1 < _._1);System.out.println("""res0: List[(Int, Int)] = """ + $show(res$0))} +} diff --git a/micro/s99/jaranda/project/plugins.sbt b/micro/s99/jaranda/project/plugins.sbt index a90e4d1..b7c5598 100644 --- a/micro/s99/jaranda/project/plugins.sbt +++ b/micro/s99/jaranda/project/plugins.sbt @@ -1,2 +1,2 @@ -addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.5.2") +addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.4.0") diff --git a/micro/s99/jaranda/src/main/scala/s99/Arithmetic.scala b/micro/s99/jaranda/src/main/scala/s99/Arithmetic.scala new file mode 100644 index 0000000..28da9d3 --- /dev/null +++ b/micro/s99/jaranda/src/main/scala/s99/Arithmetic.scala @@ -0,0 +1,56 @@ +package s99 + +import scala.annotation.tailrec + +object Arithmetic { + + implicit def int2MyInt(i: Int) = new MyInt(i); + + val primes: Stream[Int] = 2 #:: 3 #:: 5 #:: 7 #:: 11 #:: 13 #:: Stream.from(17).filter(_.isPrime) + + class MyInt(val i: Int) { + + def isPrime: Boolean = { + (2 to math.sqrt(i).toInt).filter(n => i % n == 0).length == 0 + } + + def isCoprimeTo(n: Int): Boolean = { + gcd(i, n) == 1 + } + + def totient: Int = { + (1 to i).filter(i.isCoprimeTo(_)).length + } + + def totientImproved : Int = { + i.primeFactorMultiplicity.map( n => (n._1 - 1) * math.pow((n._1),(n._2 - 1)) ).reduce( (n , m) => n * m).intValue(); + } + + def primeFactors: List[Int] = { + @tailrec + def primeFactorsRec(n: Int, ps: Stream[Int], factors: List[Int]) : List[Int] = { + if (n.isPrime) n :: factors + else if (n % ps.head == 0) primeFactorsRec(n / ps.head, ps, ps.head :: factors) + else primeFactorsRec(n, ps.tail, factors) + } + primeFactorsRec(i, primes, Nil).reverse + } + + def primeFactorMultiplicity: List[(Int,Int)] = { + val factorsMultiplicity = i.primeFactors.groupBy(f => f).map(f1 => (f1, (f1._1, f1._2.size))).values.toList + factorsMultiplicity.sortWith(_._1 < _._1) + } + + } + + def gcd(a: Int, b: Int) : Int = (a,b) match { + case (a, 0) => a + case _ => gcd(b, a%b) + } + + def listPrimesInRange(r: Range) : List[Int] = { + primes.dropWhile(_ < r.head).takeWhile(_ <= r.last).toList + } + +} + diff --git a/micro/s99/jaranda/src/main/scala/s99/Lists.scala b/micro/s99/jaranda/src/main/scala/s99/Lists.scala index 88033c2..05576b8 100644 --- a/micro/s99/jaranda/src/main/scala/s99/Lists.scala +++ b/micro/s99/jaranda/src/main/scala/s99/Lists.scala @@ -2,14 +2,6 @@ package s99 import scala.annotation.tailrec -/** - * Created with IntelliJ IDEA. - * User: jaranda - * Date: 05/01/14 - * Time: 09:47 - * To change this template use File | Settings | File Templates. - */ - object Lists { /** diff --git a/micro/s99/jaranda/src/main/scala/s99/dummy.sc b/micro/s99/jaranda/src/main/scala/s99/dummy.sc new file mode 100644 index 0000000..cbde7e0 --- /dev/null +++ b/micro/s99/jaranda/src/main/scala/s99/dummy.sc @@ -0,0 +1,14 @@ +package s99 + +import s99.Arithmetic._ + +object dummy { + + println(315.primeFactors) //> List(3, 3, 5, 7) + + List(3,3,4,5).groupBy(f => f).map(fl => (fl._1, (fl._1, fl._2.size))).values.toList.sortWith(_._1 < _._1) + //> res0: List[(Int, Int)] = List((3,2), (4,1), (5,1)) + + listPrimesInRange(7 to 31) //> res1: List[Int] = List(7, 11, 13, 17, 19, 23, 29, 31) + +} \ No newline at end of file diff --git a/micro/s99/jaranda/src/test/scala/s99/ArithmeticSpec.scala b/micro/s99/jaranda/src/test/scala/s99/ArithmeticSpec.scala new file mode 100644 index 0000000..567a1ae --- /dev/null +++ b/micro/s99/jaranda/src/test/scala/s99/ArithmeticSpec.scala @@ -0,0 +1,41 @@ +package s99 + +import org.scalatest.FunSuite + +class ArithmeticSpec extends FunSuite { + + import s99.Arithmetic._; + + test("Determine whether a given integer number is prime.") { + assert{ 7.isPrime == true } + } + + test("Determine the greatest common divisor of two positive integer numbers.") { + assert{ gcd(36, 63) == 9 } + } + + test("Determine whether two positive integer numbers are coprime.") { + assert{ 35.isCoprimeTo(64) } + } + + test("Calculate Euler's totient function phi(m).") { + assert{ 10.totient == 4 } + } + + test("Determine the prime factors of a given positive integer.") { + assert{ 315.primeFactors == List(3, 3, 5, 7) } + } + + test("Determine the prime factors of a given positive integer (2).") { + assert{ 315.primeFactorMultiplicity == List((3,2), (5,1), (7,1)) } + } + + test("Calculate Euler's totient function phi(m) (improved)."){ + assert{ 10.totientImproved == 4 } + } + + test("Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.") { + assert{ listPrimesInRange(7 to 31) == List(7, 11, 13, 17, 19, 23, 29, 31) } + } + +} \ No newline at end of file diff --git a/micro/s99/jaranda/src/test/scala/s99/Lists.scala b/micro/s99/jaranda/src/test/scala/s99/Lists.scala index 1662ec1..bafa697 100644 --- a/micro/s99/jaranda/src/test/scala/s99/Lists.scala +++ b/micro/s99/jaranda/src/test/scala/s99/Lists.scala @@ -3,14 +3,6 @@ package s99 import org.scalatest.matchers.ShouldMatchers import org.scalatest.FlatSpec -/** - * Created with IntelliJ IDEA. - * User: jaranda - * Date: 05/01/14 - * Time: 10:27 - * To change this template use File | Settings | File Templates. - */ - class ListsSpec extends FlatSpec with ShouldMatchers { "Lists problems set" should "find the last element of a list" in {