Skip to content

Commit eb39cdd

Browse files
authored
Merge pull request #73 from francolq/patch-1
new math function is_sqrt
2 parents e5fde44 + 52eb39f commit eb39cdd

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lib/aiken/math.ak

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,24 @@ test sqrt5() {
349349
test sqrt6() {
350350
sqrt(-42) == None
351351
}
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

Comments
 (0)