Create a function named divisors/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’ (null in C#) (use Either String a in Haskell and Result<Vec, String> in Rust).
Example:
1 2 3
divisors(12) # should return [2,3,4,6] divisors(25) # should return [5] divisors(13) # should return "13 is prime"
Answer:
1 2 3 4 5 6 7 8 9 10 11
#1、 def(n) divisors = (2...n).select{|item| n % item == 0} divisors.empty? ? "#{n} is prime" : divisors end #2、 require'prime' def(n) n.prime? ? "#{n} is prime" : (2...n).select{|i|n%i==0} end
近期评论