progs/lecture3.scala
changeset 152 114a89518aea
parent 77 3cbe3d90b77f
child 153 4383809c176a
equal deleted inserted replaced
151:c5ca7f8e21a5 152:114a89518aea
     1 // Scala Lecture 3
     1 // Scala Lecture 3
     2 //=================
     2 //=================
       
     3 
       
     4 // adding two binary strings very, very lazy manner
       
     5 
       
     6 def badd(s1: String, s2: String) : String = 
       
     7   (BigInt(s1, 2) + BigInt(s2, 2)).toString(2)
       
     8 
       
     9 
       
    10 // collatz function on binary numbers
       
    11 
       
    12 def bcollatz(s: String) : Long = (s.dropRight(1), s.last) match {
       
    13   case ("", '1') => 1                                  // we reached 1
       
    14   case (rest, '0') => 1 + bcollatz(rest)               // even number => divide by two
       
    15   case (rest, '1') => 1 + bcollatz(badd(s + '1', s))   // odd number => s + '1' is 2 * s + 1
       
    16                                                        //               add another s gives 3 * s + 1  
       
    17 } 
       
    18 
       
    19 bcollatz(9.toBinaryString)
       
    20 bcollatz(837799.toBinaryString)
       
    21 bcollatz(100000000000000000L.toBinaryString)
       
    22 bcollatz(BigInt("1000000000000000000000000000000000000000000000000000000000000000000000000000").toString(2))
       
    23 
       
    24 def conv(c: Char) : Int = c match {
       
    25   case '0' => 0
       
    26   case '1' => 1
       
    27 }
       
    28 
       
    29 def badds(s1: String, s2: String, carry: Int) : String = (s1, s2, carry) match {
       
    30   case ("", "", 1) => "1"
       
    31   case ("", "", 0) => ""
       
    32   case (cs1, cs2, carry) => (conv(cs1.last) + conv(cs2.last) + carry) match {
       
    33     case 3 => badds(cs1.dropRight(1), cs2.dropRight(1), 1) + '1'
       
    34     case 2 => badds(cs1.dropRight(1), cs2.dropRight(1), 1) + '0'
       
    35     case 1 => badds(cs1.dropRight(1), cs2.dropRight(1), 0) + '1'
       
    36     case 0 => badds(cs1.dropRight(1), cs2.dropRight(1), 0) + '0'
       
    37   }
       
    38 } 
       
    39 
       
    40 def bcollatz2(s: String) : Long = (s.dropRight(1), s.last) match {
       
    41   case ("", '1') => 1                                          // we reached 1
       
    42   case (rest, '0') => 1 + bcollatz2(rest)                      // even number => divide by two
       
    43   case (rest, '1') => 1 + bcollatz2(badds(s + '1', '0' + s, 0))   // odd number => s + '1' is 2 * s + 1
       
    44                                                                   //         add another s gives 3 * s + 1  
       
    45 } 
       
    46 
       
    47 bcollatz2(9.toBinaryString)
       
    48 bcollatz2(837799.toBinaryString)
       
    49 bcollatz2(100000000000000000L.toBinaryString)
       
    50 bcollatz2(BigInt("1000000000000000000000000000000000000000000000000000000000000000000000000000").toString(2))
       
    51 
     3 
    52 
     4 
    53 
     5 // One of only two places where I conceded to mutable
    54 // One of only two places where I conceded to mutable
     6 // data structures: The following function generates 
    55 // data structures: The following function generates 
     7 // new labels
    56 // new labels