Skip to content

Commit

Permalink
mySqrt - 5 seconds answer
Browse files Browse the repository at this point in the history
  • Loading branch information
luiznasc committed Oct 4, 2023
1 parent 5d67776 commit 8496593
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mySqrt/mySqrt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

func mySqrt(x int) int {
count := 0
for {
if count*count > x {
count--
break
}
count++
}
return count
}
20 changes: 20 additions & 0 deletions mySqrt/mySqrt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "testing"

func TestMySqrt(t *testing.T) {
t.Run("4", func(t *testing.T) {
got := mySqrt(4)
want := 2
if got != want {
t.Errorf("got %v, want %v", got, want)
}
})
t.Run("8", func(t *testing.T) {
got := mySqrt(8)
want := 2
if got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}

0 comments on commit 8496593

Please sign in to comment.