# Prime Factorization using recursion in Elm?

**URL:** <https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520>\
**Category:** Learn\
**Created:** [December 16, 2023, 11:01pm UTC](https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520 "2023-12-16T23:01:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [December 16, 2023, 11:01pm UTC](https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520/1 "2023-12-16T23:01:08Z")

</div>

As I found no prime factorizer written in Elm, I wanted to do it by myself.  
I found first a [code in c++](https://stackoverflow.com/questions/39607725/prime-factorization-using-recursion) . It works except for bigger numbers, e.g. 84894624407  
It fails with segmentation fault (Apple clang version 15.0.0):

```auto
MacBook-Pro cplus % ./numberfactors            
Give a composite number: 84894624407
zsh: segmentation fault ./numberfactors

```

So I translated it manually to JavaScript and then it can handle bigger numbers too.  
Next I changed it manually to [Elm code](https://ellie-app.com/pM6nqqC82wVa1) (look at the LOGS in Ellie.) and it works with the bigger numbers. However, there is another problem: I can get the prime factors as calculated within the recursive function, only through Debug.log in the browser console window.

Is there any way, how to get out the results calculated recursively, to be shown as normal html text instead of console and without using Debug.log or Debug.toString?

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [December 17, 2023, 1:59am UTC](https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520/2 "2023-12-17T01:59:57Z")

</div>

For `showFactors` instead of returning `number` you could change your accumulator to be a record with fields for each of the values you want to return. The when it completes you can take and convert each of the values to a Strong and print them

---

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [December 17, 2023, 8:40am UTC](https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520/3 "2023-12-17T08:40:45Z")

</div>

Thank you for a good idea!  
I wondered, could it be still simpler in the following way:

```auto
module Main exposing (main)

import Html exposing (..)

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 = acc ++ String.fromInt factor ++ " " 
            number2 = number // factor
        in  
            showFactors number2 factor v2 -- recursive call
   

   -- this modulus function is used
   -- in order to output factor !=2 
   else
     let factor2 = factor + 1 
     in  
        showFactors number factor2 acc 

-- Driver program to test above function
compositeNr = 84894624407
accumulator = ""

ts = showFactors compositeNr 2 accumulator

main =
      text ("Test result " ++ ts) 

-- result = 3067 4357 6353

```

Yes, it works!  
There is now nothing left in the log, only this simple text result visible within html, just enough for me:

```auto
Test result 3067 4357 6353

```

Having the numbers as a record instead of a single string may be used to verify the correct result:

```auto
factor1 * factor2 * factor3 == number
3067 * 4357 * 6353 = 84894624407

```

---

<div class="post-metadata">

**Author:** ![polarit](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/polarit/32/4651_2.png) [@polarit](https://discourse.elm-lang.org/u/polarit)\
**Post date:** [December 17, 2023, 5:11pm UTC](https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520/4 "2023-12-17T17:11:25Z")

</div>

The calculated prime factors are saved into a list.  
The calculated product of the factors within the list is equal to the original number.

[New version](https://ellie-app.com/pMYxVPQ4fvca1) with **number input** field ( + browser.sandbox TEA )

Added to [Rosetta Code](https://rosettacode.org/wiki/Prime_decomposition#Elm) now !

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [December 27, 2023, 5:11pm UTC](https://discourse.elm-lang.org/t/prime-factorization-using-recursion-in-elm/9520/5 "2023-12-27T17:11:56Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
