Tuesday, 5 January 2016

Kata Day 40: Divisible Ints


Problem Description: You are given an integer N. Your job is to figure out how many substrings inside of N divide evenly with N.

Confused? I'll break it down for you.

Let's say that you are given the integer '877692'.

8 does not evenly divide with 877692. 877692/8 = 109711 with 4 remainder.

7 does not evenly divide with 877692. 877692/7 = 125384 with 4 remainder.

7 does not evenly divide with 877692. 877692/7 = 125384 with 4 remainder.

6 evenly divides with 877692. 877692/6 = 146282 with 0 remainder.

9 does not evenly divide with 877692. 877692/9 = 97521 with 3 remainder.

2 evenly divides with 877692. 877692/2 = 438846 with 0 remainder.
We aren't going to stop there though. We need to check ALL of the substrings inside of 877692.

87 does not evenly divide with 877692. 877692/87 = 10088 with 36 remainder.

77 does not evenly divide with 877692. 877692/77 = 11398 with 46 remainder.

76 does not evenly divide with 877692. 877692/76 = 11548 with 44 remainder.

69 does not evenly divide with 877692. 877692/69 = 12720 with 12 remainder.

etc.
Rules:

-If an integer is 0, then it does NOT divide evenly into anything.
-Even though N can divide evenly with itself, we do not count it towards the end number. For Example:

N = 23, the answer will be 0.
-If there are multiple instances of a number, they all get counted. For example:

N = 11, the answer will be 2
Input:

A non negative integer.

Output:

The number of times you found an integer that was evenly divisible with N.

Level:6kyu

Link To Kata: Divisible Ints

In this particular Kata we have to find the number of substrings of a given number who completely divide te given number.We start solving the Kata by converting the given numbers to a string as it makes using the substring parts easier for us. We initialize  a variable count equal to 0 to keep in check of the number of times we get the desired results. Then we start to loop from 1 to just before the length of the substring of input number. For every counter of the loop we initialize a variable i equal to 0. We check if the number divided by te substring from i to the sum of i and the loop counter gives a remainder of 0 and if the divisor substring is not equal to 0 or equal to the input number itself. If the above conditions are satisfied then we increment our count variable by 1. We repeat the above procedure for every loop counter.

In the end we output the count variable as the solution to this Kata.  

Please go through the code for a better understanding. 

Here is a link to the code: Kata Day 40

Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment