Private Sub CommandButton1_Click()
do
n = InputBox("请输入一个大于1的正整数", "数据输入")
if n>2 then exit do
loop
i = 2
Do
r = n Mod i
i = i + 1
If r = 0 Then
MsgBox (Str(n) + "是质数")
exit sub
end if
if i>=n^0.5 then exit do
Loop
MsgBox (Str(n) + "不是质数")
End Sub
# Q1x = 0.5
n = 0
while ( abs(x - cos(x)) > 0.01) {
x = cos(x)
n = n + 1
}
print(sprintf('%i, x=%.3f, cos(x)=%.3f', n, x, cos(x))
Q2,正好刚刚回答了另一个R的质数的问题(http://zhidao.baidu.com/question/571598738),借过来用一下。这个数比较大,要运行一段时间,别着急。
Eratosthenes <- function(n) {if (n>2) {
sieve <- seq(2,n)
primes <- c()
for ( i in seq(2,n)) {
if (any(sieve == i)) {
primes <- c ( primes, i)
sieve <- c ( sieve[(sieve %% i ) != 0 ], i)
if ((i-2) %in% sieve) {
print(sprintf("%i, %i", (i-2), (i)))
}
}
}
return(primes)
} else {
print ("n > 2")
}
}
primes = Eratosthenes(1000^2)
[1] "3, 5"
[1] "5, 7"
[1] "11, 13"
[1] "17, 19"
[1] "29, 31"
[1] "41, 43"
[1] "59, 61"
[1] "71, 73"
...