Skip to content

Instantly share code, notes, and snippets.

@ggsoft
ggsoft / gist:c1d9b7bae4ffc2361b59f582d8d7d558
Created February 3, 2017 09:04
Algorithms based on the Fibonacci series implementation
// 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)
@ggsoft
ggsoft / gist:f8582768ec4bacd896006ab894ba991b
Last active February 4, 2017 02:15
BigDecimal and BigInt sqrt implementation
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) {
package app.model
import app.util.Cfg
import scala.util.Failure
import anorm._
trait Entity[A] {
val parser: RowParser[A]
val sql: String