Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. private long fatorial_iterativo(int num){
  2.         long start = System.nanoTime();
  3.         long fat = 1;
  4.         for (int i = 2; i <= num; i++) {
  5.             fat = fat * i;
  6.         }
  7.         long end = System.nanoTime();
  8.         lbl_valor_tempo_fat_iterativa.setText(Long.toString(end-start));
  9.         return fat;  
  10.     }
  11.     private long fatorial_recursivo(long num){
  12.         long start = System.nanoTime();
  13.         if ((num == 0) || (num == 1)) {
  14.             long end = System.nanoTime();
  15.             lbl_valor_tempo_fat_recursivo.setText(Long.toString(end-start));
  16.             return 1;
  17.            
  18.         }
  19.         else{
  20.             long end = System.nanoTime();
  21.             lbl_valor_tempo_fat_recursivo.setText(Long.toString(end-start));
  22.             return num * fatorial_recursivo(num - 1);
  23.            
  24.         }
  25.        
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement