fork download
  1. // Calculate Nth Fibbionacci number in O(log N)
  2. object Milk{
  3. // Matrix multiplication
  4. def matmul(mat1 : Array[ Array [Int] ], mat2 : Array[ Array [Int] ]): Array[ Array [Int] ] = {
  5. val n = mat1.size;
  6. var res = new Array[ Array [Int]](n, n);
  7. for(i <- 0 until n){
  8. for(j <- 0 until n){
  9. res(i)(j) = 0;
  10. for(k <- 0 until n){
  11. res(i)(j) += mat1(i)(k) * mat2(k)(j);
  12. }
  13. }
  14. }
  15. return res;
  16. }
  17. // Matrix powering
  18. def matpow(mat : Array[ Array [Int] ], pow : Int): Array[ Array [Int] ] = {
  19. val n = mat.size;
  20. var res = new Array[ Array [Int]](n, n);
  21. for(i <- 0 until n){
  22. for(j <- 0 until n){
  23. if(i == j) res(i)(j) = 1;
  24. else res(i)(j) = 0;
  25. }
  26. }
  27. while(pow > 0){
  28. if(pow % 2){
  29. res = matmul(res, mat);
  30. }
  31. mat = matmul(mat, mat);
  32. }
  33. return res;
  34. }
  35. // Calculate sum of a
  36. def fibb(n : Int): Int = {
  37. var mat = new Array[ Array [Int]](2, 2);
  38. mat(0)(0) = 1; mat(0)(1) = 1; mat(1)(0) = 1; mat(1)(1) = 0;
  39. mat = matpow(mat, n);
  40. return mat(1)(0);
  41. }
  42. // Main function
  43. def main(args : Array[ String ]) = {
  44. val n = args.size;
  45. for( i <- 0 until n) a(i) = println(fibb(args(i).toInt));
  46. }
  47. }
  48.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.scala:28: error: type mismatch;
 found   : Int
 required: Boolean
			if(pow % 2){
                               ^
Main.scala:31: error: reassignment to val
			mat = matmul(mat, mat);
                            ^
Main.scala:45: error: not found: value a
		for( i <- 0 until n) a(i) = println(fibb(args(i).toInt));
                                     ^
three errors found
stdout
Standard output is empty