Created
February 20, 2015 15:17
-
-
Save taroyabuki/50f24e2b0889ce67c3e7 to your computer and use it in GitHub Desktop.
Prime Factorization http://blog.unfindable.net/archives/8252
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
factor[n_] := Reverse[Flatten[ | |
Map[Table[#[[1]], {#[[2]]}] &, FactorInteger[n]]]] | |
On[Assert]; | |
Assert[factor[24] == {3, 2, 2, 2}]; | |
draw[origin_, frame_, delta_, {p_, rest___}, start_] := { | |
Circle[origin, frame], | |
With[{r = If[p == 1, 1, frame Sin[Pi/p]/(1 + Sin[Pi/p])]}, | |
Table[ | |
With[{t0 = delta + 2 Pi i/p}, | |
With[{t = t0 + If[p == 2, Pi/2, 0]}, | |
With[{center = origin + (frame - r) {Cos[t], Sin[t]}}, | |
If[Length[{rest}] == 0, | |
Disk[center, r], | |
(*Text[start+i,center],*) | |
draw[center, r, t0 + If[First[{rest}] == 2, Pi/2, 0], {rest}, start + i Times @@ {rest}]]]]], | |
{i, 0, p - 1}]]} | |
graphicalFactor[n_] := { | |
draw[{0, 0}, 1, 0, factor[n], 1], | |
Text[n, {0, 0}, | |
BaseStyle -> { | |
FontFamily -> "Palatino", | |
FontSize -> fontSize, | |
If[n < 3, White, Black]}]} | |
fontSize = 24; | |
Manipulate[ | |
Graphics[Flatten[graphicalFactor[n]], PlotRange -> All], | |
{n, 1, 1024, 1}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment