-
Notifications
You must be signed in to change notification settings - Fork 1
/
007_Digital Root.cpp
138 lines (111 loc) · 2.63 KB
/
007_Digital Root.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Codewars Coding Challenge
Sum of Digits / Digital Root
Digital root is the recursive sum of all the digits in a number.
Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.
Examples
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11
https://www.codewars.com/kata/541c8630095125aba6000c00/train/cpp
*/
// My Solution
// #include <iostream>
// using namespace std;
int digital_root(int n)
{
int sum = 0;
// Menangani kasus bilangan negatif
if (n < 0) {
// Mengembalikan -1 untuk menandakan kesalahan
return -1;
}
// Menghitung jumlah digit bilangan sampai hanya satu digit tersisa
while (n > 0 || sum > 9) {
if (n == 0) {
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
// Mengembalikan hasil digital root
return sum;
}
// int digital_root(int n){
// int sum = 0;
// while(n > 0 || sum > 9){
// if(n == 0){
// n = sum;
// sum = 0;
// }
// sum += n % 10;
// n /= 10;
// }
// return sum;
// }
// int main(){
// // system("cls")
// int n;
// do{
// cout << "Masukkan angka: ";
// cin >> n;
// cout << "Hasil = " << digital_root(n) << endl;
// }while(true);
// return 0;
// }
// Sample test
/*Describe(Fixed_tests)
{
It(Digital_root)
{
Assert::That(digital_root(16) , Equals(7));
Assert::That(digital_root(195) , Equals(6));
Assert::That(digital_root(992) , Equals(2));
Assert::That(digital_root(167346) , Equals(9));
Assert::That(digital_root(0) , Equals(0));
}
};
*/
// Other solution
// PERFECT SOLUTION
// =1=
// int digital_root(int Z) {
// return --Z % 9 + 1;
// }
// =2=
// int digital_root(int n)
// {
// return (n-1) % 9 +1;
// }
// =3=
// int digital_root(int n)
// {
// if(n < 10)
// return n;
// return digital_root(n % 10 + digital_root(n / 10));
// }
// =4=
// int digital_root(int n)
// {
// /*
// a number of can be represented in the form of 9k+i where 0<=i<=8
// a remainder of i means digital root i if it's a natural number
// for 0 its 0
// */
// if(not n)return n;
// n%=9;
// return n?n:9;
// }
// =5=
// int digital_root(int n)
// {
// int sum = 0;
// while(n > 0)
// {
// sum += (n % 10);
// n = (n / 10);
// }
// return sum < 10 ? sum : digital_root(sum);
// }