In theory, integer multiplication is faster than operations on fractional numbers — in this case, extracting the root. But this is not the only reason. Especially, the calculation of the root can be made before the cycle, since the cycle p does not change. And we get one operation instead of a heap of multiplications in a cycle.
I don’t know Pascal, but suppose that a 64-bit integer type is used for numbers.
When we extract the square root, the number is given in real double precision. There are 53 significant bits, not 64. Oops. This is a potential bug of the second algorithm. Although with integer multiplication we should take care of the absence of overflow.
On the values ​​(2 27 +1) 2 = 18014398777917441 and (2 30 +1) 2 = 1152921506754330625, the bug does not appear. Frankly speaking, I am not sure whether it is possible to choose a number that is a complete square, so that the square root of it, reduced to double, is strictly less than the original. But if it succeeds, it will mean that the second algorithm contains an error. Therefore, there is usually something else added to accurately capture the root.
Yes, as for the algorithm itself - there are a lot of places that can be optimized. If we have already determined that the number is not prime, it is not advisable to check its further divisors. The only even simple is 2, all other even ones can not be checked. As well as not to check the even divisors.
And you can also implement an algorithm based on the sieve of Eratosthenes. On the basis, because here the interval is set, not starting at 1.
a*a <banda < sqrt(b)equivalent? And they work under normal compilation in the same way (within the limits of error). - pavel*is faster than taking the square root ofsqrt. Multiplication can be performed with a single processor command, and the square root is a complex and lengthy iterative operation." But in general, it seems that the teacher simply does not know that the modern mathematical coprocessor is able to calculate the square root at the same speed as the multiply. - user194374