Skip to content

Commit

Permalink
Solve 542f3d5fd002f86efc00081a
Browse files Browse the repository at this point in the history
  • Loading branch information
tbsklg committed Jun 30, 2024
1 parent 8bb7deb commit 03a21c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Kyu6/PrimeFactors.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Kyu6.PrimeFactors where

primeFactors :: Integer -> [Integer]
primeFactors n =
let f = head [x | x <- [2 .. n], n `mod` x == 0]
in if n == 1 then [] else f : primeFactors (n `div` f)
14 changes: 14 additions & 0 deletions test/Kyu6/PrimeFactorsSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Kyu6.PrimeFactorsSpec (spec) where

import Kyu6.PrimeFactors (primeFactors)
import Test.Hspec

spec :: Spec
spec = do
describe "Prime factors" $ do
it "Example tests" $ do
primeFactors 1 `shouldBe` []
primeFactors 3 `shouldBe` [3]
primeFactors 8 `shouldBe` [2, 2, 2]
primeFactors 9 `shouldBe` [3, 3]
primeFactors 12 `shouldBe` [2, 2, 3]

0 comments on commit 03a21c7

Please sign in to comment.