Prime decomposition: Difference between revisions
Content added Content deleted
(→{{header|Quackery}}: added tests for primality) |
imported>Polarit (Added a new solution in Elm language) |
||
Line 1: | Line 1: | ||
module Main exposing (main) |
|||
{{task|Prime Numbers}} |
|||
import Html exposing (Html, div, h1, text) |
|||
import Html.Attributes exposing (style) |
|||
-- See live: |
|||
-- <nowiki>https://ellie-app.com/pMYxVPQ4fvca1</nowiki> |
|||
accumulator : List Int |
|||
accumulator = |
|||
[] |
|||
ts = |
|||
showFactors compositeNr 2 accumulator |
|||
--compositeNr = 84894624407 -- ==> 3067 4357 6353 |
|||
-- compositeNr = 65536 -- = 2^16 |
|||
compositeNr = 2*3*5*7*11*13*17 -- = 510510 |
|||
main = |
|||
div |
|||
[ style "margin" "5%" |
|||
, style "font-size" "1.5em" |
|||
, style "color" "blue" |
|||
] |
|||
[ h1 [] [ text "Prime Factorizer" ] |
|||
, text |
|||
("Prime factors: " |
|||
++ listAsString ts |
|||
++ " from number " |
|||
++ String.fromInt (List.product ts) |
|||
) |
|||
] |
|||
showFactors : Int -> Int -> List Int -> List Int |
|||
showFactors number factor acc = |
|||
if number < 2 then |
|||
acc |
|||
-- returns the final result if number < 2 |
|||
else if |
|||
modBy factor number == 0 |
|||
-- modulo used to get prime factors |
|||
then let |
|||
v2 : List Int |
|||
v2 = |
|||
factor :: acc |
|||
number2 : Int |
|||
number2 = |
|||
number // factor |
|||
in |
|||
showFactors number2 factor v2 |
|||
-- recursive call |
|||
-- this modulus function is used |
|||
-- in order to output factor !=2 |
|||
else |
|||
let |
|||
factor2 : Int |
|||
factor2 = |
|||
factor + 1 |
|||
in |
|||
showFactors number factor2 acc |
|||
listAsString : List Int -> String |
|||
listAsString myList = |
|||
List.map String.fromInt myList |
|||
|> List.map (\el -> " " ++ el) |
|||
|> List.foldl (++) " " |
|||
-- Driver program to test above function |
|||
-- compositeNr = 84894624407 |
|||
-- result = 3067 4357 6353{{task|Prime Numbers}} |
|||
[[Category:Arbitrary precision]] |
[[Category:Arbitrary precision]] |
||
<nowiki>==Elm==</nowiki> |
|||
{{omit from|GUISS}} |
|||
<nowiki><syntaxhighlight lang=”elm"></nowiki>{{omit from|GUISS}} |
|||
The prime decomposition of a number is defined as a list of prime numbers |
The prime decomposition of a number is defined as a list of prime numbers |