[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[数值计算] 求Collatz猜想代码

求Collatz猜想代码

  1. @echo off
  2. set n=106
  3. :loop
  4. set /a t=%n%%%2
  5. if %t% equ 0 (echo,%n%&set /a n/=2) else (echo,%n%&set /a n=%n%*3+1)
  6. if %n% neq 1 (goto loop) else (echo,%n%&pause&exit)
复制代码

TOP

回复 2# pcl_test

TOP

本帖最后由 aa77dd@163.com 于 2015-6-28 00:57 编辑
  1. @echo off
  2. for /l %%# in (1 1 99999) do call :Collatz %%#
  3. exit
  4. :Collatz
  5. set "X=%1" & set "out="
  6. :loop
  7. (
  8.     set "out=%out%%X%,"
  9.     if %X% equ 1 echo,%out%1 & echo, & exit /b
  10.     set /a "t = (X & 1) << 31 >> 31, X = t & (X << 1) + X + 1 | ~t & X >> 1"
  11.     goto :loop
  12. )
  13. exit /b
复制代码

TOP

  1. 1
  2. 2,1
  3. 3,10,5,16,8,4,2,1
  4. 4,2,1
  5. 5,16,8,4,2,1
  6. 6,3,10,5,16,8,4,2,1
  7. 7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  8. 8,4,2,1
  9. 9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  10. 10,5,16,8,4,2,1
  11. 11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  12. 12,6,3,10,5,16,8,4,2,1
  13. 13,40,20,10,5,16,8,4,2,1
  14. 14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  15. 15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
  16. 16,8,4,2,1
  17. 17,52,26,13,40,20,10,5,16,8,4,2,1
  18. 18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  19. 19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  20. 20,10,5,16,8,4,2,1
  21. 21,64,32,16,8,4,2,1
  22. 22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  23. 23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
  24. 24,12,6,3,10,5,16,8,4,2,1
  25. 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. 26,13,40,20,10,5,16,8,4,2,1
  27. 27,82,41,124,62,31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,4
  28. 12,206,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,133
  29. 6,668,334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719
  30. ,2158,1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,
  31. 9232,4616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,2
  32. 3,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
  33. 28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  34. 29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  35. 30,15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
  36. 31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,1
  37. 55,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,50
  38. 2,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,
  39. 1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1
  40. 154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,1
  41. 60,80,40,20,10,5,16,8,4,2,1
  42. 32,16,8,4,2,1
  43. 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
  44. 34,17,52,26,13,40,20,10,5,16,8,4,2,1
  45. 35,106,53,160,80,40,20,10,5,16,8,4,2,1
  46. 36,18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  47. 37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  48. 38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  49. 39,118,59,178,89,268,134,67,202,101,304,152,76,38,19,58,29,88,44,22,11,34,17,52,
  50. 26,13,40,20,10,5,16,8,4,2,1
  51. 40,20,10,5,16,8,4,2,1
  52. 41,124,62,31,94,47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206
  53. ,103,310,155,466,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,
  54. 334,167,502,251,754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,
  55. 1079,3238,1619,4858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4
  56. 616,2308,1154,577,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,3
  57. 5,106,53,160,80,40,20,10,5,16,8,4,2,1
  58. 42,21,64,32,16,8,4,2,1
  59. 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,
  60. 8,4,2,1
  61. 44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  62. 45,136,68,34,17,52,26,13,40,20,10,5,16,8,4,2,1
  63. 46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1
  64. 47,142,71,214,107,322,161,484,242,121,364,182,91,274,137,412,206,103,310,155,466
  65. ,233,700,350,175,526,263,790,395,1186,593,1780,890,445,1336,668,334,167,502,251,
  66. 754,377,1132,566,283,850,425,1276,638,319,958,479,1438,719,2158,1079,3238,1619,4
  67. 858,2429,7288,3644,1822,911,2734,1367,4102,2051,6154,3077,9232,4616,2308,1154,57
  68. 7,1732,866,433,1300,650,325,976,488,244,122,61,184,92,46,23,70,35,106,53,160,80,
  69. 40,20,10,5,16,8,4,2,1
  70. 48,24,12,6,3,10,5,16,8,4,2,1
  71. 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
  72. ...
复制代码

TOP

水,汇编
  1. ;Collatz猜想
  2. ;Code By 老刘
  3. ;环境:MASM32 SDK
  4. ;编译指令:ml /coff Collatz猜想.asm /link /subsystem:console
  5. ;参数:需要验证猜想的数。
  6. ;没写高精度,所以输入或计算中途的超过2^32 - 1的数会有溢出。
  7. ;参考:MHL批处理标准教程之3n+1猜想。
  8. Include masm32rt.inc
  9. Main Proto
  10. .Data?
  11. bBuffer DB 11 Dup (?)
  12. dwInput DD ?
  13. dwCrLfZero DD ?
  14. dwJumpPlusOne DD ?
  15. ;End Data?
  16. .Code
  17. Main Proc
  18. ;获得参数
  19. Invoke ArgClC,1,Offset bBuffer
  20. Sub Esp,4
  21. Invoke atodw_ex,Offset bBuffer
  22. Add Esp,4
  23. Mov dwInput,Eax
  24. Push Eax
  25. Invoke StdOut,cfm$("{")
  26. Pop Eax
  27. ;开始标记
  28. Xor Ecx,Ecx ;计数器
  29. .While Eax > 1
  30. PushAD
  31. Invoke dw2a,Eax,Offset bBuffer
  32. Invoke StdOut,Offset bBuffer
  33. Invoke StdOut,cfm$(",")
  34. PopAD
  35. Inc Ecx
  36. Mov Ebx,Eax
  37. Shr Eax,1
  38. Mov Edx,Eax ;除以2并取整数部分的结果
  39. Shl Eax,1
  40. .If Eax == Ebx
  41. Mov Eax,Edx
  42. .Else
  43. Lea Eax,[2*Ebx+Ebx+1]
  44. .EndIf
  45. .EndW
  46. Push Ecx
  47. Invoke StdOut,cfm$("1}\n")
  48. Invoke StdOut,cfm$("表长:")
  49. Pop Ecx
  50. Inc Ecx
  51. Invoke dw2a,Ecx,Offset bBuffer
  52. Invoke StdOut,Offset bBuffer
  53. Invoke StdOut,cfm$("\n")
  54. Ret
  55. Main EndP
  56. Start:
  57. Call Main
  58. Invoke ExitProcess,NULL
  59. End Start
  60. End
复制代码
  1. {1}
  2. 表长:1
  3. {2,1}
  4. 表长:2
  5. {3,10,5,16,8,4,2,1}
  6. 表长:8
  7. {4,2,1}
  8. 表长:3
  9. {5,16,8,4,2,1}
  10. 表长:6
  11. {6,3,10,5,16,8,4,2,1}
  12. 表长:9
  13. {7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  14. 表长:17
  15. {8,4,2,1}
  16. 表长:4
  17. {9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  18. 表长:20
  19. {10,5,16,8,4,2,1}
  20. 表长:7
  21. {11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  22. 表长:15
  23. {12,6,3,10,5,16,8,4,2,1}
  24. 表长:10
  25. {13,40,20,10,5,16,8,4,2,1}
  26. 表长:10
  27. {14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  28. 表长:18
  29. {15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
  30. 表长:18
  31. {16,8,4,2,1}
  32. 表长:5
  33. {17,52,26,13,40,20,10,5,16,8,4,2,1}
  34. 表长:13
  35. {18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  36. 表长:21
  37. {19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  38. 表长:21
  39. {20,10,5,16,8,4,2,1}
  40. 表长:8
  41. {21,64,32,16,8,4,2,1}
  42. 表长:8
  43. {22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  44. 表长:16
  45. {23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
  46. 表长:16
  47. {24,12,6,3,10,5,16,8,4,2,1}
  48. 表长:11
  49. {25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  50. 表长:24
  51. {26,13,40,20,10,5,16,8,4,2,1}
  52. 表长:11
  53. {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}
  54. 表长:112
  55. {28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  56. 表长:19
  57. {29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  58. 表长:19
  59. {30,15,46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
  60. 表长:19
  61. {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}
  62. 表长:107
  63. {32,16,8,4,2,1}
  64. 表长:6
  65. {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}
  66. 表长:27
  67. {34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  68. 表长:14
  69. {35,106,53,160,80,40,20,10,5,16,8,4,2,1}
  70. 表长:14
  71. {36,18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  72. 表长:22
  73. {37,112,56,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  74. 表长:22
  75. {38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  76. 表长:22
  77. {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}
  78. 表长:35
  79. {40,20,10,5,16,8,4,2,1}
  80. 表长:9
  81. {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}
  82. 表长:110
  83. {42,21,64,32,16,8,4,2,1}
  84. 表长:9
  85. {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}
  86. 表长:30
  87. {44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  88. 表长:17
  89. {45,136,68,34,17,52,26,13,40,20,10,5,16,8,4,2,1}
  90. 表长:17
  91. {46,23,70,35,106,53,160,80,40,20,10,5,16,8,4,2,1}
  92. 表长:17
  93. {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}
  94. 表长:105
  95. {48,24,12,6,3,10,5,16,8,4,2,1}
  96. 表长:12
  97. {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}
  98. 表长:25
  99. {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}
  100. 表长:25
复制代码

TOP

去学去写去用才有进步。安装python3代码存为xx.py 双击运行或右键用IDLE打开按F5运行

TOP

返回列表