gmp_lib.mpn_sqrtrem Method

GMP Native Interface for .NET

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
)
Public Shared Function mpn_sqrtrem ( 
	r1p As mp_ptr,
	r2p As mp_ptr,
	sp As mp_ptr,
	n As mp_size_t
) As mp_size_t
public:
static mp_size_t mpn_sqrtrem(
	mp_ptr^ r1p, 
	mp_ptr^ r2p, 
	mp_ptr^ sp, 
	mp_size_t n
)
static member mpn_sqrtrem : 
        r1p : mp_ptr * 
        r2p : mp_ptr * 
        sp : mp_ptr * 
        n : mp_size_t -> mp_size_t 

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);
' Create multi-precision operands, and expected result.
Dim sp As New mp_ptr(New UInteger() { &H1, &H1})
Dim r1p As New mp_ptr(New UInteger(sp.Size * (gmp_lib.mp_bytes_per_limb / 4) - 1) {})
Dim r2p As New mp_ptr(New UInteger(sp.Size * (gmp_lib.mp_bytes_per_limb / 4) - 1) {})
Dim result As New mp_ptr(New UInteger() { &H10000, &H0})
Dim remainder As New mp_ptr(New UInteger() { &H1, &H0})

' Set r1p = trunc(sqrt(sp)), r2p = sp - r1p^2
Dim r2n As mp_size_t = 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)

No code example is currently available or this language may not be supported.

No code example is currently available or this language may not be supported.

See Also