Complete the function which get an input number n such that n >= 10 and n < 10000, then:
Sum all the digits of n.
Subtract the sum from n, and it is your new n.
If the new n is in the list below return the associated fruit, otherwise return back to task 1.
Example
n = 325
sum = 3+2+5 = 10
n = 325-10 = 315 (not in the list)
sum = 3+1+5 = 9
n = 315-9 = 306 (not in the list)
sum = 3+0+6 = 9
n =306-9 = 297 (not in the list)
...
. ...until you find the first n in the list below.
Here are all possible values of n:
1-kiwi
2-pear
3-kiwi
4-banana
5-melon
6-banana
7-melon
8-pineapple
9-apple
10-pineapple
11-cucumber
12-pineapple
13-cucumber
14-orange
15-grape
16-orange
17-grape
18-apple
19-grape
20-cherry
21-pear
22-cherry
23-pear
24-kiwi
25-banana
26-kiwi
27-apple
28-melon
29-banana
30-melon
31-pineapple
32-melon
33-pineapple
34-cucumber
35-orange
36-apple
37-orange
38-grape
39-orange
40-grape
41-cherry
42-pear
43-cherry
44-pear
45-apple
46-pear
47-kiwi
48-banana
49-kiwi
50-banana
51-melon
52-pineapple
53-melon
54-apple
55-cucumber
56-pineapple
57-cucumber
58-orange
59-cucumber
60-orange
61-grape
62-cherry
63-apple
64-cherry
65-pear
66-cherry
67-pear
68-kiwi
69-pear
70-kiwi
71-banana
72-apple
73-banana
74-melon
75-pineapple
76-melon
77-pineapple
78-cucumber
79-pineapple
80-cucumber
81-apple
82-grape
83-orange
84-grape
85-cherry
86-grape
87-cherry
88-pear
89-cherry
90-apple
91-kiwi
92-banana
93-kiwi
94-banana
95-melon
96-banana
97-melon
98-pineapple
99-apple
100-pineapple
Tests:
SubtractSum(10)
SubtractSum(1204)
Good luck!
This challenge comes from aryan-firouzian on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (17)
Solution of this task:
The number minus the sum of its digits is divisible by 9:
N = (number - sum(number's digits)) % 9 == 0
because:
number = d0 + d1 * 10^1 + ... + dm * 10^m
let dk is the k-th digit of the number:
dk * 10^k - dk = dk * 9 * 1..1, (k 1s), for k = 1,.., m
(d0 * 10^0 - d0 = 0)
and
N = d1 * 9 * 1 + d2 * 9 * 11 + ... + dm * 9 * 1...1 = 9(d1 + d2 * 11 + ... + dm * 1...1)
<=>
D = sum(N's digits) % 9 == 0
what is the definition of number divisible by 9
=>
N = 9K, D = 9L (K, L are some integers) and their difference is also divisible by 9:
N - D = 9K - 9L = 9(K - L)
If we repeat the subraction of the sum of the digits of the numbers we are getting smaller differences and they are all divisible by 9
=>
the first number in the interval <1, 100> is also divisible by 9
all items in the basket with the index divisible by 9 are apples
=> the solution is an apple
Ada solution
Yanother Ruby solution
Base ten is implicit, I suppose.
Rust solution:
Heres my solution in Javascript:
It seems like it always returns apple, pretty neat question huh (Unless im wrong? 😅)
How I found this out:
Some python-that-can-be-improved:
Output of the above:
Here is my simple solution to find the fruit name with PHP:
Here is Ruby solution,
I think this might be called a naive solution...
...and neither do I understand recursion (maximum recursion depth exceeded while calling a Python object) and why it always comes up apple.