Skip to content

Instantly share code, notes, and snippets.

@onilton
Created April 27, 2015 07:09
Show Gist options
  • Save onilton/fc922b3708f02c3da323 to your computer and use it in GitHub Desktop.
Save onilton/fc922b3708f02c3da323 to your computer and use it in GitHub Desktop.
import scala.tools.ant.sabbus.Break
object test {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val vetorInverso = Map( 0 -> 0, 1 -> 5, 2 -> 4, 3 -> 3, 4 -> 2, 5 ->1 )
//> vetorInverso : scala.collection.immutable.Map[Int,Int] = Map(0 -> 0, 5 -> 1
//| , 1 -> 5, 2 -> 4, 3 -> 3, 4 -> 2)
vetorInverso(3) //> res0: Int = 3
//val moduloTest = 5
//for (x <- 0 to 15)
// println (s"$x % $moduloTest = " + x % moduloTest)
case class Matriz(modulo: Int) {
val maxi = modulo -1
val maxj = maxi
def realValor(i: Int, j:Int) = (i + j) % modulo
def inversoMat(i: Int) = (modulo - i) % modulo
def inversoDefault(i: Int): Int = {
(0 to maxj).find( j => realValor(i,j) == 0).get
}
def valor(i: Int, j: Int) = {
if (i < modulo && j < modulo)
realValor(i,j)
else
throw new Exception("i ou j nao permitido")
}
def show() = {
for (i <- 0 to maxi) {
for (j <- 0 to maxj) {
print("(" + i + "," + j + ")%=" + valor(i,j) + " | " )
}
println()
}
}
}
val minhaMatriz = Matriz(6) //> minhaMatriz : test.Matriz = Matriz(6)
minhaMatriz.show() //> (0,0)%=0 | (0,1)%=1 | (0,2)%=2 | (0,3)%=3 | (0,4)%=4 | (0,5)%=5 |
//| (1,0)%=1 | (1,1)%=2 | (1,2)%=3 | (1,3)%=4 | (1,4)%=5 | (1,5)%=0 |
//| (2,0)%=2 | (2,1)%=3 | (2,2)%=4 | (2,3)%=5 | (2,4)%=0 | (2,5)%=1 |
//| (3,0)%=3 | (3,1)%=4 | (3,2)%=5 | (3,3)%=0 | (3,4)%=1 | (3,5)%=2 |
//| (4,0)%=4 | (4,1)%=5 | (4,2)%=0 | (4,3)%=1 | (4,4)%=2 | (4,5)%=3 |
//| (5,0)%=5 | (5,1)%=0 | (5,2)%=1 | (5,3)%=2 | (5,4)%=3 | (5,5)%=4 |
minhaMatriz.inversoMat(3) //> res1: Int = 3
minhaMatriz.inversoDefault(3) //> res2: Int = 3
//2 - 4
//(i + j) % 5 = 0
//(4,1)% 5 =0
// -4 -> ????
// i =4
// (4 + j) % 5 = 0
//j = (modulo - i) % modulo
//inverso(i) = j = (5 - i) % 5
// j % 5 = -4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment