Skip to content

Commit

Permalink
Merge pull request #73 from francolq/patch-1
Browse files Browse the repository at this point in the history
new math function is_sqrt
  • Loading branch information
KtorZ authored Mar 22, 2024
2 parents e5fde44 + 52eb39f commit eb39cdd
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/aiken/math.ak
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,24 @@ test sqrt5() {
test sqrt6() {
sqrt(-42) == None
}

/// Checks if an integer has a given integer square root x.
/// The check has constant time complexity (O(1)).
///
/// ```aiken
/// math.is_sqrt(0, 0)
/// math.is_sqrt(25, 5)
/// ! math.is_sqrt(25, -5)
/// math.is_sqrt(44203, 210)
/// ```
pub fn is_sqrt(self: Int, x: Int) -> Bool {
x * x <= self && ( x + 1 ) * ( x + 1 ) > self
}

test is_sqrt1() {
is_sqrt(44203, 210)
}

test is_sqrt2() {
is_sqrt(975461057789971041, 987654321)
}

0 comments on commit eb39cdd

Please sign in to comment.