Skip to content

Commit 2d89721

Browse files
committed
simple derive
1 parent 1b04f31 commit 2d89721

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

goderive

1.81 MB
Binary file not shown.

goderive.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ func inputEquation(order *int, args []int) string {
2222
return buffer.String()
2323
}
2424

25+
func outputEquation(args []int) string {
26+
var buffer strings.Builder
27+
var d int
28+
order := len(args) - 1
29+
for i := order; i > 0; i-- {
30+
d = args[len(args)-1-i] * i
31+
if i > 2 {
32+
buffer.WriteString(fmt.Sprintf("%dx^%d + ", d, order-1))
33+
} else if i == 2 {
34+
buffer.WriteString(fmt.Sprintf("%dx + ", d))
35+
} else {
36+
buffer.WriteString(fmt.Sprintf("%d ", d))
37+
}
38+
}
39+
return buffer.String()
40+
}
41+
2542
func main() {
2643
order := flag.Int("order", -1, "The order of the equation to be derived")
2744
flag.Parse()
@@ -41,6 +58,8 @@ func main() {
4158
}
4259
}
4360

44-
input := inputEquation(order, intArgs)
45-
fmt.Println(input)
61+
fmt.Println(inputEquation(order, intArgs))
62+
fmt.Println("Deriving...")
63+
fmt.Println(outputEquation(intArgs))
64+
4665
}

0 commit comments

Comments
 (0)