Created
October 23, 2018 23:59
-
-
Save exactmike/e20a8d95488f2aea07bca3206c4cefe6 to your computer and use it in GitHub Desktop.
Collatz Conjecture
This file contains hidden or 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
| #https://en.wikipedia.org/wiki/Collatz_conjecture | |
| function Test-CollatzConjecture | |
| { | |
| [cmdletbinding()] | |
| param | |
| ( | |
| [parameter(ParameterSetName = 'SpecifiedNumber')] | |
| [int32]$BeginningNumber | |
| , | |
| [parameter(ParameterSetName = 'RandomNumber')] | |
| [ValidateScript({$_ -ge 2})] | |
| $MinimumNumber = 2 | |
| , | |
| [parameter(ParameterSetName = 'RandomNumber')] | |
| $MaximumNumber = 50 | |
| ) | |
| if ($PSCmdlet.ParameterSetName -eq 'RandomNumber') | |
| { | |
| $BeginningNumber = Get-Random -Maximum $MaximumNumber -Minimum $MinimumNumber | |
| } | |
| $iteration = 0 | |
| $output = [pscustomobject]@{ | |
| Iteration = $iteration | |
| Result = $BeginningNumber | |
| } | |
| $output | |
| Do | |
| { | |
| $iteration++ | |
| if ($BeginningNumber % 2 -eq 0) | |
| { | |
| $ResultNumber = $BeginningNumber/2 | |
| } | |
| else | |
| { | |
| $ResultNumber = ($BeginningNumber * 3) + 1 | |
| } | |
| $output = [pscustomobject]@{ | |
| Iteration = $iteration | |
| Result = $ResultNumber | |
| } | |
| $output | |
| $BeginningNumber = $ResultNumber | |
| } | |
| Until ($ResultNumber -eq 1) | |
| } | |
| function Show-Chart | |
| { | |
| param | |
| ( | |
| $Title = 'Collatz Conjecture' | |
| , | |
| $data | |
| , | |
| $xvaluesproperty | |
| , | |
| $yvaluesproperty | |
| ) | |
| Add-Type -AssemblyName System.Windows.Forms.DataVisualization | |
| $Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart | |
| $ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea | |
| $Series = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Series | |
| $ChartTypes = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType] | |
| $Series.ChartType = $ChartTypes::Bar | |
| $Chart.Series.Add($Series) | |
| $Chart.ChartAreas.Add($ChartArea) | |
| $Chart.Series['Series1'].Points.DataBindXY($data.$xvaluesproperty, $data.$yvaluesproperty) | |
| $Chart.Width = 700 | |
| $Chart.Height = 400 | |
| $Chart.Left = 10 | |
| $Chart.Top = 10 | |
| $Chart.BackColor = [System.Drawing.Color]::White | |
| $Chart.BorderColor = 'Black' | |
| $Chart.BorderDashStyle = 'Solid' | |
| $ChartTitle = New-Object System.Windows.Forms.DataVisualization.Charting.Title | |
| $ChartTitle.Text = $Title | |
| $Font = New-Object System.Drawing.Font @('Microsoft Sans Serif','12', [System.Drawing.FontStyle]::Bold) | |
| $ChartTitle.Font =$Font | |
| $Chart.Titles.Add($ChartTitle) | |
| $ChartArea.Area3DStyle.Enable3D=$True | |
| $ChartArea.Area3DStyle.Inclination = 50 | |
| $Chart.Series['Series1']['PieLineColor'] = 'Black' | |
| $Chart.Series['Series1']['PieLabelStyle'] = 'Outside' | |
| $Chart.Series['Series1'].Label = "#VALX (#VALY)" | |
| #region Windows Form to Display Chart | |
| $AnchorAll = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor | |
| [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left | |
| $Form = New-Object Windows.Forms.Form | |
| $Form.Width = 740 | |
| $Form.Height = 490 | |
| $Form.controls.add($Chart) | |
| $Chart.Anchor = $AnchorAll | |
| $Form.Add_Shown({$Form.Activate()}) | |
| [void]$Form.ShowDialog() | |
| #endregion Windows Form to Display Chart | |
| } | |
| #Sample Usage | |
| $results = Test-CollatzConjecture -BeginningNumber 55 | |
| $data = $results | Sort-Object -Descending -Property Iteration | |
| Show-Chart -Title 'Collatz Conjecture Test of 55' -data $data -xvaluesproperty Iteration -yvaluesproperty Result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment