DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: webb123
今日帖子: 34
在线用户: 20
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 15:13:32
标题:
我想用对象数组解决一个数学问题,但对象数组用起来有问题,请教一下,谢谢 浏览:4008
加入我的收藏
楼主: 题目是: 一个节点,2天后繁殖第一个节点,然后每天繁殖一个;新繁殖出来的节点也是2天后,繁殖第一个,然后每个一个。要统计几天后,一共多少个节点。我的想法是定义一个对象,属性有 年龄,然后还有一个方法(当前天数-自己的年龄》2 就往对象数组中实例化一个对象)。最后我只要数数组的大小即可。

请问有高人帮我做一个对象不?谢谢
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 15:59:29
1楼: 对象数组要怎么做?
谢谢
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 17:32:59
2楼: type
  TMyNode=Class(TObject)
Private
  _birthday:integer;
  function get_bir:integer;
  procedure set_bir(birthday:integer);
public
  property birday:integer read get_bir write set_bir;
  function mycheck(currday:integer):TmyNode;
end;
定义一个对象

利用对象的方法,判断当前对象的年龄,如果年龄大于2,则创建一个对象
function TMyNode.mycheck(currday:integer):TmyNode;
var
  myNodeChild:TmyNode;
begin
  if currday-_birthday>2 then begin
     myNodeChild:= Tmynode.create;
     myNodeChild.set_bir(currday);
     result:=myNodeChild;
  end
  else begin
    result:=nil;
  end;
end;

结果不能访问 _birthday,请问这块要怎么写?请高人指导,谢谢
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 18:01:27
3楼: 结果是出来了,但又出来一个新问题,数组太大,放不下,它的结果是20亿。
请问如何在Delphi中定义和使用这么大的数组?
谢谢
----------------------------------------------
-
作者:
男 wk_knife (wk_knife) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 18:09:21
4楼: 难道这不是个数学题么?用公式就能计算出有多少个结果。一个函数就应该搞定的?!
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 18:50:28
5楼: function DoMath(Days: Integer): Integer;
var
  D: Integer;
begin
  Result := 1;
  for D := 3 to Days do
  begin
    Result := Result + DoMath(Days - D);
  end;
end;

这应该是 小学升初中的数学题吧。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 19:30:50
6楼: 这个办法我也想到过,少算了后期 节点每天复制一个的部分。

超大数组在Delphi中如何处理?
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 21:57:00
7楼: 3 = 2
6 = 6
7 = 9
10 = 28
20 = 1278
50 = 122106097
100 = ?

你的 2 天后,应该是 从第三天开始吧。
别告诉我是从第二天开始。
此帖子包含附件:wang_80919_2019210215815.zip 大小:862.4K
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 21:58:06
7楼: 动态内存申请可以大很多,可是这程序还是编得太烂,我16GB的本子都没有算出结果来。

高手来支招吧,我算到了44天,45天就是算不出来,45天的答案大约是11亿多。
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 21:59:03
8楼: 44天 是7亿多
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 21:59:41
8楼: 44 = 12333413
45 = 18059374

把数据类型,改成 UInt64 看看。

算了,你说是多少,就是多少吧。
我的数学,也就是 小学生水平。理解不了你的高级算法。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 22:13:30
9楼: 不是整形数的问题,就是我的对象数组 太占内存。
我把对象中的integer都改成 smallint了,还是有不行。计算过程中经常达到98%内存占用。

其实 Flying wang的水平还是挺好的了。
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 22:14:47
10楼: 45天 11亿多是标准答案,不是我说的。
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:20:51
9楼: 改了一下算法。
100 很快得到结果 24382819596721629
此帖子包含附件:wang_80919_2019210222050.zip 大小:862.6K
----------------------------------------------
(C)(P)Flying Wang
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:27:14
11楼: 我知道哪里写错了。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 luchao900 (哈你宝贝) ▲▲▲▲△ -
普通会员
2019/2/10 22:35:26
12楼: 用python,写个回调,不论多长都能算出来

这个问题好像是奥数里面母兔子生小兔子的问题
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:36:09
13楼: function DoMath(Days: Integer): UInt64;
var
  D: Integer;
begin
  Result := 1;
  for D := 3 to Days do
  begin
    Result := Result + DoMath(D - 2);
  end;
end;
此帖子包含附件:wang_80919_2019210223632.zip 大小:862.6K
----------------------------------------------
(C)(P)Flying Wang
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:38:42
14楼: 新的 附件 计算结果
44 = 701408733
45 = 1134903170

我小学数学,之所以不能考 100 分,就是因为经常性的 粗心大意。

正确答案和错误答案之间,差距很小。但是结果的差距很大。

这就是 谬之毫厘差之千里 。
顺便复习了一下 小学语文。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:39:41
15楼: 100 = 3736710778780434371
秒出结果。

当然,上面的代码 会卡死的。
秒出的代码 过几个月再公布(如果我还记得的话)。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 22:46:04
12楼: 1        -----1
2          1
3          2
4          3
5          5
6          8
7          13
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:47:19
16楼: 你自己去运行 最新 附件 吧。
8 是 21

13 楼 有代码 有 附件。

爱信不信。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 22:47:47
16楼: 说说实现方法吧。我也学习一下
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:50:38
17楼: 复制粘贴都不会了?

13 楼 有代码 有 附件。

爱信不信。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 22:53:51
18楼: python 能超过 Int64 吗?
delphi 目前好像还不能自动计算 超过 64 BIT 的数。
范围是 0..18446744073709551615
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 22:57:24
19楼: 没有刷新屏幕,所以开始没有看到。谢谢 Flaying wang
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/10 22:59:22
20楼: 最新的算法结果是对的
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/10 23:01:02
21楼: 计算 500000 的时候 卡死了。
反正 200000 的时候 等一等还是能出结果的。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 scarlette (Scarlette) ★☆☆☆☆ -
普通会员
2019/2/11 0:53:19
22楼: 一个Fibonacci数列搞那么复杂?…
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/11 1:38:49
23楼: 是啊 根据 这个 算法
F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=3,n∈N*)
也能写出代码来。

