This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Algorithms based on the Fibonacci series implementation: 1,1,2,3,5,8,13,21,34,55,... | |
def fib(n: Int): BigInt = { | |
if (n < 1) 0 | |
else if (n < 3) 1 | |
else fib(n-1) + fib(n-2) | |
} | |
// Exponential (very slow, for n> 40 we can see that) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sqrt(a: BigDecimal, scale: Int = 0): BigDecimal = { | |
def sqrt(a: BigInt): BigInt = { // find b: BigInt => b^2 <= a < (b + 1)^2 | |
if (a < 0) throw new ArithmeticException("Square root of negative number") | |
else if (a < 2) a | |
else { | |
def next(n: BigInt, i: BigInt): BigInt = (n + i/n) >> 1 | |
var n0 = BigInt(2).pow((a.bitLength >> 1) + (a.bitLength & 1)) | |
var n1 = next(n0, a) | |
while ((n1 - n0).abs > 1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.model | |
import app.util.Cfg | |
import scala.util.Failure | |
import anorm._ | |
trait Entity[A] { | |
val parser: RowParser[A] | |
val sql: String |