Skip to content

Instantly share code, notes, and snippets.

@pohatu
Created April 14, 2014 07:22
Show Gist options
  • Save pohatu/10623903 to your computer and use it in GitHub Desktop.
Save pohatu/10623903 to your computer and use it in GitHub Desktop.
some fizz buzz solutions in powershell
function fizzbuzz(){
for ($i=1; $i -lt 101; $i++){
$db=0;
if ($i%3 -eq 0){$db = $db -bor 1}
if ($i%5 -eq 0){$db = $db -bor 2}
switch($db)
{
1 { Write-Output "Fizz"} #01
2 { Write-Output "Buzz"} #10
3 { Write-Output "FizzBuzz"} #11
default {Write-Output $i} #00
}
}
}
function fizzbuzz2(){
$a = 1..101 | % {""}
for ($i=3; $i -le 100; $i +=3){$a[$i]="Fizz"}
for ($i=5; $i -le 100; $i +=5){$a[$i]+="Buzz"}
for ($i=1; $i -le 100; $i++){if ($a[$i] -eq ""){$a[$i]=$i}}
return $a
}
function fizzbuzz3(){
$a = 1..101 | % {""}
$b = 1..101 | % {""}
$d = 1..101 | % {""}
for ($i=3; $i -le 100; $i +=3){$a[$i]="Fizz"}
for ($i=5; $i -le 100; $i +=5){$b[$i]="Buzz"}
for ($i=1; $i -le 100; $i++){
$d[$i] = $a[$i] + $b[$i]
if ($d[$i] -eq ""){$d[$i]=$i}
}
return $d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment