Click or drag to resize
gmp_libmpn_sqrtrem Method
Compute the square root of {sp, n} and put the result at {r1p, ceil(n / 2)} and the remainder at {r2p, retval}.

Namespace:  Math.Gmp.Native
Assembly:  Math.Gmp.Native (in Math.Gmp.Native.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public static mp_size_t mpn_sqrtrem(
	mp_ptr r1p,
	mp_ptr r2p,
	mp_ptr sp,
	mp_size_t n
)

Parameters

r1p
Type: Math.Gmp.Nativemp_ptr
The first result integer.
r2p
Type: Math.Gmp.Nativemp_ptr
The second result integer.
sp
Type: Math.Gmp.Nativemp_ptr
The operand integwer.
n
Type: Math.Gmp.Nativemp_size_t
The number of limbs of sp.

Return Value

Type: mp_size_t
The number of limbs of r2p.
Remarks

r2p needs space for n limbs, but the return value indicates how many are produced.

The most significant limb of {sp, n} must be non-zero. The areas {r1p, ceil(n / 2)} and {sp, n} must be completely separate. The areas {r2p, n} and {sp, n} must be either identical or completely separate.

If the remainder is not wanted then r2p can be NULL, and in this case the return value is zero or non-zero according to whether the remainder would have been zero or non-zero.

A return value of zero indicates a perfect square. See also mpn_perfect_square_p.

Examples
// Create multi-precision operands, and expected result.
mp_ptr sp = new mp_ptr(new uint[] { 0x00000001, 0x00000001 });
mp_ptr r1p = new mp_ptr(new uint[sp.Size * (gmp_lib.mp_bytes_per_limb / 4)]);
mp_ptr r2p = new mp_ptr(new uint[sp.Size * (gmp_lib.mp_bytes_per_limb / 4)]);
mp_ptr result = new mp_ptr(new uint[] { 0x00010000, 0x00000000 });
mp_ptr remainder = new mp_ptr(new uint[] { 0x00000001, 0x00000000 });

// Set r1p = trunc(sqrt(sp)), r2p = sp - r1p^2
mp_size_t r2n = gmp_lib.mpn_sqrtrem(r1p, r2p, sp, sp.Size);

// Assert result.
Assert.IsTrue(r2n == 1);
 Assert.IsTrue(r1p.SequenceEqual(result));
Assert.IsTrue(r2p.SequenceEqual(remainder));

// Release unmanaged memory.
gmp_lib.free(sp, r1p, r2p, result);
See Also