Problem Description: Create a function named divisors that takes an integer and returns an array with all of the integer's divisors(except for 1 and the number itself). If the number is prime return the string '(integer) is prime'
(use Either String a in Haskell).
Example:
divisors(12); //should return [2,3,4,6]
divisors(25); //should return [5]
divisors(13); //should return "13 is prime"
You can assume that you will only get positive integers as inputs.
Level:6kyu
Link To Kata: Find the divisors!
Its a pretty easy kata. We have to print out the number of divisors of a given number excluding 1 and the number itself. If there are no such divisors then we output that the number is prime. We initialize a divisor array. We start from 2 to half the value of the given number and push all such numbers which divide the given number completely to divisor array.
In the end if length of divisor array is equal to 0 then we output that the number is prime or else we return the divisor array as the solution to this kata.
Please go through the code for a better understanding.
Here is the link to code: Kata Day 51
Please mention your suggestions or your doubts in the comment below.
Happy coding. :)
No comments:
Post a Comment