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