原来,小学生,就学这么高级的东西啊。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/11 11:41:39
24楼: 学习了。果然都是高人。谢谢 scarletter and Flying Wang
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/11 13:41:46
25楼: function DoMath(Days: SmallInt): UInt64;
var
  I, CntDay0, CntDay1, CntDay2, CntTmp2: UInt64;
begin
  CntDay0 := 0;
  CntDay1 := 0;
  CntDay2 := 1;
  for I := 1 to Days do
  begin
    CntTmp2 := CntDay2;
    CntDay2 := CntDay0 + CntDay1;
    CntDay0 := CntDay0 + CntDay1;
    CntDay1 := CntTmp2;
  end;
  Result := CntDay0 + CntDay1 + CntDay2;
end;

计算DoMath(100)只需要0.4毫秒,计算结果是1298777728820984005,计算过程已经出现大于64bit整数导致溢出,因此这个其实是的不正确的结果
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/11 13:51:18
26楼: 0 --> 1
1 --> 1
2 --> 2
3 --> 3
4 --> 5
5 --> 8
6 --> 13
7 --> 21
8 --> 34
9 --> 55
10 --> 89
11 --> 144
12 --> 233
13 --> 377
14 --> 610
15 --> 987
16 --> 1597
17 --> 2584
18 --> 4181
19 --> 6765
20 --> 10946
21 --> 17711
22 --> 28657
23 --> 46368
24 --> 75025
25 --> 121393
26 --> 196418
27 --> 317811
28 --> 514229
29 --> 832040
30 --> 1346269
31 --> 2178309
32 --> 3524578
33 --> 5702887
34 --> 9227465
35 --> 14930352
36 --> 24157817
37 --> 39088169
38 --> 63245986
39 --> 102334155
40 --> 165580141
41 --> 267914296
42 --> 433494437
43 --> 701408733
44 --> 1134903170
45 --> 1836311903
46 --> 2971215073
47 --> 4807526976
48 --> 7778742049
49 --> 12586269025
50 --> 20365011074
51 --> 32951280099
52 --> 53316291173
53 --> 86267571272
54 --> 139583862445
55 --> 225851433717
56 --> 365435296162
57 --> 591286729879
58 --> 956722026041
59 --> 1548008755920
60 --> 2504730781961
61 --> 4052739537881
62 --> 6557470319842
63 --> 10610209857723
64 --> 17167680177565
65 --> 27777890035288
66 --> 44945570212853
67 --> 72723460248141
68 --> 117669030460994
69 --> 190392490709135
70 --> 308061521170129
71 --> 498454011879264
72 --> 806515533049393
73 --> 1304969544928657
74 --> 2111485077978050
75 --> 3416454622906707
76 --> 5527939700884757
77 --> 8944394323791464
78 --> 14472334024676221
79 --> 23416728348467685
80 --> 37889062373143906
81 --> 61305790721611591
82 --> 99194853094755497
83 --> 160500643816367088
84 --> 259695496911122585
85 --> 420196140727489673
86 --> 679891637638612258
87 --> 1100087778366101931
88 --> 1779979416004714189
89 --> 2880067194370816120
90 --> 4660046610375530309
91 --> 7540113804746346429
92 --> 12200160415121876738
93 --> 1293530146158671551
94 --> 13493690561280548289
95 --> 14787220707439219840
96 --> 9834167195010216513
97 --> 6174643828739884737
98 --> 16008811023750101250
99 --> 3736710778780434371
100 --> 1298777728820984005
----------------------------------------------
-
作者:
男 lps (lps) ★☆☆☆☆ -
盒子活跃会员
2019/2/11 14:06:25
27楼: 这个办法?!
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/11 14:42:25
28楼: 更正一下:计算DoMath(100)只需要0.4微秒,也就是1/2,500,000秒。
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/11 17:38:05
29楼: 强。我争取再去找几道题目来满足一下大家的编程欲望
----------------------------------------------
-
作者:
男 vkow (vkow) ★☆☆☆☆ -
普通会员
2019/2/11 17:58:22
30楼: 看来坛子里面没有做财务的人啊。

建议改用Extended类型。这个可以表达物理世界的任何数。
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/11 19:28:05
30楼: 正确值:
90 --> 4660046610375530309
91 --> 7540113804746346429
92 --> 12200160415121876738
93 --> 32233441537043572922
94 --> 53753695172916510279
95 --> 89160210806384372338
96 --> 130614181029918102484
97 --> 219774391836302474822
98 --> 350388572866220577306
99 --> 570362093771044858923
100 --> 905277867591458366958
101 --> 1475639961362503225881
102 --> 2381116958022483399636
103 --> 3872229718430793694788
104 --> 6253346676453277094424
105 --> 10125775523952592596011
106 --> 16363649401360062621164
107 --> 26489424925312655217175
108 --> 42853273455741239645140
109 --> 69358171180099701931586
110 --> 112211444635840941576726
111 --> 181569814945009165315115
112 --> 293765786781804299822570
113 --> 475335601726813465137685
114 --> 769101587637686286767060
115 --> 1244452662163545558974016
116 --> 2013554249801231845741076
117 --> 3258007111093845926521899
118 --> 5271545888096031965193704
119 --> 8529552999189877891715603
120 --> 13801099086414978378716116
121 --> 22330667558403902077500990
122 --> 36131766644818880456217106
123 --> 58462434402351851055524907
124 --> 94594185574371685704672742
125 --> 153056619976723536760197649
126 --> 247650805750224290986677204
127 --> 400707441199746873553944124
128 --> 648358246949971164540621328
129 --> 1049065688348847106616372267
130 --> 1697423919826019225349924324
131 --> 2746489608174866331966296591
132 --> 4443913528200014625838027732
133 --> 7190403151847680003611393594
134 --> 11634316680047694629449421326
135 --> 18824719832094503701582621739
136 --> 30459036496669399285224973794
137 --> 49283756328763902986807595533
138 --> 79742792825632431340554376148
139 --> 129026549169869133373169040952
140 --> 208769341995501564713723417100
141 --> 337795891165569827155414264875
142 --> 546565233145598592823330612704
143 --> 884361124311168419978744877579
144 --> 1430926357456966141870597297108
145 --> 2315287481783607360895149243958
146 --> 3746213839240573502765746541066
147 --> 6061501321024379992729417591851
148 --> 9807715160249480696449357063646
149 --> 15869216481273860689178774655497
150 --> 25676931641523540514696653525972
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/11 21:00:46
31楼: 93    12200160415121876738
94    19740274219868223167
95    31940434634990099905
96    51680708854858323072
97    83621143489848422977

