通过 VBA 编码写出 1 到 100 之间的素数 [英] Write prime numbers between 1 to 100 through VBA coding
问题描述
我找到了下面的代码,但它不起作用.请分享在VBA中编写质数的更合适的代码
I found the below coding but its not working. Please share the more appropriate code for writing prime numbers in VBA
Private Sub cmdPrime_Click()
Dim p, n, i As Integer
p = 1
Print "Prime Numbers are : "
For n = 1 To 100
For i = 2 To n – 1
If n Mod i = 0 Then
p = 0
Exit For
Else
p = 1
End If
Next
If p = 1 Then
Print n
End If
Next
End Sub
推荐答案
我猜你是从另一种语言翻译的?您确实应该指出哪些线路出现故障以及您研究了什么.
I am guessing you were translating this from another language? You really should have pointed out which lines were failing and what you researched.
"
:智能引号.这是在应用程序之间复制时的典型情况,所以要小心.在visual basic编辑器中使用的语音标记需要""
才能编译.
The ""
: Smart quotes. This is typical of when copying between applications, so be careful. The speech marks for use in the visual basic editor need to be ""
in order to compile.
如果你把 Option Explicit
放在你的代码的顶部,顺便说一下,它会给你很多关于变量声明和拼写的很好的警告.
If you put Option Explicit
at the top of your code it gives you lots of nice warnings about variable declarations and spellings by the way.
你只需要 100 所以 Integer
很好,但是在这种情况下 Integer
没有 Long
的优势,所以使用Long
更安全,以防您将来决定超出 Integer
的容量,然后冒溢出的风险.您还需要在足够高的上限下分解 mod
.
You are only going to 100 so Integer
is fine, but there are no advantages of Integer
over Long
in this instance, so using Long
is safer in case you decide, in the future, to go beyond the capacity of an Integer
, and then you risk overflow. You would also, at a sufficiently high upper limit, need to factor out mod
.
MOD 函数 如果除数(第二个参数在 MOD 函数中),乘以 134,217,728,小于或等于到被评估的数字(MOD 中的第一个参数函数).
The MOD function returns an error if the divisor (the second argument in the MOD function), multiplied by 134,217,728, is less than or equal to the number being evaluated (the first argument in the MOD function).
Microsoft 建议重新工作为 =number-(INT(number/divisor)*divisor)
;我想你可以用 CLng
替换 INT
以保持 Longs.
Microsoft suggest re-working as =number-(INT(number/divisor)*divisor)
; which I guess you could replace INT
with CLng
in to keep with Longs.
Option Explicit
Private Sub cmdPrime_Click()
Dim p As Long, n As Long, i As Long, iCounter As Long
p = 1
With ActiveSheet
.Cells(iCounter + 1, 1) = "Prime Numbers are: " 'Debug.Print "Prime Numbers are: "
For n = 2 To 100 ''< As pointed out 1 is not technically a prime btw so can start at 2
For i = 2 To n - 1
If n Mod i = 0 Then ' If n - (CLng(n / i) * i) = 0 Then
p = 0
Exit For
Else
p = 1
End If
Next
If p = 1 Then
iCounter = iCounter + 1
.Cells(iCounter, 1) = n 'Debug.Print n
End If
Next
End With
End Sub
为将来的读者保留:其他有用的评论来自@ChrisNeilsen.
To preserve for future readers: The additional helpful comments are from @ChrisNeilsen.
要测试n
是否为质数,您只需要测试n
的平方根的可整除性.而且您只需要通过先前检测到的素数来测试可分性.您甚至可以跳过 n
的值.
To test if n
is prime, you only need to test divisability up to square root of n
. And you only need to test for divisibility by previously detected primes. And you can skip even values of n
.
这篇关于通过 VBA 编码写出 1 到 100 之间的素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!