找回密码
 加入
搜索
楼主: pusofalse

[效率算法] 练习004 - 计算100以内的勾股数

 火... [复制链接]
发表于 2010-3-3 13:57:20 | 显示全部楼层
本帖最后由 auto 于 2010-3-3 20:20 编辑

$begin = TimerInit()
local $stmp[30]
$an = 30
for $a = 1 to $an
        $stmp[$a-1] = (2*$a+1)^2
Next
$i = 0
$t = 100 ;$T为需要计算的数值 5<=t<=10000
$str = ""

for $P =1 to $t-1 Step 2
        For $Q =2 to $t  Step 2
                $PP = $P^2
                $QQ = $Q^2
                $z= $PP+ $QQ/2 + $P*$Q
                if $z > $t Then ExitLoop
                $x = $PP + $P*$Q
                $y = $QQ/2 + $P*$Q
                $n = 1
                While $n*$z <=$t
                        $j = 0
                        for $a = 0 to $an-1
                                if $n >= $stmp[$a] and Mod($n,$stmp[$a]) = 0  Then
                                        $j=1
                                        ExitLoop
                                EndIf                                       
                        Next
                        if $j then
                        Else                               
                                $str &= $n&@TAB&$n*$x &@TAB &$n*$y&@TAB &$n*$z&@CR
                                $i += 1
                        EndIf                       
                        $n += 1
                WEnd               
        Next
Next

MsgBox(1,"共找到"&$i&"符合要求的数据 | 用时"&TimerDiff($begin),"点击确定复制到粘贴板");不输出$str
ClipPut($str)

;其中用到了求mod函数,使得效率更高一些10000以内数字基本上都在1-2秒内就可以算出

评分

参与人数 2金钱 +70 收起 理由
rolaka + 20 用位移可以更快...
afan + 50 强悍

查看全部评分

发表于 2010-3-3 19:56:54 | 显示全部楼层
網路找的.哪位高手改寫一下

《原始畢氏三元數之生成定理》

設(x,y,z)為畢氏三元數且y為偶數,則(x,y,z)為原始的畢氏三元數之充要條件為存在互質的自然數m與n,一奇一偶,且m>n,使得:

x=m*m-n*n, y=2m*n, z=m*m+n*n

以上就是我要寫程式所用的定理。

=====程式開始=====

#include <iostream>

#include <iomanip>

#include <cstdlib>



using namespace std;



int GCD(int,int);//傳回最大公因數值

int main()

{

int m,n,x,y,z;

cout << "小於100的畢氏三元數:" << endl;

for(m=2;m<=9;m++)

{

for(n=m-1;n>0;n-=2)

{

if(GCD(m,n)==1)

{

x=m*m-n*n;

y=2*m*n;

z=m*m+n*n;

if(z<=100)

cout << x << "\t" << y << "\t" << z << endl;

}

}

}

system("pause");//程式暫停

return 0;

}

int GCD(int m,int n)

{

if(m%n==0)

return n;

else

return GCD(n,m%n);

}

=====程式結束=====

ps. 數學真是太偉大了
參考資料 蔡聰明著《數學拾貝》
发表于 2010-3-3 21:54:03 | 显示全部楼层
;~ int GCD(int,int);//傳回最大公因數值
;~ int main()
;~ {
;~ int m,n,x,y,z;
;~ cout << "小於100的畢氏三元數:" << endl;
;~ for(m=2;m<=9;m++)
$time = TimerInit()
$r = ""
;~ {
For $m = 2 To 9
;~ for(n=m-1;n>0;n-=2)
;~ {
        $n = $m - 1
        While $n > 0
;~ if(GCD(m,n)==1)
;~ {
                If _GCD($m, $n) == 1 Then
;~ x=m*m-n*n;
                        $x = $m ^ 2 - $n ^ 2
;~ y=2*m*n;
                        $y = 2 * $m * $n
;~ z=m*m+n*n;
                        $z = $m ^ 2 + $n ^ 2
;~ if(z<=100)
                        If $z <= 100 Then
;~ cout << x << "\t" << y << "\t" << z << endl;
                                $r &= $x & ' ' & $y & ' ' & $z & @CRLF
;~ }
                        EndIf
;~ }
                EndIf
;~ }
                $n -= 2
        WEnd
Next
;~ system("pause");//程式暫停
;~ return 0;
MsgBox(0, TimerDiff($time), $r)
;~ }
;~ int GCD(int m,int n)
;~ {
Func _GCD($m, $n)
        While Mod($m, $n) <> 0
                $temp = $m
                $m = $n
                $n = Mod($temp, $n)
        WEnd
        Return $n
EndFunc   ;==>_GCD
结果返回不正常....
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-19 20:26 , Processed in 0.073179 second(s), 18 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表