Sunday, April 17, 2016

Installing SBT in the RHEL

1. log into the linux server using Putty application
2. Type curl https://binary.com/sbt/rpm/rpm  sudo tee /etc/yu,.repos.d/bintray-sbt-rpm.repo
3. yum install sbt




Check the sbt version
 sbt "show sbtVersion"
[info] Set current project to scala (in build file:/opt/SPARK_PROJECT/scala/)

[info] 0.13.9

Friday, April 15, 2016

Tail Recursive function Example

Factorial Function without tail recursion

package TailRecur

object Factorial4 {
def factorial(n:Int): Int ={
if(n==0)1
else n*factorial(n-1)
  }                                  //> factorial: (n: Int)Int
  factorial(5)                       //> res0: Int = 120
}

if 5==0 
5* factorial(4)
5 * 4 * factorial(3)
5 * 4 * 3 * factorial(2)
5 * 4 * 3 * 2 * factorial(1)
5 * 4 * 3 *2 *1 =120 

---------------------------------
Tail Recursive function factorial function


object Factorial4 {

def factorial(n:Int): Int ={
 def loop(acc: Int, n: Int): Int =
  if(n==0)acc
   else loop(acc * n, n-1)
   loop(1,n)
  }                   //> factorial: (n: Int)Int
   factorial(5)       //> res0: Int = 120

  }

---------------------------------
 if 5==0 , false 
 else, loop(1*5,4)
 if 4==0 , false 
 else, loop(5 * 4,3)
 if 3==0 , false 
 else, loop(20*3,2)
 if 2==0 , false 
 else, loop(60*2,1)
 if 1==0 , false 
 else, loop(120*1,0)
 if 0==0 , true
 120







Newton–Raphson method of finding Square Root Program in Scala

To Know more about the Newton-Raphson Method , check out the wiki
https://en.wikipedia.org/wiki/Newton%27s_method


     
object wk1 {
   
def sqrt(Num: Double): Double ={

def sqrIr(sq: Double, Num: Double): Double  =

if (Isok(sq,Num))sq
else sqrIr(improve(sq,Num),Num)

def Isok(sq: Double , Num: Double) =
scala.math.abs(sq * sq - Num) / Num < 0.001

def improve(sq: Double, Num: Double) =
(sq + Num/sq) /2

sqrIr(1.0,Num)

 }                //> sqrt: (Num: Double)Double
 sqrt(5)          //> res0: Double = 2.2360688956433634
 sqrt(1067)       //> res1: Double = 32.66497607490649

-----------------------------scala program---------
object Sqrt {
    def main(args: Array[String]) {
    println("Newton's Square root")
      for (row <- 1 to 10) {
      print(sqrt(row))
      println()
    
  }}

def sqrt(Num: Double): Double ={

def sqrIr(sq: Double, Num: Double): Double  =
if (Isok(sq,Num))sq
else sqrIr(improve(sq,Num),Num)

def Isok(sq: Double , Num: Double) =
scala.math.abs(sq * sq - Num) / Num < 0.001

def improve(sq: Double, Num: Double) =
(sq + Num/sq) /2

 sqrIr(1.0,Num)
  }
   
}