I tried this with elixir, and on my m3 mac, it ran in 6.5s. Perl took over 40s :P
#!/usr/bin/env elixir
u =
case System.argv() do
[arg] -> String.to_integer(arg)
_ -> raise "Please provide a valid integer as the first argument."
end
r = :rand.uniform(10_000) - 1
a = :array.new(10_000, default: 0)
a =
Enum.reduce(0..9_999, a, fn i, acc ->
inner_sum =
Enum.reduce(0..99_999, 0, fn j, sum ->
sum + rem(j, u)
end)
updated_value = :array.get(i, acc) + inner_sum + r
:array.set(i, updated_value, acc)
end)
IO.puts(:array.get(r, a))
Oh, the programs are supposed to take an integer from the command line? Then I wonder how many of them would fail when given an integer that's just a wee bit too large (like, say (2^129)-1).
I don't think that's a concern. My assumption is that the point of taking user input is to prevent a compile from precomputing the result.