Instantly share code, notes, and snippets.

View gist:c1d9b7bae4ffc2361b59f582d8d7d558
// 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)
View gist:f8582768ec4bacd896006ab894ba991b
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) {
View gist:db518028672c238f0321
package app.model
import app.util.Cfg
import scala.util.Failure
import anorm._
trait Entity[A] {
val parser: RowParser[A]
val sql: String