forked from taniadovzhenko/CppPracticum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkob.c
More file actions
71 lines (50 loc) · 834 Bytes
/
kob.c
File metadata and controls
71 lines (50 loc) · 834 Bytes
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
#include <stdio.h>
// task 1.2.1.3 - Koblitz
int fun_kob(int ak){
if(ak & 1){ // x%2
return ((ak<<1) + ak + 1)>>1; // 3*x+1
}
return ak>>1; // x/2
}
int koblitz2(int ak);
int A = 3;
int B = 1;
int C = 2;
int koblitz(int ak){
if(ak % C){ // x%2
return A * ak + B;
}
return ak / C; // x/2
}
int main(){
int a=27;
a= 871;
do{
a =koblitz(a);
printf("%d, ", a);
}while(a>1);
printf("\n\n\n");
int num_max = 0;
int r=0;
for(int i=2;i<1000;++i){
a = i;
int p = 0;
while(a>1){
//printf("%d, ", a);
a = koblitz(a);
p++;
}
//printf("%d, ",p);
if(num_max<p){
num_max=p;
r=i;
}
}
printf("\n N=%d for %d", num_max, r);
}
int koblitz2(int ak){
if(ak % C){ // x%2
return (A * ak + B)%4?(A * ak + B):(A * ak + B+2);
}
return ak / C; // x/2
}