VB求100以內素數的和,用 vb 做 100以內質數求和 的程式

時間 2022-02-09 13:00:04

1樓:匿名使用者

我是vb初學者,難道沒考慮到什麼嗎,為什麼我想的**就這麼點呢private sub command1_click()dim i as integer

dim j as integer

dim s as integer

dim sushu as boolean

s = 0

for j= 2 to 100

sushu = true

for i = 2 to j-1

if j / i = j \ i thensushu = false

『exit for

end if

next i

if sushu then s=s+j

next j

print s

end sub

2樓:匿名使用者

不使用函式的素數和:(在介面新增二個text控制元件即可)

private sub form_load()

dim aa(1 to 100) as integer, bb as integer, cc(1 to 100) as integer, ee as integer, ssh as integer, i as integer, j as integer

aa(1) = 1: aa(2) = 2: bb = 2

for i = 1 to 100

cc(i) = i

for j = 2 to i - 1

if i mod j = 0 then

bb = bb + 1

aa(bb) = i

exit for

end if

next j

next i

for i = 1 to 100

for j = 1 to 100

if cc(i) = aa(j) then cc(i) = 0

next j

next i

for i = 1 to 99

for j = 1 to 100 - i

if cc(j) < cc(j + 1) then

bb = cc(j)

cc(j) = cc(j + 1)

cc(j + 1) = bb

end if

next j

next i

bb = 0

for i = 1 to 100

if cc(i) > 0 then

bb = bb + 1

end if

next i

cc(bb + 1) = 1: cc(bb + 2) = 2

for i = 1 to bb + 1

for j = 1 to bb + 2 - i

if cc(j) > cc(j + 1) then

ee = cc(j)

cc(j) = cc(j + 1)

cc(j + 1) = ee

end if

next j

next i

for i = 1 to bb + 2 '在 text1 中顯示素數

text1.text = text1.text & cstr(cc(i)) & ", "

if i mod 10 = 0 then text1.text = text1.text & vbcrlf

next i

for i = 1 to bb + 2

ssh = ssh + cc(i)

next i

text2.text = ssh '在text2 中顯示素數和

end sub

3樓:小寶

2 function calcprimes(intprimeubound)

3 'intprimeubound 是要計算素數的上限值

4 dim i, j

5 dim strtmp

6 dim bitarray

7 8 redim bitarray(intprimeubound + 1)

9 10 for i = 0 to intprimeubound

11 bitarray(i) = 1

12 next

1314 for i = 2 to cint(sqr(intprimeubound))

15 if 1 = bitarray(i) then

16 j = i

17 while j*i <= intprimeubound

18 bitarray(i*j) = 0

19 j = j + 1

20 wend

21 end if

22 next

23 24 for i = 2 to intprimeubound

25 if 1 = bitarray(i) then

26 strtmp = strtmp & cstr(i) & ","

27 end if

28 next

2930 if strtmp <> "" then

31 strtmp = left(strtmp, len(strtmp) - 1)

32 calcprimes = split(strtmp, ",")

33 else

34 calcprimes = ""

35 end if

3637 end function

前面是行號, 自己去掉, 返回結果是 intprimeubound 以內所有的素陣列成的陣列

用 vb 做 100以內質數求和 的程式

4樓:匿名使用者

dim s, m, i as integers = 5 '後面的只能判斷大於7的數是否是質數,所以s初始值為前兩個質數2和3的和

for i = 7 to 100 step 2 '質數除了2以外都是奇數,所以步長為2

for m = 3 to i - 2 step 2 '原理同上if i mod m = 0 then goto notsign '如果能整除,則不是質數,跳過相加環節

next m

s = s + i

notsign:

next i

print s 's值即為和

5樓:匿名使用者

在窗體上新增個按鈕

private sub command1_click()dim i as integer, s as integer, j as integer, l as integer

for i = 2 to 100

l = 0

for j = 2 to i - 1

if i mod j = 0 then l = 1next j

if l <> 1 then s = s + inext i

print "100以內質數和為" & send sub

6樓:悠悠周郎

fuction issushu(i as integer) as boolean

for j as integer = 2 to cint(sqrt(i)) ' 任意整數如果除以 2 到它的平方根均不能整除則為質數

if i mod j = 0 then exit functionnext

return true ' 因為沒有被整除過,因此返回 trueend function

for i as integer = 1 to 100if issushu(i)=true thensum = sum + i

next i

身在外地,沒有 basic 測試,你試試吧

7樓:匿名使用者

private sub command1_click()for n = 1 to 100

if isprime(n) = true then sum = sum + n

next

print sum

end sub

function isprime(byval n as long) as boolean

dim i as long

for i = 2 to int(n / 2)if n mod i = 0 then isprime = false: exit function

next

isprime = true

end function

8樓:

private sub form_click()dim i, j, n, s as integers = 0

for i = 1 to 100

for j = 2 to i - 1

if i mod j <> 0 then

n = 1

end if

next j

if n = 1 then

s = s + i

end if

next i

print s;

end sub

vb題:程式設計計算100以內素數的和

9樓:匿名使用者

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

for i = 2 to 100

for j = 2 to i

if i mod j = 0 then

exit for

end if

next

if i = j then

k = k + i

end if

next

msgbox k

end sub

10樓:美麗可愛

private sub form_load()

show

dim n, i, k as integer '定義n , i , k為整數型

s = 0                  '賦值s 為0

for n = 1 to 100       '列舉 for迴圈 n 從1到100

k = 0                  '賦值k 為0

for i = 1 to n         '巢狀for迴圈 i為除數

if n mod i = 0 then    '如果 i 能整除n

k = k + 1              '(k為可以整除n的個數) 則k加1

end if

next i                 '結束for迴圈

if k = 2 then          '如果 k等於2 (由於素數只有兩個能夠整除的除數,為素數的特性)

s = s + n              's相當於累加器,將滿足k=2條件的n加起來

end if

next n                 '結束for迴圈

print s                '輸出s

end sub

以上是我的一種列舉的方法,還有多種解法,希望可以幫助你。

要理解這段**並不難,主要理解:

雙重迴圈有這幾特點:

一是:一旦進入內迴圈,要把內迴圈做完才能持行外迴圈**,除非你是用exit for 強制退出

二是:外迴圈做一次,內迴圈要做完,強制性退出(exit for)的除外

用VB編寫簡單的過程,用於求100以內所有奇數的和,通過函式名稱返回

function jiafa n as integer as long for i 1 to n step 2s s i next i jiafa s end function private sub form click he jiafa 100 print he end sub private ...

用VB求1到100奇數和出現的問題

private sub command1 click dim i as integer,s as integeri 1 s 0 while i 99 s s i i i 2 wend msgbox s 1 3 5 99 str s end sub 你應該是用i來控制迴圈,用s來求和,如果你用s i ...

c語言 輸出100以內的素數 break的用法為什麼會有這

安然而止 if i n 的意思是判斷被除數是否小於除數,如果小於當然是不用再進行下去了。這個式子寫成if i n 2 更好,可以少算幾步。 每次從if n i 0 break 迴圈裡逃出來迴圈都能有2中結果,一 被i整除了,直接被break跳出i迴圈,這時程式還會往下跑,就有必要遮擋下對n的列印 我...