How to resolve the algorithm Partition an integer x into n primes step by step in the Haskell programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm Partition an integer x into n primes step by step in the Haskell programming language
Table of Contents
Problem Statement
Partition a positive integer X into N distinct primes.
Or, to put it in another way: Find N unique primes such that they add up to X.
Show in the output section the sum X and the N primes in ascending order separated by plus (+) signs: The output could/should be shown in a format such as: This task is similar to factoring an integer.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Partition an integer x into n primes step by step in the Haskell programming language
This Haskell code implements a function to find all possible partitions of a given number x
into n
prime numbers. A partition is a way of dividing a number into smaller parts, in this case, prime numbers.
Here's a detailed explanation of the code:
-
Importing Modules:
Data.List
provides functions for list manipulation, such asdelete
andintercalate
.Data.Numbers.Primes
provides access to the list of prime numbers, andprimes
returns an infinite list of prime numbers.Data.Bool
provides thebool
function, which takes three arguments and returns either the first or second argument based on the truthiness of the third argument.
-
partitions
Function:- This is the main function that finds all possible partitions of
x
inton
prime numbers. It takes two integer arguments,x
andn
. - If
n
is less than or equal to 1, it means there are no partitions, and it returns a list withx
as the only element. - Otherwise, it calls the
go
function with the list of prime numbers less than or equal tox
,x
, andn
.
- This is the main function that finds all possible partitions of
-
go
Function:- This function recursively finds all possible partitions. It takes three arguments:
ps
: A list of prime numbers less than or equal tox
.x
: The number to partition.n
: The number of partitions.
- If
n
is equal to 1, it means we have found a partition. It checks ifx
is in the list of prime numbersps
and returns it as a list if it is. - Otherwise, it uses the
found
function to check ifx
can be partitioned inton
prime numbers using the prime numbers inps
.
- This function recursively finds all possible partitions. It takes three arguments:
-
found
Function:- This function checks if there exists a prime number
p
inps
such thatx - p
can be partitioned inton - 1
prime numbers using the remaining prime numbers inps
. - It returns
True
if such a partition exists andFalse
otherwise.
- This function checks if there exists a prime number
-
main
Function:- This is the main entry point of the program.
- It defines a list of test cases as pairs of numbers
(x, n)
. - It uses the
mapM_
function to apply theputStrLn
function to each test case. - The
putStrLn
function prints a string followed by a newline.
-
justifyLeft
Function:- This is a helper function that justifies a given string
s
to the left withn
characters, using the characterc
.
- This is a helper function that justifies a given string
Source code in the haskell programming language
import Data.List (delete, intercalate)
import Data.Numbers.Primes (primes)
import Data.Bool (bool)
-------------------- PRIME PARTITIONS ---------------------
partitions :: Int -> Int -> [Int]
partitions x n
| n <= 1 =
[ x
| x == last ps ]
| otherwise = go ps x n
where
ps = takeWhile (<= x) primes
go ps_ x 1 =
[ x
| x `elem` ps_ ]
go ps_ x n = ((flip bool [] . head) <*> null) (ps_ >>= found)
where
found p =
((flip bool [] . return . (p :)) <*> null)
((go =<< delete p . flip takeWhile ps_ . (>=)) (x - p) (pred n))
-------------------------- TEST ---------------------------
main :: IO ()
main =
mapM_ putStrLn $
(\(x, n) ->
intercalate
" -> "
[ justifyLeft 9 ' ' (show (x, n))
, let xs = partitions x n
in bool
(tail $ concatMap (('+' :) . show) xs)
"(no solution)"
(null xs)
]) <$>
concat
[ [(99809, 1), (18, 2), (19, 3), (20, 4), (2017, 24)]
, (,) 22699 <$> [1 .. 4]
, [(40355, 3)]
]
------------------------- GENERIC -------------------------
justifyLeft :: Int -> Char -> String -> String
justifyLeft n c s = take n (s ++ replicate n c)
You may also check:How to resolve the algorithm Unicode variable names step by step in the Perl programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Phixmonti programming language
You may also check:How to resolve the algorithm Empty program step by step in the Plain English programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the UnixPipes programming language
You may also check:How to resolve the algorithm Read entire file step by step in the BaCon programming language