这是我的计算结果,楼上92以后就与我算的不同了。
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/11 21:03:48
32楼: 160   1226132595394188293000174702095995
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/11 21:36:59
33楼: 把你的代码贴出来看看
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/11 23:37:36
34楼: 13 楼 和 23 楼都有算法。
计算结果都是一样的。
不过你们大概是不相信的。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 bmsr (白忙剩人) ★☆☆☆☆ -
普通会员
2019/2/12 0:00:02
35楼: 超大数处理
http://blog.sina.com.cn/s/blog_53866d7501015suw.html
http://blog.sina.com.cn/s/blog_53866d7501016d6p.html
----------------------------------------------
http://blog.sina.com.cn/bmsrnote
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/12 9:01:03
36楼: 上次提供的数据确实有误,更正后的数据:
90 --> 2880067194370816120
91 --> 4660046610375530309
92 --> 7540113804746346429
93 --> 12200160415121876738
94 --> 19740274219868223167
95 --> 31940434634990099905
96 --> 51680708854858323072
97 --> 83621143489848422977
98 --> 135301852344706746049
99 --> 218922995834555169026
100 --> 354224848179261915075
----------------------------------------------
-
作者:
男 wang_80919 (Flying Wang) ★☆☆☆☆ -
普通会员
2019/2/12 11:22:23
37楼: 大家的数学都是 检查之后就能改正 说明 粗心大意 啊。
----------------------------------------------
(C)(P)Flying Wang
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/12 12:10:02
38楼: 用8192bit整数算出来的结果:
***[1] --> 1
***[2] --> 1
***[3] --> 2
***[4] --> 3
***[5] --> 5
***[6] --> 8
***[7] --> 13
***[8] --> 21
***[9] --> 34
***[10] --> 55
***[20] --> 6765
***[30] --> 832040
***[40] --> 102334155
***[50] --> 12586269025
***[60] --> 1548008755920
***[70] --> 190392490709135
***[80] --> 23416728348467685
***[90] --> 2880067194370816120
***[100] --> 354224848179261915075
***[200] --> 280571172992510140037611932413038677189525
***[300] --> 222232244629420445529739893461909967206666939096499764990979600
***[400] --> 176023680645013966468226945392411250770384383304492191886725992896575345044216019675
***[500] --> 139423224561697880139724382870407283950070256587697307264108962948325571622863290691557658876222521294125
***[600] --> 110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200
***[700] --> 87470814955752846203978413017571327342367240967697381074230432592527501911290377655628227150878427331693193369109193672330777527943718169105124275
***[800] --> 69283081864224717136290077681328518273399124385204820718966040597691435587278383112277161967532530675374170857404743017623467220361778016172106855838975759985190398725
***[900] --> 54877108839480000051413673948383714443800519309123592724494953427039811201064341234954387521525390615504949092187441218246679104731442473022013980160407007017175697317900483275246652938800
***[1000] --> 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
***[2000] --> 4224696333392304878706725602341482782579852840250681098010280137314308584370130707224123599639141511088446087538909603607640194711643596029271983312598737326253555802606991585915229492453904998722256795316982874482472992263901833716778060607011615497886719879858311468870876264597369086722884023654422295243347964480139515349562972087652656069529806499841977448720155612802665404554171717881930324025204312082516817125
***[3000] --> 410615886307971260333568378719267105220125108637369252408885430926905584274113403731330491660850044560830036835706942274588569362145476502674373045446852160486606292497360503469773453733196887405847255290082049086907512622059054542195889758031109222670849274793859539133318371244795543147611073276240066737934085191731810993201706776838934766764778739502174470268627820918553842225858306408301661862900358266857238210235802504351951472997919676524004784236376453347268364152648346245840573214241419937917242918602639810097866942392015404620153818671425739835074851396421139982713640679581178458198658692285968043243656709796000
***[4000] --> 39909473435004422792081248094960912600792570982820257852628876326523051818641373433549136769424132442293969306537520118273879628025443235370362250955435654171592897966790864814458223141914272590897468472180370639695334449662650312874735560926298246249404168309064214351044459077749425236777660809226095151852052781352975449482565838369809183771787439660825140502824343131911711296392457138867486593923544177893735428602238212249156564631452507658603400012003685322984838488962351492632577755354452904049241294565662519417235020049873873878602731379207893212335423484873469083054556329894167262818692599815209582517277965059068235543139459375028276851221435815957374273143824422909416395375178739268544368126894240979135322176080374780998010657710775625856041594078495411724236560242597759185543824798332467919613598667003025993715274875
***[5000] --> 3878968454388325633701916308325905312082127714646245106160597214895550139044037097010822916462210669479293452858882973813483102008954982940361430156911478938364216563944106910214505634133706558656238254656700712525929903854933813928836378347518908762970712033337052923107693008518093849801803847813996748881765554653788291644268912980384613778969021502293082475666346224923071883324803280375039130352903304505842701147635242270210934637699104006714174883298422891491273104054328753298044273676822977244987749874555691907703880637046832794811358973739993110106219308149018570815397854379195305617510761053075688783766033667355445258844886241619210553457493675897849027988234351023599844663934853256411952221859563060475364645470760330902420806382584929156452876291575759142343809142302917491088984155209854432486594079793571316841692868039545309545388698114665082066862897420639323438488465240988742395873801976993820317174208932265468879364002630797780058759129671389634214252579116872755600360311370547754724604639987588046985178408674382863125
***[6000] --> 377013149387799945333900224880775791236117337930100445535490043525045843539648818562578569150145472631258140598040875416901451349878686710189798735122954887088454924500028574003068434783807870575430696066439101846750599745829545721693784957073273631818673420751972327938538799801845560513422262958882244516053880771140410978978788942838058153862334417256446852231577242261307228498349256708182071208335817888584565766108075330781175405119280446072770439376209526520001751104214901240319492664624851096737783185573483303395157215338535471110527043630771804782569715147171393026481175856948947183228899948494260568208750719855268516476269647878714108168277444438804007613683373880351707165423524328506540659186023484368510133227968867699680214790847184799670735773757954844122828271297646621289176554762574427786113873868257758061282170336001273324376526581229870956568892731479303850890073064863486442100376745831539591175879381143478831554126208085279063513741706269326525112915577755575469466383184602256762543155745636219359236200272816189871334546629683994275939790168821520825923115371290776425534079797676308190573421064864056358117445863611087717811034416870861710009856863627270553744159738699063771087537466728943484891875282416138475233419592000
***[7000] --> 36643483050372328322763589672816049218571543934175989626270698720728011459961452615205304474088508634285133393772080143860609858437637289909505603382510796045818812761764843963097882756899306880632339149624457792521065549662450746982954629516070098764978344151183599533003076277908774345939181724390901980527597663311555613033194153844866587511336793498907902783405698117902719459066855627353047337434107530829780633602911908426389755252823713762551462513907377077479794770248229483843646633681833549215123470585482715472809087383941758904346522640847918233307726932886610834511442709077969599000511722444264347175538294548185363876202654698511562719377096542631053943527028083011034249574050544328985535168955291671366036244479158743237803279520401188053252788470288847800035137267512317663342926011439333402801452136904199295820198703432837127533865033077917441010089802284149246910370407338605473066356582210888458052852205569170152356850628426912688415908336529818984499627245623589210650557134791498344835054794775623211187554679169981434112608914574843324668332643775010924705690019102274575145613483922681596385654759233269444264241619526680808134173728034947587278323338845059843941403722357555875001230291335579485064878430855934357730321907669366710759730431158502094644082670389464425638942201877393180553647515377317433628216098889451332718516720207276058883659104584529812229076091130643430114436305262385420314059450210042195431947096493318081320875
***[8000] --> 3561533204460626739768914905427460387141369539110154082973500638991885819498711815304829246223963373749873423083216889782034228521693267175594214186111978816819236959743284321273097535654614718808050244321699002512466203835566030351092652496815708455980825654877181538741827129421689128991879649533246136168998590044965735035810856774605383628378979290580539135791985063484992877932473487054068899476937399295193905527420792975902913836012199062687063537510151753758100626402591751183925883151617648375005313453493271681248233059858496951790113255897429539560654496639601132039360167542277472498901884679404509894269174519328918160745655327632006736189766801968534195725815421784083495026969542066047758885029695257263330719223956309043195653930347983496830801755572982419821881275569179922973415736010289561700699477021488635509784509168019589640190234350021673802856836365767446249424907273016689053388000785637444921523414602360860001530139933615215383220927084750528293779491002813557093860863839463287251443115581618266959802005566973874793475256663122039030056061200186123236430592279484254766158650545069933528061680141046574115103014532101595841822474764213889385114174543352137856680694687244097968099924183815689652779302937329729253678579649215884078334428338037327451220722810587680172255878795449524781554973097109174140632623167659027450550461045055883872225659796812847075286475208205923875668405160707778568995306926178023176315799965539425437791083258303238592641010878264249883586034912756021070468742995902773902487497010335873840408520900059054071283266816325489230566003110549946685475230821114509971542662742044237174282248020953398789607528748909125
***[9000] --> 346160291286684746313289272940653195821004938840574649197792354882626761451249209476688158830845438584081783077248969641036805567685925275096026379667053520731117017902152350004056055263290740468312565519013758527115134986679500927618960595898784125436743322890902556558480966166842068858827121368084037500398689666218044551350360442646862696279483129043660912539980584260616428129687853518243358544506024804211410314325692133398509706576596745187284000675448960566587838629300214068490071678112576561242120168921521062906855593650494011391615760506296564726181583639595605639315514462323593255726350681177298919692687529131475966996479175735276726414564038873951028105275764984412066591637415934772279139455253087288034009154993561193308269619883651016764739580528775882659332284075160900251889565909001620005955898020498741253426259183467407856451935255744598735677063143605951074135853992415938277666700535092854704966755528337679057697026202081967337389133151551900992563506110333584028337592625787023110008351289364452178195306107056259768301902940710358705695173401175797592063874235214258319794748258830812224447883759078743189231351248058057742818858083652751310389157188583652487191753681908871324452598859106144951594606797195786754559141129782727790928218793262000601818878041232712620482141748965030717911922256234544033929372196985105014830841771293541916105669494308393067698504542986022441130160781328108216014215621166500631310190198456977407701085658980743316402172456165191576635035885104241740897029972735571913355609051794588175072614073380279274977928339443177725603335698391914343325635814053921690136602830888930385122496970174170881935462763316271613842866892088329627160809282789447665993788904319486227140536689107577971883772835153070545678359828373104418268785375257487856252266909918112168299229852218195022374231040315139836301079422737682650129388000
***[10000] --> 33644764876431783266621612005107543310302148460680063906564769974680081442166662368155595513633734025582065332680836159373734790483865268263040892463056431887354544369559827491606602099884183933864652731300088830269235673613135117579297437854413752130520504347701602264758318906527890855154366159582987279682987510631200575428783453215515103870818298969791613127856265033195487140214287532698187962046936097879900350962302291026368131493195275630227837628441540360584402572114334961180023091208287046088923962328835461505776583271252546093591128203925285393434620904245248929403901706233888991085841065183173360437470737908552631764325733993712871937587746897479926305837065742830161637408969178426378624212835258112820516370298089332099905707920064367426202389783111470054074998459250360633560933883831923386783056136435351892133279732908133732642652633989763922723407882928177953580570993691049175470808931841056146322338217465637321248226383092103297701648054726243842374862411453093812206564914032751086643394517512161526545361333111314042436854805106765843493523836959653428071768775328348234345557366719731392746273629108210679280784718035329131176778924659089938635459327894523777674406192240337638674004021330343297496902028328145933418826817683893072003634795623117103101291953169794607632737589253530772552375943788434504067715555779056450443016640119462580972216729758615026968443146952034614932291105970676243268515992834709891284706740862008587135016260312071903172086094081298321581077282076353186624611278245537208532365305775956430072517744315051539600905168603220349163222640885248852433158051534849622434848299380905070483482449327453732624567755879089187190803662058009594743150052402532709746995318770724376825907419939632265984147498193609285223945039707165443156421328157688908058783183404917434556270520223564846495196112460268313970975069382648706613264507665074611512677522748621598642530711298441182622661057163515069260029861704945425047491378115154139941550671256271197133252763631939606902895650288268608362241082050562430701794976171121233066073310059947366875
----------------------------------------------
-
作者:
男 wk_knife (wk_knife) ★☆☆☆☆ -
盒子活跃会员
2019/2/12 12:16:11
39楼: 你们都是过年闲得吧!
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/12 12:27:39
40楼: 有点忙,但还能抽空
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/12 14:59:45
41楼: 我用的是cnpack的超大数据。@bluestorm8

