Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package lab1;
  7.  
  8. import java.util.Random;
  9.  
  10.  
  11.  
  12. /**
  13.  *
  14.  * @author ge0rge04
  15.  */
  16. public class Main {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.     public static void disp(int n)
  22.     {
  23.  
  24.         if (n%2==0) {
  25.             System.out.print(n+" ");
  26.             if (n==1) return;
  27.             disp(n/2);
  28.  
  29.         }
  30.         else {
  31.             System.out.print(n + " ");
  32.             if (n==1) return;
  33.             disp(3*n+1);
  34.         }
  35.     }
  36.     public static void disp_m(int n,int count)
  37.     {
  38.  
  39.         if (n%2==0) {
  40.             if (n==1) {System.out.print(count+ " pasi"); return;}
  41.             disp_m(n/2,++count);
  42.  
  43.         }
  44.         else {
  45.            
  46.             if (n==1) {System.out.print(count+ " pasi"); return;}
  47.             disp_m(3*n+1,++count);
  48.         }
  49.    
  50.     }
  51.    
  52.     public static void main(String[] args) {
  53.         // TODO code application logic here
  54.         Random gen = new Random();
  55.         int n = gen.nextInt(200);
  56.         long one = System.currentTimeMillis();
  57.         if (n<=100) disp(n);
  58.         else disp_m(n,0);
  59.        long two = System.currentTimeMillis();
  60.        System.out.println("\n"+ (two-one) + " milisecunde");
  61.        
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement