VB程式設計將6 100之間的全部偶數表示為兩個素數之和,在列表

時間 2021-09-01 20:52:08

1樓:匿名使用者

private sub command1_click()dim a as integer, b as integerdim i as integer, j as integerdim k as integer

a = 6

b = 100

list1.clear

for i = a to b step 2for j = 2 to i / 2

if prime(j) and prime(i - j) thenk = k + 1

list1.additem cstr(k) & ": " & cstr(i) & "=" & cstr(j) & "+" & cstr(i - j) '修改這裡的**就行了

end if

next j

next i

label1.caption = "6~100之間有" & cstr(k) & "對素數和"

end sub

'判斷是否為素數

private function prime(n as integer) as boolean

dim i as integer

if n = 1 then

exit function

elseif n = 2 then

prime = true

exit function

elseif n mod 2 = 0 thenexit function

end if

for i = 3 to sqr(n) step 2if n mod i = 0 then exit functionnext i

prime = true

end function

2樓:

此題需要先編寫一個求素數的自定義函式,具體如下:

1.開啟vb,畫一個listbox控制元件,名稱list1,再畫一個label控制元件,名稱label1

2.畫一個command控制元件,名稱command1

3.雙擊command1,開啟**視窗,輸入如下**:

function pdsu(a as integer) as integer

dim b as integer: dim i as integer

if a <= 3 then

pdsu = 1

else

b = a \ 2

for i = 2 to b

if (a mod i) = 0 then

pdsu = 0: exit function

exit for

end if

next i

pdsu = 1

end if

end function

private sub command1_click()

dim m as integer

dim n as integer

dim i as integer

dim s as integer

s = 0

for i = 6 to 100 step 2

for m = 2 to i - 2

n = i - m

if (pdsu(m) = 1) and (pdsu(n) = 1) then

list1.additem str(i) & "=" & str(m) & "+" & str(n): s = s + 1

exit for

end if

next m

next i

label1.caption = "6-100間共有" & str(s) & "對素數和"

end sub

4.如果操作輸入無誤,即可以顯示正常結果,共有48對素數和

說明:解答此題,我用了約10分鐘,給我加分吧

vb的程式設計問題,偶較笨不要罵我

private sub command1 click dim a as integer dim b as integer dim c as integer a val text1.text b val text2.text c val text3.text do while a b c and a ...

VB程式設計 隨機產生10 99之間(含10,99)的整數,找出其中最大 最小的元素

給你基本思路 利用vb內部函式 rnd 產生隨機函式 為0 1之間的隨即數字,轉換成10 99之間的隨機數字 然後利用交換法來找出最大和最小數字,最後輸出就可以了 小李無刀 使用陣列儲存,隨機函式使用,最小值最大值檢索 以下是關鍵 其他的我就不打了 dim i as integer dim max ...

求200 300之間全部素數的和,C語言程式設計

思路 定義一個函式用於判斷該數是否是素數,接著從200到300進行依次判斷該數是否是素數,如果是則累加,最後輸出累加和即可。include int fun int n 判斷該數是否是素數int i for i 2 i 用c語言編一程式,輸出200 300之間的全部素數,求解啊 樓上的樓上演算法可以改...