1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza.ioでelixir その330

Posted at

概要

paiza.ioでelixirやってみた。
AtCoder、やってみた。

参考にしたページ

サンプルコード

abc193_c

defmodule Abc193.C.Main do
  def main() do
    n = IO.read(:line) 
        |> String.trim() 
        |> String.to_integer()
    do_solve(n)
    |> IO.puts()
  end
  defp do_solve(n) when n in [1, 2], do: n
  defp do_solve(n) do
    2..(:math.sqrt(n) |> round())
    |> Enum.reduce(%{}, fn a, acc ->
      Enum.reduce_while(2..34, acc, fn b, acc ->
        z = pow(a, b)
        if z <= n, do: 
            {:cont, Map.update(acc, z, true, & &1)}, 
        else: 
            {:halt, acc}
      end)
    end)
    |> Enum.count()
    |> Kernel.-(n)
    |> Kernel.*(-1)
  end
  def pow(a, b), do: do_pow(a, b, 1)
  defp do_pow(_, 0, acc), do: acc
  defp do_pow(a, b, acc), do: do_pow(a, b - 1, a * acc)
end

Abc193.C.Main.main()



実行結果

6

成果物

以上。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?