File tree Expand file tree Collapse file tree 2 files changed +12
-4
lines changed
Expand file tree Collapse file tree 2 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -30,10 +30,12 @@ func intToRoman(num int) string {
3030 builder := strings.Builder {}
3131
3232 for _ , symbol := range symbols {
33- for num >= symbol .value {
34- num -= symbol .value
35- builder .WriteString (symbol .roman )
36- }
33+ divisible := num / symbol .value
34+
35+ num -= divisible * symbol .value
36+
37+ repeated := strings .Repeat (symbol .roman , divisible )
38+ builder .WriteString (repeated )
3739 }
3840
3941 return builder .String ()
Original file line number Diff line number Diff line change @@ -30,3 +30,9 @@ func TestIntToRoman3(t *testing.T) {
3030 t .Errorf ("Expected %s but got %s" , expected , result )
3131 }
3232}
33+
34+ func BenchmarkIntToRoman (b * testing.B ) {
35+ for i := 0 ; i < b .N ; i ++ {
36+ intToRoman (b .N )
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments