Advertisement
polectron

Untitled

Feb 11th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. package alg717254455R.s2;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Loop4 {
  6. public static void loop4(int n) {
  7. /*Quadratic algorithm O(n^4) */
  8. Random rn = new Random();
  9. @SuppressWarnings("unused")
  10. int cont=0;
  11. for (int i=1; i<=n; i++)
  12. for (int j=1; j<=i; j++)
  13. for (int k=1; k<=i; k++)
  14. for (int l=1; l<=i; l++)
  15. cont += rn.nextInt();
  16. }
  17.  
  18. public static void main(String arg[]) {
  19. long t1,t2;
  20. int nTimes = Integer.parseInt(arg[0]);
  21.  
  22. for (int n=1; n<=100000; n*=2) {
  23. t1 = System.currentTimeMillis();
  24.  
  25. for (int repetitions=1; repetitions<=nTimes; repetitions++){
  26. loop4(n);
  27. }
  28.  
  29. t2 = System.currentTimeMillis();
  30. System.out.println ("n="+n+ "**TIME=" +(t2-t1)+ " ** nTimes=" + nTimes);
  31. } //for
  32. } //main
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement