标题: [数值计算] 求Collatz猜想代码 [打印本页]
作者: email10t 时间: 2015-6-26 11:57 标题: 求Collatz猜想代码
求Collatz猜想代码
作者: pcl_test 时间: 2015-6-26 12:16
- @echo off
- set n=106
- :loop
- set /a t=%n%%%2
- if %t% equ 0 (echo,%n%&set /a n/=2) else (echo,%n%&set /a n=%n%*3+1)
- if %n% neq 1 (goto loop) else (echo,%n%&pause&exit)
复制代码
作者: email10t 时间: 2015-6-26 13:10
回复 2# pcl_test
作者: aa77dd@163.com 时间: 2015-6-26 15:24
本帖最后由 aa77dd@163.com 于 2015-6-28 00:57 编辑
- @echo off
- for /l %%# in (1 1 99999) do call :Collatz %%#
- exit
- :Collatz
- set "X=%1" & set "out="
- :loop
- (
- set "out=%out%%X%,"
- if %X% equ 1 echo,%out%1 & echo, & exit /b
- set /a "t = (X & 1) << 31 >> 31, X = t & (X << 1) + X + 1 | ~t & X >> 1"
- goto :loop
- )
- exit /b
复制代码
作者: email10t 时间: 2015-7-1 13:27
- 1
-
- 2,1
-
- 3,10,5,16,8,4,2,1
-
- 4,2,1
-
- 5,16,8,4,2,1
-
- 6,3,10,5,16,8,4,2,1
-
- 7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 8,4,2,1
-
- 9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 10,5,16,8,4,2,1
-
- 11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 12,6,3,10,5,16,8,4,2,1
-
- 13,40,20,10,5,16,8,4,2,1
-
- 14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 16,8,4,2,1
-
- 17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 20,10,5,16,8,4,2,1
-
- 21,64,32,16,8,4,2,1
-
- 22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 24,12,6,3,10,5,16,8,4,2,1
-
- 25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 26,13,40,20,10,5,16,8,4,2,1
-
- 27,82,41,124,62,31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,4
- 12,206,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,133
- 6,668,334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719
- ,2158,1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,
- 9232,4616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,2
- 3,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 30,15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,1
- 55,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,50
- 2,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,
- 1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1
- 154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,1
- 60,80,40,20,10,5,16,8,4,2,1
-
- 32,16,8,4,2,1
-
- 33,100,50,25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 35,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 36,18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 39,118,59,178,89,268,134,67,202,101,304,152,76,38,19,58,29,88,44,22,11,34,17,52,
- 26,13,40,20,10,5,16,8,4,2,1
-
- 40,20,10,5,16,8,4,2,1
-
- 41,124,62,31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206
- ,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,
- 334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,
- 1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4
- 616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,3
- 5,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 42,21,64,32,16,8,4,2,1
-
- 43,130,65,196,98,49,148,74,37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,
- 8,4,2,1
-
- 44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 45,136,68,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- 46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
-
- 47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,155,466
- ,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,502,251,
- 754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,1619,4
- 858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1154,57
- 7,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,160,80,
- 40,20,10,5,16,8,4,2,1
-
- 48,24,12,6,3,10,5,16,8,4,2,1
-
- 49,148,74,37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
-
- ...
复制代码
作者: 老刘1号 时间: 2019-4-21 12:30
水,汇编- ;Collatz猜想
- ;Code By 老刘
- ;环境:MASM32 SDK
- ;编译指令:ml /coff Collatz猜想.asm /link /subsystem:console
- ;参数:需要验证猜想的数。
- ;没写高精度,所以输入或计算中途的超过2^32 - 1的数会有溢出。
- ;参考:MHL批处理标准教程之3n+1猜想。
-
-
- Include masm32rt.inc
- Main Proto
-
- .Data?
- bBuffer DB 11 Dup (?)
- dwInput DD ?
- dwCrLfZero DD ?
- dwJumpPlusOne DD ?
- ;End Data?
-
- .Code
- Main Proc
- ;获得参数
- Invoke ArgClC,1,Offset bBuffer
- Sub Esp,4
- Invoke atodw_ex,Offset bBuffer
- Add Esp,4
- Mov dwInput,Eax
-
- Push Eax
- Invoke StdOut,cfm$("{")
- Pop Eax
- ;开始标记
- Xor Ecx,Ecx ;计数器
- .While Eax > 1
- PushAD
- Invoke dw2a,Eax,Offset bBuffer
- Invoke StdOut,Offset bBuffer
- Invoke StdOut,cfm$(",")
- PopAD
- Inc Ecx
- Mov Ebx,Eax
- Shr Eax,1
- Mov Edx,Eax ;除以2并取整数部分的结果
- Shl Eax,1
- .If Eax == Ebx
- Mov Eax,Edx
- .Else
- Lea Eax,[2*Ebx+Ebx+1]
- .EndIf
- .EndW
-
- Push Ecx
- Invoke StdOut,cfm$("1}\n")
- Invoke StdOut,cfm$("表长:")
- Pop Ecx
- Inc Ecx
- Invoke dw2a,Ecx,Offset bBuffer
- Invoke StdOut,Offset bBuffer
- Invoke StdOut,cfm$("\n")
-
- Ret
- Main EndP
-
- Start:
- Call Main
- Invoke ExitProcess,NULL
- End Start
- End
复制代码
- {1}
- 表长:1
- {2,1}
- 表长:2
- {3,10,5,16,8,4,2,1}
- 表长:8
- {4,2,1}
- 表长:3
- {5,16,8,4,2,1}
- 表长:6
- {6,3,10,5,16,8,4,2,1}
- 表长:9
- {7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:17
- {8,4,2,1}
- 表长:4
- {9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:20
- {10,5,16,8,4,2,1}
- 表长:7
- {11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:15
- {12,6,3,10,5,16,8,4,2,1}
- 表长:10
- {13,40,20,10,5,16,8,4,2,1}
- 表长:10
- {14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:18
- {15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:18
- {16,8,4,2,1}
- 表长:5
- {17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:13
- {18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:21
- {19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:21
- {20,10,5,16,8,4,2,1}
- 表长:8
- {21,64,32,16,8,4,2,1}
- 表长:8
- {22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:16
- {23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:16
- {24,12,6,3,10,5,16,8,4,2,1}
- 表长:11
- {25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:24
- {26,13,40,20,10,5,16,8,4,2,1}
- 表长:11
- {27,82,41,124,62,31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:112
- {28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:19
- {29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:19
- {30,15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:19
- {31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:107
- {32,16,8,4,2,1}
- 表长:6
- {33,100,50,25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:27
- {34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:14
- {35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:14
- {36,18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:22
- {37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:22
- {38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:22
- {39,118,59,178,89,268,134,67,202,101,304,152,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:35
- {40,20,10,5,16,8,4,2,1}
- 表长:9
- {41,124,62,31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:110
- {42,21,64,32,16,8,4,2,1}
- 表长:9
- {43,130,65,196,98,49,148,74,37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:30
- {44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:17
- {45,136,68,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:17
- {46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:17
- {47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
- 表长:105
- {48,24,12,6,3,10,5,16,8,4,2,1}
- 表长:12
- {49,148,74,37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:25
- {50,25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
- 表长:25
复制代码
作者: codegay 时间: 2019-4-21 16:11
https://zh.wikipedia.org/wiki/%E ... 9%E7%8C%9C%E6%83%B3
http://www.rosettacode.org/mw/in ... nce&redirect=no
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |