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 >> (uint64_t)log2(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. }
Success #stdin #stdout 1.94s 3340KB
stdin
Standard input is empty
stdout
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
error: 1435790 ^2 - 1
2097152