summaryrefslogtreecommitdiff
path: root/cpp/somme.cpp
blob: df8f8eebc35f56fb34f1fa1efb4e03913afc7c4d (plain)
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
/* gator 2017
input: 1 5
output: 1
*/

#include <iostream>
#include <fstream>

bool pari(int n) { return ((n % 2) == 0) ? true : false; }

int sequenza(int n, bool pollatz, int tCollatz = -1)
{
	int m, tot = 1;
	
	if(pollatz)
		m = 5;
	else
		m = 3;

	while(n != 1) {		
		if(pari(n))
			n /= 2;
		else
			n = n*m+1;
		
		tot++;
		
		if(tCollatz != -1 && tot > tCollatz)
			break;
	}
	
	return tot;
}

int main()
{
	std::ifstream in("input.txt");
	std::ofstream out("output.txt");
	
	int N1, N2, tot = 0, collatz;
	in >> N1 >> N2;
	
	for(int i = N1; i <= N2; i++) {
		collatz = sequenza(i, false);
		if(sequenza(i, true, collatz) < collatz)
			tot++;
	}
	
	out << tot;
	
	out.close();
	in.close();
	
	return 0;
}