Created
March 8, 2018 18:38
-
-
Save KeKsBoTer/83c224f6e26b7a194b98e47c9333338b to your computer and use it in GitHub Desktop.
Dice graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"math/big" | |
"strconv" | |
"github.com/ALTree/bigfloat" | |
) | |
type set struct { | |
n, k, s int64 | |
} | |
func main() { | |
flag.Parse() | |
length := flag.NArg() | |
if length < 1 { | |
fmt.Println("missing number of dices") | |
return | |
} | |
n, err := strconv.Atoi(flag.Arg(0)) | |
if err != nil { | |
fmt.Println("argument is not a number") | |
return | |
} | |
omega := bigfloat.Pow(big.NewFloat(6), big.NewFloat(float64(n))) | |
plotValues(n, 6*n, func(s int64) float64 { | |
r := würfelSummenSatz(s, int64(n)) | |
f := new(big.Float).SetInt(r) | |
v, _ := f.Quo(f, omega).Float64() | |
return v | |
}, "plot-"+strconv.Itoa(n)+".png") | |
} | |
func würfelSummenSatz(s, n int64) *big.Int { | |
return sum(0, n, func(k int64) *big.Int { | |
return partialSum(s, n, k) | |
}) | |
} | |
func partialSum(s, n, k int64) *big.Int { | |
nk := big.NewInt(0).Binomial(n, k) | |
skn := big.NewInt(0).Binomial(s-6*k-1, s-6*k-n) | |
result := nk.Mul(nk, skn) | |
if k%2 == 1 { | |
result.Neg(result) | |
} | |
return result | |
} | |
func sum(start, end int64, f func(int64) *big.Int) *big.Int { | |
sum := big.NewInt(0) | |
for i := start; i <= end; i++ { | |
sum.Add(sum, f(i)) | |
} | |
return sum | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"fmt" | |
"io/ioutil" | |
"strconv" | |
"github.com/wcharczuk/go-chart" | |
) | |
func plotValues(start, end int, f func(int64) float64, fileName string) { | |
values := make([]chart.Value, end-start+2) | |
for i := start; i <= end; i++ { | |
values[i-start] = chart.Value{ | |
Value: f(int64(i)), | |
Label: strconv.Itoa(i), | |
} | |
} | |
graph := chart.BarChart{ | |
Title: "", | |
TitleStyle: chart.StyleShow(), | |
Background: chart.Style{ | |
Padding: chart.Box{ | |
Top: 40, | |
}, | |
}, | |
Height: 512, | |
BarWidth: 60, | |
XAxis: chart.Style{ | |
Show: true, | |
}, | |
YAxis: chart.YAxis{ | |
Style: chart.Style{ | |
Show: true, | |
}, | |
}, | |
Bars: values, | |
} | |
buffer := bytes.NewBuffer([]byte{}) | |
err := graph.Render(chart.PNG, buffer) | |
if err != nil { | |
fmt.Println(err) | |
} | |
ioutil.WriteFile(fileName, buffer.Bytes(), 0644) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment