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 diff --git a/src/logic/S99Logic.scala b/src/logic/S99Logic.scala new file mode 100644 index 0000000..9d52dce --- /dev/null +++ b/src/logic/S99Logic.scala @@ -0,0 +1,68 @@ +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)) }) + } + + 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]) { + table2((a: Boolean, b: Boolean) => a and (a or not(b))) + } +} \ No newline at end of file