刚看到,对不起
----------------------------------------------
-
作者:
男 luchao900 (哈你宝贝) ▲▲▲▲△ -
普通会员
2019/2/12 15:17:22
41楼: wang_80919

我用python测试 阶乘回调 ,回调深度 994 就崩了
结果很长
993     41094550152463707882643359706479714100653648481957742920230734403007949783827424700508760160783665066556496369796368412759294613889016590873277970150845487298399291434554244749035545893320864717158024071203880058200537684026096218297707288997197013835667357766210302984207197644513341307594464711728415130348167271010608093133244150465522321811685724118639319008549029754370573446216074878275583576643738093441843763702814070441023918017818834659812188312978812954529320816025098758823083235484026479844738330233445537220522592228982958851955120005087442338425303153332394679177612847010697389809177188881268002872716215806214315708365914046317211129095044841344438702094024336092441448059514822655228783572664718083465069170001421083854231677811062370146932379497394249219973212394928212222881888040050763475324144752816654627577870020839253013886495049455048630888586397966169681266522184111950824443472924881515159205715906266627395540238928089044481474364665988553584288634837740713610288429802630883084653093460045493621927932086967820984214638678243723272738475581368293184255509340583187468479979440857683863571660948208011244212102082493493833362999968877386081047258260026271540448070646620665786823786599537352383760815553613626028162892719779384706214047527454174033210703917725941514020256094434641833679745540427089604811886805015943681132324872061231869016681390833114207029010669956489697406311292065465447132455550827177766793018508777075927220539116899954640534695858446258479069732365916672654408479992978791102212398531972346323784272623701154519991515308745303995920993973977471307677571994794421357161063680528275560022769919608909070224520953236960384864271520748866061792845174671837544425488931290919641029684319628789069200620552461352914998454443627642679497852539181390895452044803266450111200806322125378886775580483987643080481762900065702457032810691517269506085205686473795027848839917682277550913571570006736204438900067122143753423687161744776165095346268175548883534112198761925226296899292570863029647005841471056603412621402546607609440523148890302299719587199929041345012860792300322816810381702204009342830936361518882676186913616959688015196564440162827795957224948943175418361652474542824023363987570116596008770072060194320460445378131459179444296899819743477760000000000

