キミならどう書く 2.0 ROUND 1 - Excel VBA
お題は「100までの整数から素数を列挙せよ」です.
世のビジネスシーンで最も多用されているアプリケーションの1つであるExcelのVBAマクロで書いてみよう。
Sub PrimeNum()
Dim SUP As Integer: SUP = Sheet1.Cells(1, 1).Value
Const ROW = 2
Dim n, i, col, flag As Integer
col = 2: Sheet1.Cells(ROW, col) = 2
For n = 3 To SUP Step 2
i = 3: flag = 1
While i <= Sqr(n)
If flag > 0 Then
flag = n Mod i
Else
GoTo OUT
End If
i = i + 2
Wend
If flag > 0 Then
col = col + 1: Sheet1.Cells(ROW, col) = n
End If
OUT:
Next n
End Sub