fork(1) download
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. uint64_t isqrt( uint64_t const n )
  8. {
  9. double x = n >> (__builtin_clzll(n)/2); // x ist höchstens 2^32
  10. double old_x;
  11. for(;;)
  12. {
  13. old_x = x;
  14. x = (x + n/x)/2;
  15.  
  16. if( std::abs(x - old_x) < 1 )
  17. break;
  18. }
  19.  
  20. return x;
  21. }
  22.  
  23. int main()
  24. {
  25. for ( uint64_t i = 2; i < 3000000; ++i )
  26. {
  27. if ( ( i & ( i - 1 ) ) == 0 )
  28. cout << i << '\n';
  29. uint64_t square = i * i;
  30. if ( isqrt( square - 1 ) != i - 1 )
  31. cout << "error: " << i << " ^2 - 1\n";
  32. if ( isqrt( square ) != i )
  33. cout << "error: " << i << " ^2\n";
  34. if ( isqrt( square + 1 ) != i )
  35. cout << "error: " << i << " ^2 + 1\n";
  36. }
  37. }
Time limit exceeded #stdin #stdout 5s 3340KB
stdin
Standard input is empty
stdout
Standard output is empty