回调 累加 的深度 在 993 就崩了
结果
992     492528

据测试猜测,python的长度应该是无限长的,回调深度在990多时崩溃,运算速度是硬伤
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/12 15:21:19
42楼: 对于超大整数,自己定义就行,想多大就多大,也不是太复杂:
unit Unit2;

interface

const
  SuperIntNum = 128;  //SuperIntNum*64bit  = 8192Bit

type
  SuperInt = record
    Values: array [0 .. (SuperIntNum - 1)] of UInt64;
  public
    class operator Implicit(const Value: UInt64): SuperInt; //定义隐含UInt64转SUperInt
    class operator Add(const A, B: SuperInt): SuperInt; //定义SuperInt加法
    class operator Equal(const A, B: SuperInt): Boolean;   //定义两个SuperInt数值是否相等
  end;

implementation

class operator SuperInt.Implicit(const Value: UInt64): SuperInt;
var
  I: Integer;
begin
  Result.Values[0] := Value;
  for I := 1 to SuperIntNum - 1 do
    Result.Values[I] := 0;
end;

class operator SuperInt.Add(const A, B: SuperInt): SuperInt;
var
  I: Integer;
  OverFlow1, OverFlow2: Boolean;
begin
  OverFlow1 := False;
  for I := 0 to SuperIntNum - 1 do
  begin
    Result.Values[I] := A.Values[I] + B.Values[I];
    if (A.Values[I] > 0) and (B.Values[I] > 0) and
      (Result.Values[I] < A.Values[I]) and (Result.Values[I] < A.Values[I]) then
      OverFlow2 := True
    else
      OverFlow2 := False;
    if OverFlow1 then
    begin
      Inc(Result.Values[I]);
      if (not OverFlow2) and (Result.Values[I] = 0) then
        OverFlow2 := True;
    end;
    OverFlow1 := OverFlow2;
  end;
