-
Notifications
You must be signed in to change notification settings - Fork 0
/
kaprekar.cpp
89 lines (70 loc) · 1.63 KB
/
kaprekar.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
84
85
86
87
88
89
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int power(int num,int index);
vector<int> sortnum(vector<int> num);
vector<int> num_to_digit(int num);
int kaprekar(int begin);
int makenumber(vector<int> dig, char ch);
int main(){
int flag=1,index;
int begin=234781;
int diff=kaprekar(begin);
vector<int> cycle;
while ( flag ){
index=0;
diff=kaprekar(diff);
cycle.push_back(diff);
while ( (index<cycle.size()-1)&&flag==1 ){
if(diff==cycle[index])
flag=0;
index++;
}
}
cout<<"The Cycle is: "<<endl;
while ( index<cycle.size() ) {
cout<<cycle[index]<<endl;
index++;
}
return 0;
}
int kaprekar(int begin){
int more=0,less=0,diff=0;
more=makenumber(sortnum(num_to_digit(begin)),'a');
less=makenumber(sortnum(num_to_digit(begin)),'d');
diff=more-less;
cout<<more<<" "<<less<<" "<<diff<<endl;
return diff;
}
int makenumber(vector<int> numarr, char ch)
{
int sum=0;
if(ch=='a')
for ( int i=0;i<numarr.size();i++ )
sum+=numarr[i]*power(10,i);
else
for ( int i=numarr.size()-1;i>=0;i--)
sum+=numarr[i]*power(10,numarr.size()-i-1);
return sum;
}
vector<int> sortnum(vector<int> num){
sort(num.begin(),num.end());
return num;
}
int power(int num,int index){
int prod=1;
while(index>=1){
prod*=num;
index--;
}
return prod;
}
vector<int> num_to_digit(int num){
vector<int> dig;
while(num>0){
dig.push_back(num%10);
num/=10;
}
return dig;
}