From 592937d95528a9f72382c2ea8c4de6bca59f71b3 Mon Sep 17 00:00:00 2001 From: Michel Daviot Date: Sat, 23 Jul 2011 12:51:35 +0200 Subject: [PATCH 1/3] prime numbers, gcd and factorize --- src/arithmetic/S99Int.scala | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/arithmetic/S99Int.scala diff --git a/src/arithmetic/S99Int.scala b/src/arithmetic/S99Int.scala new file mode 100644 index 0000000..d6be096 --- /dev/null +++ b/src/arithmetic/S99Int.scala @@ -0,0 +1,55 @@ +package arithmetic { + class S99Int(val n: Int) { + import S99Int._ + import scala.collection.mutable.ListBuffer + + def isPrime = { + S99Int.updatePrimesTo(n) + S99Int.knownPrimes.contains(n) + } + + def isCoprimeTo(i: Int): Boolean = gcd(n, i) == 1 + + @annotation.tailrec + private def factorize(n: Int, primes: Seq[Int], buffer: ListBuffer[Int]) { + if (n > 1 && !primes.isEmpty) { + val p = primes(0) + if (n % p == 0) factorize(n / p, primes, buffer + p) else factorize(n, primes.tail, buffer) + } + } + + def primeFactors: List[Int] = { + if (n < 0) error("primeFactors called on a number < 0") + updatePrimesTo(n) + val buffer = new ListBuffer[Int] + factorize(n, knownPrimes, buffer) + buffer.toList + } + } + + object S99Int { + implicit def int2S99Int(i: Int): S99Int = new S99Int(i) + + var knownPrimes = Seq[Int](2, 3) + + def isPrimeWithKnown(i: Int) = !knownPrimes.exists(i % _ == 0) + + def updatePrimesTo(max: Int) { + val lastPrime = knownPrimes.last + if (max > lastPrime) + lastPrime to max foreach (i => if (isPrimeWithKnown(i)) knownPrimes = knownPrimes :+ i) + } + + def gcd(i: Int, j: Int): Int = { + if (j == 0) + i + else + gcd(j, i % j) + } + + def main(args: Array[String]) { + println(0 to 1000 map (_.primeFactors)) + } + } + +} \ No newline at end of file From 9839e7c095410b649e135f6fc5dd76d4293b9272 Mon Sep 17 00:00:00 2001 From: Michel Daviot Date: Fri, 5 Aug 2011 00:36:29 +0200 Subject: [PATCH 2/3] tables de verite --- src/logic/S99Logic.scala | 60 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/logic/S99Logic.scala diff --git a/src/logic/S99Logic.scala b/src/logic/S99Logic.scala new file mode 100644 index 0000000..f285de8 --- /dev/null +++ b/src/logic/S99Logic.scala @@ -0,0 +1,60 @@ +package logic + +object S99Logic { + def and(a: Boolean, b: Boolean) = a && b + def or(a: Boolean, b: Boolean) = a || b + def nor(a: Boolean, b: Boolean) = a || !b + def nand(a: Boolean, b: Boolean) = a && !b + def impl(a: Boolean, b: Boolean) = !a || b + def equ(a: Boolean, b: Boolean) = a == b + + def table2(f: ((Boolean, Boolean) => Boolean)) { + table(2, { s: Seq[Boolean] => f(s(0), s(1)) }) + } + + def table(size: Char, f: (Seq[Boolean] => Boolean)) { + def pow(n: Int, m: Int): Int = { + import scala.annotation.tailrec + @tailrec + def _pow(m: Int, acc: Int): Int = m match { + case 0 => acc + case _ => _pow(m - 1, acc * n) + } + _pow(m, 1) + } + printTitles(size) + //loops through all combination of boolean of length size + for (i <- 0 until pow(2, size)) printValues(intToBooleanSeq(i, size), f) + } + + private def printn(a: Seq[Any]) { + val b = new StringBuilder + for (i <- a) b.append(i).append(" ") + println(b) + } + + private def printTitles(n: Char) { + val end = ('A' + n).toChar + val titles = ('A' until end) map (_.toString) + printn(titles :+ "result") + } + + private def printValues(s: Seq[Boolean], f: (Seq[Boolean] => Boolean)) { + printn(s :+ f(s)) + } + + /** + * Converts the number to a Seq[Boolean] of length size based on binary representaion. + */ + private def intToBooleanSeq(number: Int, size: Int): Seq[Boolean] = { + 0 until size map { pos: Int => (number & (1 << pos)) != 0 } + } + + def main(args: Array[String]) { + def f(a: Seq[Boolean]) = and(a(0), or(a(1), a(2))) + table(3, f) + for (f <- Seq(and _, or _, nor _, nand _, impl _, equ _)) { + table2(f) + } + } +} \ No newline at end of file From 22f24e5fad63509bbf93f8661a6527d8f3b371fd Mon Sep 17 00:00:00 2001 From: Michel Daviot Date: Sat, 6 Aug 2011 18:24:32 +0200 Subject: [PATCH 3/3] P047 : Continue problem P46 by redefining and, or, etc as operators. (i.e. make them methods of a new class with an implicit conversion from Boolean.) not will have to be left as a object method. --- src/logic/S99Logic.scala | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/logic/S99Logic.scala b/src/logic/S99Logic.scala index f285de8..9d52dce 100644 --- a/src/logic/S99Logic.scala +++ b/src/logic/S99Logic.scala @@ -1,12 +1,24 @@ package logic +class S99Logic(val b: Boolean) { + def and(a: Boolean) = a && b + def or(a: Boolean) = a || b + def nor(a: Boolean) = a || !b + def nand(a: Boolean) = a && !b + def impl(a: Boolean) = !a || b + def equ(a: Boolean) = a == b +} + object S99Logic { + implicit def boolean2S99Logic(b: Boolean): S99Logic = new S99Logic(b) + def and(a: Boolean, b: Boolean) = a && b def or(a: Boolean, b: Boolean) = a || b def nor(a: Boolean, b: Boolean) = a || !b def nand(a: Boolean, b: Boolean) = a && !b def impl(a: Boolean, b: Boolean) = !a || b def equ(a: Boolean, b: Boolean) = a == b + def not(b: Boolean) = !b def table2(f: ((Boolean, Boolean) => Boolean)) { table(2, { s: Seq[Boolean] => f(s(0), s(1)) }) @@ -51,10 +63,6 @@ object S99Logic { } def main(args: Array[String]) { - def f(a: Seq[Boolean]) = and(a(0), or(a(1), a(2))) - table(3, f) - for (f <- Seq(and _, or _, nor _, nand _, impl _, equ _)) { - table2(f) - } + table2((a: Boolean, b: Boolean) => a and (a or not(b))) } } \ No newline at end of file