end;

class operator SuperInt.Equal(const A, B: SuperInt): Boolean;
var
  I: Integer;
begin
  Result := True;
  for I := 0 to SuperIntNum - 1 do
  begin
    if (A.Values[I] <> B.Values[I]) then
    begin
      Result := False;
      Break;
    end;
  end;
end;

end.
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/12 16:28:22
43楼: //加上超大整数转为字符串的功能
unit Unit2;

interface

uses VCL.Dialogs;

const
  SuperIntNum = 128;  //SuperIntNum*64bit  = 8192Bit

type
  SuperInt = record
    Values: array [0 .. (SuperIntNum - 1)] of UInt64;
  private
    procedure DivideWord(const A: SuperInt; const B: Byte;
          var Q: SuperInt; var R: Byte);
    procedure ToShortStrR(const A: SuperInt; var S: RawByteString);
  public
    class operator Implicit(const Value: UInt64): SuperInt; //定义隐含UInt64转SUperInt
    class operator Add(const A, B: SuperInt): SuperInt; //定义SuperInt加法
    class operator Equal(const A, B: SuperInt): Boolean; //定义两个SuperInt数值是否相等
    function ToString: String;
  end;

implementation

class operator SuperInt.Implicit(const Value: UInt64): SuperInt;
var
  I: Integer;
begin
  Result.Values[0] := Value;
  for I := 1 to SuperIntNum - 1 do
    Result.Values[I] := 0;
end;

class operator SuperInt.Add(const A, B: SuperInt): SuperInt;
var
  I: Integer;
  OverFlow1, OverFlow2: Boolean;
begin
  OverFlow1 := False;
  for I := 0 to SuperIntNum - 1 do
  begin
    Result.Values[I] := A.Values[I] + B.Values[I];
    if (A.Values[I] > 0) and (B.Values[I] > 0) and
      (Result.Values[I] < A.Values[I]) and (Result.Values[I] < A.Values[I]) then
      OverFlow2 := True
    else
      OverFlow2 := False;
    if OverFlow1 then
    begin
      Inc(Result.Values[I]);
      if (not OverFlow2) and (Result.Values[I] = 0) then
        OverFlow2 := True;
    end;
    OverFlow1 := OverFlow2;
  end;
end;

class operator SuperInt.Equal(const A, B: SuperInt): Boolean;
var
  I: Integer;
begin
  Result := True;
  for I := 0 to SuperIntNum - 1 do
  begin
    if (A.Values[I] <> B.Values[I]) then
    begin
      Result := False;
      Break;
    end;
  end;
end;

procedure SuperInt.DivideWord(const A: SuperInt; const B: Byte; var Q: SuperInt;
  var R: Byte);
var
  I: Integer;
  C, D, E: UInt32;
  {$POINTERMATH ON}
  PA, PQ: ^Word;
  {$POINTERMATH OFF}
begin
  if B = 0 then
    ShowMessage('Divided By Zero'); // RaiseDivByZeroError;
  PA := @A.Values;
  PQ := @Q.Values;
  C := PA[SuperIntNum * 4 - 1];
  D := C div B;
  E := C mod B;
  PQ[SuperIntNum * 4 - 1] := Word(D);

  for I := SuperIntNum * 4 - 2 downto 0 do
  begin
    C := (E shl 16) or PA[I];
    D := C div B;
    E := C mod B;
    PQ[I] := Word(D);
  end;

  R := Byte(E);
end;

procedure SuperInt.ToShortStrR(const A: SuperInt; var S: RawByteString);
var
  C: SuperInt;
  D: Byte;
  I: Integer;
begin
  if A = 0 then
  begin
    S := '0';
    Exit;
  end;
  SetLength(S, SuperIntNum * 64);
  C := A;
  I := 0;
  repeat
    DivideWord(C, 10, C, D);
    Inc(I);
    S[I] := AnsiChar(D + 48);
  until C = 0;
  SetLength(S, I);
end;

function  SuperInt.ToString: String;
var
  S: RawByteString;
  I, L: Integer;
begin
  ToShortStrR(Self, S);
  L := Length(S);
  SetLength(Result, L);
  for I := 1 to L do
    Result[I] := WideChar(S[L - I + 1]);
end;

end.
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/12 16:30:57
44楼: //主程序
uses Unit2;

function DoMath(Days: SmallInt): String;
var
  I: Integer;
  Sum, CntDay0, CntDay1, CntDay2, CntTmp2: SuperInt;
begin
  CntDay0 := 0;
  CntDay1 := 0;
  CntDay2 := 1;
  for I := 2 to Days do
  begin
    CntTmp2 := CntDay2;
    CntDay2 := CntDay0 + CntDay1;
    CntDay0 := CntDay0 + CntDay1;
    CntDay1 := CntTmp2;
  end;
  Sum := CntDay0 + CntDay1 + CntDay2;
  Result := Sum.ToString;
