Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir script does not complete

I tried to implement FizzBuzz in elixir. I think it works, but I have some weird behavior and it may be my environment. I can run the following .exs file using elixir fizzbuzz.exs and on the first run it works as you would expect. Even on the second run it would work as you would expect. However, if I wait 7 or 8 seconds and run it again, it will stop at 13, or maybe 14. It might even make it to 50. There really isn't anything too consistent about what I am experiencing. I've reproduced it on the first run, I've reproduced it after waiting two seconds between runs. I've even gone a few minutes without being able to reproduce it at all. Please help me...

IO.puts "Hello world from Elixir"

defmodule Fizzbuzz do
  def run(max) do
    run(1, max)
  end

  def run(n, max) do
    if rem(n, 3) == 0 do
      IO.puts "FIZZ"
    end

    if rem(n, 5) == 0 do
      IO.puts "BUZZ"
    end

    if rem(n, 3) != 0 && rem(n, 5) != 0 do
      IO.puts n
    end

    if n < max do
      run(n + 1, max)
    end
  end
end

Fizzbuzz.run(100)

evidence

#: Elixir -v
Erlang/OTP 21 [erts-10.2] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]

Elixir 1.9.4 (compiled with Erlang/OTP 20)
like image 738
Oxymoron Avatar asked May 31 '26 06:05

Oxymoron


1 Answers

First, try updating to the latest release of Elixir, 1.13, currently.

Try running your file with mix run <filename>, so in your case:

mix run fizzbuzz.exs
like image 98
DogEatDog Avatar answered Jun 02 '26 20:06

DogEatDog