1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VB.netでFizzBuzz

Last updated at Posted at 2015-02-15

#おさらい
Wikipedia
#1 : IF

For i As Integer = 1 To 100
    If i mod 15 = 0 Then
        Console.WriteLine("FizzBuzz")
    Else If i mod 5 = 0 Then
        Console.WriteLine("Buzz")
    Else If i mod 3 = 0 Then
        Console.WriteLine("Fizz")
    Else
        Console.WriteLine(i)
    End If
Next

よくあるパターン

#2 : Select

For i As Integer = 1 To 100
    Select Case 0
        Case i mod 15
             Console.WriteLine("FizzBuzz")
        Case i mod 5
             Console.WriteLine("Buzz")
        Case i mod 3
             Console.WriteLine("Fizz")
        Case Else
             Console.WriteLine(i)
    End Select
Next

C#ではできない書き方です

#3 : IIF関数(C#における三項演算子)

For i As Integer = 1 To 100
    Console.WriteLine(IIF(i mod 15 = 0, "FizzBuzz", IIF(i mod 5 = 0, "Buzz", IIF(i mod 3 = 0, "Fizz", i.ToString())))) 
Next

読みにくい

#4 : Switch関数(F#におけるパターンマッチ(ちょっと大変))

For i As Integer = 1 To 100
    Console.WriteLine(Microsoft.VisualBasic.Switch(i mod 15 = 0, "FizzBuzz", i mod 5 = 0, "Buzz", i mod 3 = 0, "Fizz", True, i.ToString()))
Next

System.Diagnostics.Switchと名前が衝突している…

#5 : LINQ使って(@ishi さん)
クラスまで定義してやるのがいい.

Module Module1
    Private Class BS
        Private _b As Boolean
        Private _s As String

        Public ReadOnly Property B As Boolean
            Get
                Return _b
            End Get
        End Property

        Public ReadOnly Property S As String
            Get
                Return _s
            End Get
        End Property

        Public Sub New(ByVal b As Boolean, ByVal s As String)
            _b = b
            _s = s
        End Sub

    End Class

    Sub Main()
        Dim l As New List(Of BS)

        For i As Integer = 1 To 100
            l.Add(New BS(i Mod 15 = 0, "FizzBuzz"))
            l.Add(New BS(i Mod 5 = 0, "Buzz"))
            l.Add(New BS(i Mod 3 = 0, "Fizz"))
            l.Add(New BS(True, i.ToString()))
            Console.WriteLine(l.Find(Function(s) s.B).S)
            REM Console.WriteLine(l.First(Funtion(s) s.B).S)でも
            l.Clear()
        Next

        l = Nothing
    End Sub

End Module

#6 : フェルマーの小定理を用いたコードゴルフ風(@fujitanozomu さん)

For i As Byte=1To 100
    Dim f=i^2Mod 3,b=i^4Mod 5
    Console.WriteLine("{0:;;Fizz}{1:;;Buzz}{2:#}",f,b,f*b*i)
Next

フェルマーの小定理(pとnは互いに素)

n^{p-1} \equiv 1 \ \ \left(mod\ \ p\right)

を用いています.(fp=3, bp=5
Console.WriteLineの行は,";" section separator, "#" digit place holderを用いています.1
書式指定子に,"A;B;C"とすると,正の数の時にA,負の数の時にB,ゼロでCが表示されます.
";;C"としているので,ゼロの時とき,Cが表示されます.
そして,"#"とすると,ゼロの時は表示されません.
fbiはどれでも割り切れるときは0に,どちらも割り切れないとiになり,望む結果になるわけですね.

###あとがき
もっと変態(?)なコードがあったらコメントで教えて下さい。

  1. https://docs.microsoft.com/ja-jp/dotnet/standard/base-types/custom-numeric-format-strings

1
2
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?