2. number is prime ifand if the number is divisible by no other only itself and
ID: 3821633 • Letter: 2
Question
2. number is prime ifand if the number is divisible by no other only itself and one. number than Write a function called DeterminelfPrime that determines whether a number is prime. Write a sub called Main that determines whether or not the number in each cell in a selected range is prime. Apply a For Each loop and use the func- tion for each cell. If a number is prime, change the background color of the cell and bold the cell. Try to optimize the running time of your program by limiting the number of iterations in a loop that are required to determine if a number is prime.Explanation / Answer
Sub DetermineIfPrime()
Const cellsRange As String ="A1:C8"
Dim bol() As Boolean, m, j&
Dim z#, y&, q
For Each q In Range(cellsRange)
If Not IsNumeric(q.Value) Then Goto finish
If q = "" Then Goto finish
z = q.Value
If Not z = Int(z) Or z = 1 Then Goto finish
m = Int(z ^ 0.5)
Redim bol(2 To m)
For y = 2 To m
If Not bol(y) Then
If z = Int(z / y) * y Then Goto finish
If y <= Int(m ^ 0.5) Then
For j = y To m Step y: bol(j) = True: Next j
End If
End If
Next y
q.Interior.Color = vbCyan
q.Font.Bold = True
finish:
Next q
End Sub
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.