Skip to content

Instantly share code, notes, and snippets.

@LawrenceHwang
Last active September 7, 2019 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LawrenceHwang/39af522593ba9486e09202057f722891 to your computer and use it in GitHub Desktop.
Save LawrenceHwang/39af522593ba9486e09202057f722891 to your computer and use it in GitHub Desktop.
Answer to dfinke@'s Powershell brain candy tweet.
# https://twitter.com/dfinke/status/1167559940598370304
function Get-Exponent {
param(
[int]$Base,
[int]$Power
)
$exponent = [Math]::Pow($Base, $Power)
$exponent
}
function Get-OneSeries {
# This function outputs an integer with the number of 1 specificed by the inputNumber.
# E.g. InputNumber = 3, the output is 111
param (
[int]$NumberofOne
)
$oneSeries = $NumberOfOne- 1
While ($oneSeries -ge 0) {
$oneSeriesResult = $oneSeriesResult + (Get-Exponent -Base 10 -Power $oneSeries)
$oneSeries = $oneSeries - 1
}
$oneSeriesResult
}
function Get-CalculatedFirstPart {
param (
[ValidateScript( { $_ -ge 1 })]
[int]$Number
)
if ($Number -gt 1) {
$result = (Get-CalculatedFirstPart -Number ($Number - 1 ))
}
$result = $result + (Get-OneSeries -NumberOfOne $Number)
$result
}
function Get-BrainCandy {
param (
[ValidateRange(1, 9)]
[int]$Layer = 1
)
for ($l = 1; $l -le $Layer; $l++) {
$firstPart = Get-CalculatedFirstPart -Number $l
$secondPart = $l
$thirdPart = $firstPart * 8 + $secondPart
Write-Host ("{0,$Layer} x `e[31m8`e[39m + $secondPart = $thirdPart" -f $firstPart)
}
}
Get-BrainCandy -layer 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment