Skip to content

Square Root Calculations #8

@600rr

Description

@600rr

Square root calculations sometimes rounding up instead of rounding down due to the high numbers of decimals.

this can be fixed by this;

sqrtBigInt: {
	name: "Square Root",
	example: "85027756688728992039343472749973395489496426478679146914594092850470138746682004844846731574411929455787504015372183412744082517126948688460316362207223994823057005387889005747347064036786977021248868",
	solver: (data) => {
		const n = BigInt(data);
		if (n < 0n) {
			return 'ERROR';
		}
		if (n < 2n) {
			return n;
		}

		// Newton's method for integer square root
		let x = n;
		let y = (x + 1n) / 2n;
		while (y < x) {
			x = y;
			y = (n / x + x) / 2n;
		}
		return x;
	}
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions