Skip to content

P031 + P046 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/arithmetic/S99Int.scala
Original file line number Diff line number Diff line change
@@ -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))
}
}

}
68 changes: 68 additions & 0 deletions src/logic/S99Logic.scala
Original file line number Diff line number Diff line change
@@ -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)))
}
}