/
problem14_collatzDP.c
76 lines (59 loc) · 1.63 KB
/
problem14_collatzDP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
The following iterative sequence is defined for the set of positive integers:
n -> n/2 (n is even)
n -> 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following
sequence: 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
It can be seen that this sequence (starting at 13 and finishing at 1)
contains 10 terms. Although it has not been proved yet (Collatz
Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
Ans:
http://en.wikipedia.org/wiki/Collatz_conjecture
From here we can observe that highest step for numbers less than 1
billion is 670,617,279, with 986 steps.
We can use bitfields to define data structure for 10 bits unsigned
int. This is done to fit 1 million numbers steps in array. If steps
are stored in int array it gives segfault
Observed this can be solved more fastly by using short :P
*/
#include<stdio.h>
#include<stdlib.h>
#define MAX 1000000
typedef struct _uint10{
unsigned int i:10;
} uint10;
int collatz(uint10 *steps,int n)
{
int count=0;
unsigned long long int val=n;
while(1){
if(val<n && val<MAX){
return (count+steps[val].i);
}
if(val&1 == 1){ // if val is odd
val=((val<<1)+val+1)>>1;
count+=2;
}else{
val=val>>1;
++count;
}
}
}
int main()
{
int i,max,maxid,temp;
uint10 steps[MAX]={0};
steps[1].i=1;
max=maxid=0;
for(i=3;i<MAX;i+=2){
steps[i-1].i=steps[(i-1)>>1].i;
steps[i].i = collatz(steps,i);
if(steps[i].i>max){
max = steps[i].i;
maxid=i;
}
}
printf("%d:%d\n",max,maxid);
return 0;
}