您的位置:首页 > 大数据 > 人工智能

Hailstone 在32位无符号整数中最大的跳跃次数

2014-04-14 15:45 351 查看
d

#include <iostream>

struct num_step
{
unsigned int n;
unsigned int step;
};

unsigned int Hailstone(unsigned int n)
{
unsigned int step = 1;
while (1 < n)
{
n = n & 0x00000001 ? n * 3 + 1 : n >> 1;
step++;
}
return step;
}
using namespace std;
int main(int __argc, const char** __argv)
{
unsigned int step = 0;

cout << "find max step of n in 0 ~ UINT_MAX" << endl;
num_step ns = { 0, 0 };

for (unsigned int i = 0; i != UINT_MAX; i++)
{
step = Hailstone(i);
if (step> ns.step)
{
ns.step = step;
ns.n = i;
cout <<i << " have current max step: " << step << endl;
}
}
cout << ns.n << " have max step:" << ns.step << endl;
return EXIT_SUCCESS;
}


运算结果:

find max step of n in 0 ~ UINT_MAX
0 have current max step: 1
2 have current max step: 2
3 have current max step: 8
6 have current max step: 9
7 have current max step: 17
9 have current max step: 20
18 have current max step: 21
25 have current max step: 24
27 have current max step: 112
54 have current max step: 113
73 have current max step: 116
97 have current max step: 119
129 have current max step: 122
171 have current max step: 125
231 have current max step: 128
313 have current max step: 131
327 have current max step: 144
649 have current max step: 145
703 have current max step: 171
871 have current max step: 179
1161 have current max step: 182
2223 have current max step: 183
2463 have current max step: 209
2919 have current max step: 217
3711 have current max step: 238
6171 have current max step: 262
10971 have current max step: 268
13255 have current max step: 276
17647 have current max step: 279
23529 have current max step: 282
26623 have current max step: 308
34239 have current max step: 311
35655 have current max step: 324
52527 have current max step: 340
77031 have current max step: 351
106239 have current max step: 354
142587 have current max step: 375
156159 have current max step: 383
216367 have current max step: 386
230631 have current max step: 443
410011 have current max step: 449
511935 have current max step: 470
837799 have current max step: 525
1117065 have current max step: 528
2234130 have current max step: 529
2978841 have current max step: 532
3433215 have current max step: 587
5425327 have current max step: 598
5649499 have current max step: 613
7532665 have current max step: 616
10043553 have current max step: 619
18064027 have current max step: 623
21409215 have current max step: 631
23682175 have current max step: 642
31576233 have current max step: 645
40096999 have current max step: 650
46340775 have current max step: 691
52970523 have current max step: 747
62779879 have current max step: 755
83706505 have current max step: 758
111608673 have current max step: 761
176369263 have current max step: 772
185804655 have current max step: 785
264733833 have current max step: 788
310322815 have current max step: 790
356319303 have current max step: 800
712638606 have current max step: 801
813500443 have current max step: 806
1084667257 have current max step: 809
2169334514 have current max step: 810
3586423035 have current max step: 811
3586423035 have max step:811
请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