Anonymous user
Prime decomposition: Difference between revisions
m
→Elm: removed all extra blank rows caused by text copy/paste
m (→{{header|Elm}}: added output section) |
imported>Polarit m (→Elm: removed all extra blank rows caused by text copy/paste) |
||
Line 2,618:
=={{header|Elm}}==
<syntaxhighlight lang="elm" line="1">
module Main exposing (main)
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
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▼
▲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 (++) " "
▲-- compositeNr = 84894624407
</syntaxhighlight>
{{out}}
Prime factors: 3067 4357 6353 from number 84894624407
=={{header|Elixir}}==
|