-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursiveDigitSum.js
174 lines (125 loc) · 3.79 KB
/
recursiveDigitSum.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
We define super digit of an integer using the following rules:
Given an integer, we need to find the super digit of the integer.
If has only digit, then its super digit is .
Otherwise, the super digit of is equal to the super digit of the sum of the digits of .
For example, the super digit of will be calculated as:
super_digit(9875) 9+8+7+5 = 29
super_digit(29) 2 + 9 = 11
super_digit(11) 1 + 1 = 2
super_digit(2) = 2
Example
The number is created by concatenating the string times so the initial .
superDigit(p) = superDigit(9875987598759875)
9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116
superDigit(p) = superDigit(116)
1+1+6 = 8
superDigit(p) = superDigit(8)
All of the digits of sum to . The digits of sum to . is only one digit, so it is the super digit.
Function Description
Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.
superDigit has the following parameter(s):
string n: a string representation of an integer
int k: the times to concatenate to make
Returns
int: the super digit of repeated times
Input Format
The first line contains two space separated integers, and .
Constraints
Sample Input 0
148 3
Sample Output 0
3
Explanation 0
Here and , so .
super_digit(P) = super_digit(148148148)
= super_digit(1+4+8+1+4+8+1+4+8)
= super_digit(39)
= super_digit(3+9)
= super_digit(12)
= super_digit(1+2)
= super_digit(3)
= 3
Sample Input 1
9875 4
Sample Output 1
8
Sample Input 2
123 3
Sample Output 2
9
Explanation 2
Here and , so .
super_digit(P) = super_digit(123123123)
= super_digit(1+2+3+1+2+3+1+2+3)
= super_digit(18)
= super_digit(1+8)
= super_digit(9)
= 9
*/
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'superDigit' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING n
* 2. INTEGER k
*/
function superDigit(n, k) {
// Write your code here
if(n.length === 1 && k === 1){
return parseInt(n)
}
let p = ''
// Loop in length of k
let i = 0
while(i < k){
// Concate the string representaion of an integer to variable p per iteration
p += n
// Increment the index to 1 per iteration
i++
}
let splitP = p.split("")
// Calculate the sum of digits in splitP
let superDigitResult = 0;
for (let i = 0; i < splitP.length; i++) {
superDigitResult += parseInt(splitP[i]);
}
// If the result is greater than or equals to 10
while(superDigitResult >= 10){
let sum = 0
// Split the result since its length is greater than 1
let sumSplit = superDigitResult.toString().split("")
for(let j = 0; j < sumSplit.length; j++){
sum += parseInt(sumSplit[j])
}
// store the result sum to superDigitResult
superDigitResult = sum
}
return superDigitResult
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const firstMultipleInput = readLine().replace(/\s+$/g, '').split(' ');
const n = firstMultipleInput[0];
const k = parseInt(firstMultipleInput[1], 10);
const result = superDigit(n, k);
ws.write(result + '\n');
ws.end();
}