编写程序验证哥德巴赫猜想:一个不小于6的偶数可以表示为俩个素数之和。
答案:2 悬赏:20
解决时间 2021-11-17 05:33
- 提问者网友:恋你成殇
- 2021-11-16 14:22
编写程序验证哥德巴赫猜想:一个不小于6的偶数可以表示为俩个素数之和。
最佳答案
- 二级知识专家网友:猖狂的痴情人
- 2021-11-16 14:51
1.设计一个判断素数的函数private bool Prime(int x),当输入值为素数时返回true,反之为false;
2.用一个2层for循环,第一层是6~某一个比较大的偶数n(自己设定。不见得用无限循环吧?)的递增2循环;第二层是3~n/2的奇数a的递增2循环,用函数Prime判断a和n-a是否都返回true,都返回true的时候就break退出第二层for循环;
3.可以在每一个n的代码里面添加一个输出显示代码,把6~n间的所有偶数表示为n=a+(n-a)的形式。
2.用一个2层for循环,第一层是6~某一个比较大的偶数n(自己设定。不见得用无限循环吧?)的递增2循环;第二层是3~n/2的奇数a的递增2循环,用函数Prime判断a和n-a是否都返回true,都返回true的时候就break退出第二层for循环;
3.可以在每一个n的代码里面添加一个输出显示代码,把6~n间的所有偶数表示为n=a+(n-a)的形式。
全部回答
- 1楼网友:高冷不撩人
- 2021-11-16 15:23
放置一个command1,一个text1,不用再做其它设定,程序会自行设定各个参数,代码如下:
private sub command1_click()
dim n as long, i as long, j as long
if isnumeric(text1.text) then
n = clng(text1.text)
if n mod 2 = 0 and n >= 6 then
for i = 2 to n \ 2
if i = 2 or pd(i) then
j = n - i
if pd(j) then
label1.caption = "结果: " & n & "=" & i & "+" & j & "符合!"
exit sub
end if
end if
next
label1.caption = "结果: 不符合!"
else
text1.text = "请输入大于或等于6的偶数!"
end if
else
text1.text = "请输入数字!"
end if
end sub
private sub form_click()
cls
print "双击退出"
text1.visible = true
label1.visible = true
command1.visible = true
end sub
private sub form_dblclick()
end
end sub
private sub form_load()
form1.caption = "哥德巴赫猜想"
form1.width = 5000
form1.height = 5000
form1.show
form1.autoredraw = true
text1.text = "在此输入一个大于等于6的偶数"
text1.forecolor = &h808080
text1.top = 500
text1.left = 200
text1.width = 3000
text1.height = 300
text1.visible = false
label1.caption = ""
label1.top = 1000
label1.left = 200
label1.width = 3000
label1.height = 300
label1.visible = false
command1.caption = "验证"
command1.top = 2000
command1.left = 200
command1.width = 800
command1.height = 300
command1.visible = false
print "1942年德国数学家哥德巴赫给数学家欧拉的一封信中,"
print "提出了把一个整数表示成两个素数之和的推测,"
print "即一个充分大的偶数(大于等于6),"
print "总可以分解为两个素数之和,"
print "编写visual basic程序来验证这一猜想"
print "单击开始......"
end sub
private sub text1_gotfocus()
text1.text = ""
text1.forecolor = &h0
command1.visible = true
end sub
function pd(a as long) as boolean
dim b as long
for b = 2 to a - 1
doevents
if a mod b = 0 then
pd = false
exit for
else
pd = true
end if
next
end function
'已经运行过.
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