Skip to content

Commit

Permalink
can be further optimized on the increment
Browse files Browse the repository at this point in the history
  • Loading branch information
luiznasc committed Oct 4, 2023
1 parent 8496593 commit 49f9a89
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions mySqrt/mySqrt.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package main

func mySqrt(x int) int {
count := 0
count := x
upperEdge := x
for {
if count*count > x {
count--
break
upperEdge = count
count = count / 2
} else {
if count*count == x {
return count
}
for i := count; i <= upperEdge; i++ {
if i*i > x {
return i - 1
} else if i*i == x {
return i
}
}
}
count++
}
return count
}

0 comments on commit 49f9a89

Please sign in to comment.