注册 登录
编程论坛 VB6论坛

求帮忙写代码,需要各位大神学习!

q23740240 发布于 2015-04-09 18:11, 344 次点击
找出100以内的勾股数。所谓勾股数就是3个正整数满足表达式a^2+b^2=c^2
2 回复
#2
风吹过b2015-04-09 19:21
for a=1 to 97
  for b=a+1 to 98
    for c=b+1 to 99
      if a^2+b^2=c^2 then
         ? a;b;c;"是一组勾股数"
      end if
    next c
  next b
next a

浏览器写的代码,未经任何优化。
优化方向:
1、尽量缩小a,b 的范围, 确保 a^2+b^2 大于10000的无效循环尽量的少。
2、测试是 开平方根快,还是计算平方快。
3、测试是把 平方计算结果 保存起来,使用时直接查表快,还是 每次计算快。

[ 本帖最后由 风吹过b 于 2015-4-9 19:22 编辑 ]
#3
xzlxzlxzl2015-04-09 20:27
'试试下面的代码
Dim a as integer,b as integer,c as integer
a=1:b=2:c=3
while c<100
  if a^2+b^=c^2 then print a,b,c
  a=a+1
  if a>99 then
    a=1
    b=b+1
  end if
  if b>99 then
    b=a+1
    c=c+1
  end if
wend
1