end;
----------------------------------------------
-
作者:
男 sxbug (太阳雨) ★☆☆☆☆ -
盒子活跃会员
2019/2/13 1:29:55
45楼: 强。楼上的厉害。
----------------------------------------------
-
作者:
男 hdcopy (hdcopy) ★☆☆☆☆ -
普通会员
2019/2/15 14:22:22
46楼: 1、简洁优雅的算法,效率往往有问题,比如递归。
2、超越极限的时候,常规方法是不行的,比如春运订票,比如标准类型溢出
3、好的算法工程师是很重要的
4、好的系统分析就是把简单问题复杂化,再将复杂问题转换为简单化的实现。
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2019/2/18 10:15:26
47楼: @hdcopy 有没有效率更高的算法?
----------------------------------------------
-
作者:
男 hdcopy (hdcopy) ★☆☆☆☆ -
普通会员
2019/2/18 13:48:55
48楼: 查表算不算
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2021/11/15 11:25:18
49楼: 用julia语言最新的1.63版来计算,算到1百万天,只用了16秒:
function docalc(days::Int64)::BigInt
     println(Dates.Time(Dates.now()))
     cntday0 = BigInt(0)
     cntday1 = BigInt(0)
     cntday2 = BigInt(1)
     for i in 2:days
         cnttmp  = cntday2
         cntday2 = cntday0 + cntday1
         cntday0 = cntday2 
         cntday1 = cnttmp
     end
     println(Dates.Time(Dates.now()))
     return cntday0 + cntday1 + cntday2
 end
此帖子包含附件:
PNG 图像
大小:49.3K
----------------------------------------------
-
作者:
男 wr960204 (武稀松) ★☆☆☆☆ -
盒子活跃会员
2021/11/15 12:00:39
50楼: 这种数学题,简化出公式,一下就算出结果来了。为啥要用数组呢
----------------------------------------------
武稀松http://www.raysoftware.cn
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2021/11/15 13:23:09
51楼: 用数组的原因是因为计算结果超出了64Bit的整数,需要用数组代表一个超大整数
----------------------------------------------
-
作者:
男 pcplayer (pcplayer) ★☆☆☆☆ -
普通会员
2021/11/15 17:35:39
52楼: 这种数学题,如果有公式,直接把公式用代码写出来,肯定一下就算出来了。

如果没有公式,要发挥计算机的长项:反复累加,反复干同样的活,消耗时间来跑。

所以,为啥楼主要真的生出一个对象来,然后来数对象?
----------------------------------------------
-
作者:
男 abc1849 (大力水手) ▲▲△△△ -
普通会员
2021/11/15 18:03:45
53楼: 之前在MSDN上找资料的时候看过类似的问题,如果你对C++有了解可以参考下:
https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170
附件是运行的效果,我记得很快就算出来了。
此帖子包含附件:
PNG 图像
大小:20.3K
----------------------------------------------
-
作者:
男 abc1849 (大力水手) ▲▲△△△ -
普通会员
2021/11/15 18:06:25
54楼: 至于大整数的问题可以用这篇博客的单元,它是用普通的 Object Pascal 和 x86-32/x86-64 汇编器编写的:
http://rvelthuis.de/programs/bigintegers.html
可以在 GitHub 上找到此文件和其他文件的最新版本:
https://github.com/rvelthuis/DelphiBigNumbers
此帖子包含附件:
PNG 图像
大小:44.9K
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2021/11/16 16:16:16
55楼: 出于好奇想看看Python的速度,用Python2.7重新计算了一次docalc(1000000),结果所需时间为135.7秒, 速度只有julia 1.63的1/8.8。

import time

def  docalc(days):
   "打印计算结果"
   cntday0 = 0
   cntday1 = 0
   cntday2 = 1
   start_time = time.time()
   for i in range(2, days+1):
      cnttmp  = cntday2
      cntday2 = cntday0 + cntday1
      cntday0 = cntday2 
      cntday1 = cnttmp
   print cntday0 + cntday1 + cntday2
   finish_time = time.time()
   print("程序执行了%f秒"%(finish_time - start_time))
   return

明天测试一下@abc1849提供Pascal版本的BigInteger的速度。
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2021/11/17 8:59:19
56楼: 使用@abc1849提供Pascal版本的BigInteger,在delphi 10.3.3测试了一下,Win32程序的消耗时间为13.8秒,Win64程序消耗的时间为9.2秒,比Julia的16秒快不少。这个测试程序涉及大量的内存复制,看来Julia在内存复制方面还有不少的优化空间。

uses Velthuis.BigIntegers;

function DoMath(Days: Int32): BigInteger;
var
  I: Integer;
  CntDay0, CntDay1, CntDay2, CntTmp2: BigInteger;
begin
  CntDay0 := 0;
  CntDay1 := 0;
  CntDay2 := 1;
  for I := 2 to Days do
  begin
    CntTmp2 := CntDay2;
    CntDay2 := CntDay0 + CntDay1;
    CntDay0 := CntDay0 + CntDay1;
    CntDay1 := CntTmp2;
  end;
  Result := CntDay0 + CntDay1 + CntDay2;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  N: BigInteger;
  BeginTime: TDateTime;
begin
  BeginTime := Now();
  N := DoMath(1000000);
  Edit1.Text := FormatDateTime('hh:nn:ss.zzz', Now - BeginTime);
  RichEdit1.Lines.Text := N.ToString;
end;

end.
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2022/2/24 15:10:50
57楼: 第几天  1  2  3  4  5  6  7  8  9  10 11
新节点  1  0  1  1  2  3  5  8  13 21 34
旧节点  0  1  1  2  3  5  8  13 21 34 55
总节点  1  1  2  3  5  8  13 21 34 55 89  <==是一个Fibonacci数列
从上面可以看出,总节点数列就是一个是Fibonacci数列,
可以计算矩阵 a = [[1,1],[1,0]]的N次幂来得到
我们要计算的就是:Fibonacci(N) = Pow(a,N)
计算矩阵的幂时采用了快速幂算法, 将计算量减少到log2(原计算量)。

实际测试结果:
   计算第1百万天的数,只用了0.167秒,与上面(56楼)的的算法相比,速度提高到9.2/0.16 = 57.5倍。
