fork download
  1. import java.math.*;
  2. import java.util.*;
  3.  
  4. class Main
  5. {
  6. private BigInteger wyraz2(int n)
  7. {
  8. double pom = Math.sqrt(5);
  9. BigDecimal podstawa = new BigDecimal((1+pom)/2.0);
  10. BigDecimal potega1 = podstawa.pow(n);
  11. podstawa = new BigDecimal((1-pom)/2.0);
  12. BigDecimal potega2 = podstawa.pow(n);
  13. BigDecimal wynik = potega1.add(potega2.negate());
  14. return wynik.divideToIntegralValue(new BigDecimal(pom)).toBigInteger();
  15. }
  16.  
  17. public void run(int n) {
  18. int counter = 0;
  19. long start;
  20. for(int i=0;i<10000;i++)
  21. {
  22. start = System.nanoTime();
  23. BigInteger f2 = wyraz2(n);
  24. long end = System.nanoTime();
  25. if(end-start<0)
  26. {
  27. counter++;
  28. System.out.println("2. Czas wykonania (w nanosekundach): "+(end-start)+" (wzor Bineta)");
  29. System.out.println("Start: "+start);
  30. System.out.println("End: "+end);
  31. }
  32. }
  33. System.out.println(counter); //313
  34. }
  35.  
  36. public static void main (String[] args) throws java.lang.Exception
  37. {
  38. new Main().run(5);
  39. }
  40. }
Success #stdin #stdout 13.13s 246976KB
stdin
Standard input is empty
stdout
0