def divisors(number): dividers = [] for i in range(1,number+1): if(number % i == 0): dividers.append(i) return dividers
module Divisors wheredivisors :: Integer -> [Integer]divisors n = 1:n:(divisorsAux 2)wheredivisorsAux k| (fromIntegral k) > sqrt (fromIntegral n) = []| otherwise = if n `mod` k == 0then if n`div`k ==kthen k:(divisorsAux (k+1))else k:(n`div`k):(divisorsAux (k+1))else divisorsAux (k+1)- def divisors(number):
- dividers = []
- for i in range(1,number+1):
- if(number % i == 0):
- dividers.append(i)
- return dividers
test.assert_equals(divisors (5*4*3), [1,2,3,4,5,6,10,12,15,20,30,60]) test.assert_equals(divisors (4), [1,2,4]) test.assert_equals(divisors (5*3), [1,3,5,15]) test.assert_equals(divisors (101), [1,101])
module ExampleSpec whereimport Test.Hspecimport Divisorsimport Data.List-- `spec` of type `Spec` must existspec :: Specspec = dodescribe "divisorList" $ doit "divisors of 5*4*3 = 60" $ do(sort(divisors (5*4*3))) `shouldBe` [1,2,3,4,5,6,10,12,15,20,30,60]it "divisors of 4" $ do(sort(divisors 4)) `shouldBe` [1,2,4]it "divisors of 5*3 = 15" $ do(sort(divisors (5*3))) `shouldBe` [1,3,5,15]it "divisors of 101" $ do(sort(divisors 101)) `shouldBe` [1,101]- test.assert_equals(divisors (5*4*3), [1,2,3,4,5,6,10,12,15,20,30,60])
- test.assert_equals(divisors (4), [1,2,4])
- test.assert_equals(divisors (5*3), [1,3,5,15])
- test.assert_equals(divisors (101), [1,101])