0
def factorize(number) do
  test = factors(number)
  Enum.filter(test,fn x -> prime?(x) == true end)
 end
def prime?(n) do
    [1,n] === factors(n)
  end
def factors(1), do: [1]
  def factors(n) when n < 1 do
    "Please input a number greater than 0"
  end
  def factors(2), do: [1,2]
  def factors(n) do
    Enum.filter(1..floor(n/2), fn x -> rem(n,x) == 0 end) ++ [n]
  end

I have these three functions and if I run factorize(13) or any other prime I get a '\r' or other random output and I don't understand why.

IOEnthusiast
  • 105
  • 6
  • 2
    I guess integers are getting interpreted as characters: in decimal 13 the ascii code for Carriage return `'\r'` -- see https://stackoverflow.com/questions/30037914/elixir-lists-interpreted-as-char-lists – GavinBrelstaff Feb 03 '23 at 19:25
  • How could I change that so the output is [13]? – IOEnthusiast Feb 03 '23 at 19:26
  • follow the link – GavinBrelstaff Feb 03 '23 at 19:28
  • 1
    A variant of this question comes up every couple months -- the answer linked by Adam is excellent. Understand that `'\r' === [13]` or `'cat' === [99, 97, 116]`. What you see is just a formatting option. Use `IO.inspect(val, charlists: :as_lists)` and it will help see past the formatting into the inner values of the list. – Everett Feb 04 '23 at 02:44

0 Answers0