-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy path15_KaprekarsConstant.cpp
More file actions
107 lines (87 loc) · 2.92 KB
/
Copy path15_KaprekarsConstant.cpp
File metadata and controls
107 lines (87 loc) · 2.92 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// For this challenge you will determine when a specific sequence terminates.
/*
have the function KaprekarsConstant(num) take the num parameter being passed which will be a 4-digit number with at least two distinct digits. Your program should perform the following routine on the number: Arrange the digits in descending order and in ascending order (adding zeroes to fit it to a 4-digit number), and subtract the smaller number from the bigger number. Then repeat the previous step. Performing this routine will always cause you to reach a fixed number: 6174. Then performing the routine on 6174 will always give you 6174 (7641 - 1467 = 6174). Your program should return the number of times this routine must be performed until 6174 is reached. For example: if num is 3524 your program should return 3 because of the following steps: (1) 5432 - 2345 = 3087, (2) 8730 - 0378 = 8352, (3) 8532 - 2358 = 6174.
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int vectorToInt(vector <int>);
/*
First we extract the digits from the current number
we store those numbers in a vector to sort them in both ascending and descending order
set a condition to check that both result in a 4 digit number
perform the subtraction and recursive back to the initial step until the base case is reached
*/
int KaprekarsConstant(int num)
{
int count = 0;
while (num != 6174)
{
vector <int> largeNum;
// extracting the digits
while (num > 0)
{
int temp = num % 10;
num /= 10;
largeNum.push_back(temp);
}
// make a copy since we need both a list sorted in descending order and one in ascending order
vector <int> smallerNum = largeNum;
// sorting the values from both list
sort(smallerNum.begin(), smallerNum.end());
sort(largeNum.begin(), largeNum.end());
reverse(largeNum.begin(), largeNum.end());
// taking the sorted elements and converting back to an int
int biggerValue = vectorToInt(largeNum);
int smallerValue = vectorToInt(smallerNum);
// condition in the case the number is not 4 digits for the larger value
if (largeNum.size() < 4)
{
biggerValue *= 10;
}
// subtracting the numbers
num = biggerValue - smallerValue;
// updating the count
count++;
}
return count;
}
// method to convert the vector to an integer
int vectorToInt(vector <int> list)
{
int result= 0;
int position;
// conditions to determine the position place
if (list.size() == 4)
{
position = 1000;
}
else if (list.size() == 3)
{
position = 100;
}
else if (list.size() == 2)
{
position = 10;
}
else
{
position = 1;
}
// loop to add each individual element into the correct place of the number
for (int x = 0; x < list.size(); x++)
{
result += (position*list[x]);
position /= 10;
}
return result;
}
int main()
{
cout << KaprekarsConstant(3524) << endl; // 3
cout << KaprekarsConstant(2111) << endl; // 5
cout << KaprekarsConstant(9831) << endl; // 7
return 0;
}