- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathcollatz.cpp
83 lines (68 loc) · 2.13 KB
/
collatz.cpp
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
77
78
79
80
81
82
83
// This code is a implementation of Collatz Numbers.
// Problem statement is: https://projecteuler.net/problem=14
// Here we need to find any number which forms longest chain(collatz series) under one million.
// One code if brute force which goes for every number and since longest chain will be arnd 525 time complexity will be: 106 * 500 (approx.)
// Other is kind of Dp-ish approach which stores value of a chain and also update all the numbers appearing in the chain thus taking less time
// Solution is bit recursive and recursion ends where we find any value which is -1.
#include<bits/stdc++.h>
usingnamespacestd;
#definelllonglong
constint inf = 1e9 + 7;
ll intcollatz(ll int curr, vector<ll int> &vec, ll int lv)
{
if (curr <= lv && vec[curr] != -1)
{
// when we find any number in chain which has a value more than -1, its time to stop
return vec[curr];
}
// collatz basic rule: n even -> (n / 2), n odd - > 3n +1
ll int next = (3 * curr) + 1;
if (curr % 2 == 0)
{
next = curr / 2;
}
// prev number in range = 1 + next number in range
// (8 -> 4 -> 2 -> 1) here (answer for 8 is 4) and (answer for 4 is 3)
int nextv = 1 + collatz(next, vec, lv);
// if number lies in range(0 - vector.size()) then update it , other ignore it
if (curr <= lv)
{
vec[curr] = nextv;
}
return nextv;
}
voidsolve()
{
int x = 1000000;
vector<ll int> vec(x + 1, -1);
vec[0] = vec[1] = 1;
for (int i = 1; i < x; i++)
{
vec[i + 1] = collatz(i + 1, vec, x - 1);
}
ll int maxm = -1, ans = -1;
for (int i = 0; i < vec.size(); i++)
{
if (vec[i] >= maxm)
{
maxm = vec[i];
ans = i;
}
maxm = max(maxm, vec[i]);
}
cout << ans << endl;
return;
}
intmain()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin.exceptions(cin.failbit);
clock_t start = clock();
solve();
clock_t end = clock();
double elapsed = double(end - start) / CLOCKS_PER_SEC;
printf("Time measured: %.3f seconds.\n", elapsed);
return0;
}