We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents e5fde44 + 52eb39f commit eb39cddCopy full SHA for eb39cdd
lib/aiken/math.ak
@@ -349,3 +349,24 @@ test sqrt5() {
349
test sqrt6() {
350
sqrt(-42) == None
351
}
352
+
353
+/// Checks if an integer has a given integer square root x.
354
+/// The check has constant time complexity (O(1)).
355
+///
356
+/// ```aiken
357
+/// math.is_sqrt(0, 0)
358
+/// math.is_sqrt(25, 5)
359
+/// ! math.is_sqrt(25, -5)
360
+/// math.is_sqrt(44203, 210)
361
+/// ```
362
+pub fn is_sqrt(self: Int, x: Int) -> Bool {
363
+ x * x <= self && ( x + 1 ) * ( x + 1 ) > self
364
+}
365
366
+test is_sqrt1() {
367
+ is_sqrt(44203, 210)
368
369
370
+test is_sqrt2() {
371
+ is_sqrt(975461057789971041, 987654321)
372
0 commit comments