此帖子包含附件:
PNG 图像
大小:89.8K
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2022/2/24 15:13:52
58楼: unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    RichEditResult: TRichEdit;
    ButtonCalc: TButton;
    EditNum: TEdit;
    EditTime: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure ButtonCalcClick(Sender: TObject);
  private
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses uMatrix2;

procedure TForm1.ButtonCalcClick(Sender: TObject);
var
  M: TMatrix2;
  Delta: TDateTime;
begin
  EditTime.Text := '';
  RichEditResult.Text  := '';
  Application.ProcessMessages;
  M.Init(1,1,1,0);
  Delta := Now();
  M := M.Pow(StrToInt(EditNum.Text));
  Delta := Now() - Delta;
  EditTime.Text := FormatDateTime('n:s.zzz', Delta);
  RichEditResult.Text := (M.Value[1,2]).ToString;
end;

end.
此帖子包含附件:bluestorm8_2022224151352.rar 大小:149.0K
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2022/2/24 15:14:13
59楼: unit uMatrix2;

interface

uses Vcl.Dialogs, System.SysUtils, Velthuis.BigIntegers;

type
  BigInt =  BigInteger;

  TMatrix2 = record
    Value: array[1..2, 1..2] of BigInt;
  public
    class operator Multiply(const A, B: TMatrix2): TMatrix2;
    class operator Add(const A, B: TMatrix2): TMatrix2;
    class operator Equal(const A, B: TMatrix2): Boolean;
    class operator Implicit(const aValue: UInt64): TMatrix2;
    function Pow(N: UInt64): TMatrix2;
    function ToString: String;
    procedure Init(const A, B, C, D: BigInt);
  end;

implementation

function TMatrix2.Pow(N: UInt64): TMatrix2;
var
  Base: TMatrix2;
begin
  if N = 0 then
    Result := 1
  else if N = 1 then
    Result := Self
  else
  begin
    Result := 1;
    Base   := Self;
    while N > 0 do
    begin
      if (N and 1) > 0 then
        Result := Result * Base;
      Base := Base * Base;
      N := (N shr 1);
    end;
  end;
end;

class operator TMatrix2.Implicit(const aValue: UInt64): TMatrix2;
begin
  Result.Value[1, 1] := aValue;
  Result.Value[1, 2] := 0;
  Result.Value[2, 1] := 0;
  Result.Value[2, 2] := aValue;
end;

class operator TMatrix2.Equal(const A, B: TMatrix2): Boolean;
var
  I, J: Integer;
begin
  Result := True;
  for I := 1 to 2 do
  begin
    for J := 1 to 2 do
    begin
      if A.Value[I, J] <> B.Value[I, J] then
      begin
        Result := False;
        Exit;
      end;
    end;
  end;
end;

class operator TMatrix2.Add(const A, B: TMatrix2): TMatrix2;
var
  I, J: Integer;
begin
  for I := 1 to 2 do
  begin
    for J := 1 to 2 do
    begin
      Result.Value[I, J] := A.Value[I, J] + B.Value[I, J];
    end;
  end;
end;

class operator TMatrix2.Multiply(const A, B: TMatrix2): TMatrix2;
var
  I, J: Integer;
begin
  for I := 1 to 2 do
  begin
    for J := 1 to 2 do
    begin
      Result.Value[I, J] := A.Value[I, 1] * B.Value[1, J] +
          A.Value[I, 2] * B.Value[2, J]
    end;
  end;
end;

function TMatrix2.ToString: String;
begin
  Result := Format('[[%s, %s], [%s, %s]]',
          [Value[1, 1].ToString, Value[1, 2].ToString,
          Value[2, 1].ToString, Value[2, 2].ToString]);
end;

procedure TMatrix2.Init(const A, B, C, D: BigInt);
begin
  Value[1, 1] := A;
  Value[1, 2] := B;
  Value[2, 1] := C;
  Value[2, 2] := D;
end;

end.
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2022/2/24 15:22:00
60楼: 用Julia语言写的话就更简单,而且运行更快
function fibonacci(n::Int64)::BigInt
    a = BigInt[[1,1] [1,0]]
    r = a^n
    return r[1, 2]
end

@time fibonacci(1000000);
计算第1百万位的数据,用了0.023893秒 (1.45 k allocations: 10.012 MiB)
          只用了23毫秒。

@btime (a^1000000000);
计算第10亿位的数据,用了72.010秒 (578254 allocations: 19.05 GiB)
----------------------------------------------
-
作者:
男 pcplayer (pcplayer) ★☆☆☆☆ -
普通会员
2022/2/24 18:09:15
61楼: 57 楼直接把它变成数学公式了,当然块了。

就好比 1 加到 100,一个一个加就慢,变成一个公式,就块。

不过,如果按照楼主的要求,老老实实的用一个一个的对象自己繁殖自己的方式来跑,会如何?
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2022/2/24 19:04:59
62楼: 快的原因,不是因为变成了公式,而是因为矩阵幂运算采用了快速幂运算。
类似于要计算2的8次方,你可以用2*2*2*2*2*2*2*2 = 256来计算,总共有7个乘法,
也可以用另一种算法:2*2 = 4,4*4=16, 16*16 = 256,只做了3个乘法就算出来了。
事实上如果不是用矩阵快速幂算法,使用这个公式来算会比以前的方法更慢。
----------------------------------------------
-
作者:
男 kwer (★★★★★) ★☆☆☆☆ -
普通会员
2022/2/25 9:52:35
63楼: 两年了,还在讨论啊。。。首先,感觉这个用数组不太好(mormot有动态数组)应该更像是树结构了。。。其次,delphi的class和record的界限并不大(本质上看class是特殊的record),这里要用record内存和开销会更加小。
----------------------------------------------
==========-==========-==========-==========-==========
     多隆, 给我备一匹最快的马, 我有事要走先~~~
==========-==========-==========-==========-==========
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行277.3438毫秒 RSS