LoginSignup
1
0

Command R+ で elixir を学ぶ (その2)ピタゴラス数

Posted at

ピタゴラス数を計算する Python のプログラムを変換してみます。

Python のプログラム

pythagoras.py
#! /usr/bin/python
#
#	pythagoras.py
#
#					Apr/11/2024
#
# ------------------------------------------------------------------
import	sys
import	math
# ------------------------------------------------------------------
def add_proc(it,jt,zz,aax,ratios):
	vv = [it,jt,zz]
	ratio = float(it) / float(jt)
	if len(aax) == 0:
		aax.append(vv)
		ratios.append(ratio)
	elif (not ratio in ratios):
		aax.append(vv)
		ratios.append(ratio)
#
# ------------------------------------------------------------------
def calc_proc(nn_max):
	aax = []
	ratios = []
	xx_min = 1
	for it in range (xx_min,nn_max):
		xx2 = it * it
		for jt in range(it+1,nn_max):
			yy2 = jt * jt
			xxyy2 = xx2 + yy2
			dz = math.sqrt(xxyy2)
			zz = int(dz)
			zz2 = zz * zz
			if (xxyy2 == zz2):
				add_proc (it,jt,zz,aax,ratios)
	return aax
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
nn_max = 50
results = calc_proc (nn_max)

count = len(results)
print("count = %d" % count)

for it in range(count):
	print("%d\t%d\t%d" % (results[it][0],results[it][1],results[it][2]))

sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./pythagoras.py 
*** 開始 ***
count = 8
3	4	5
5	12	13
7	24	25
8	15	17
9	40	41
12	35	37
20	21	29
28	45	53
*** 終了 ***

Command R+ で変換した結果

pythagoras01.exs
#! /usr/bin/elixir
#
#       pythagoras.ex
#
#                                       Apr/11/2024
#
# ------------------------------------------------------------------
IO.puts "*** 開始 ***"

defmodule Pythagoras do
  def add_proc(it, jt, zz, aax, ratios) do
    vv = [it, jt, zz]
    ratio = it / jt

    if Enum.empty?(aax) do
      aax ++ [vv]
      ratios ++ [ratio]
    else
      if not Enum.member?(ratios, ratio) do
        aax ++ [vv]
        ratios ++ [ratio]
      end
    end
  end

  def calc_proc(nn_max) do
    aax = []
    ratios = []
    xx_min = 1

    for it <- xx_min..nn_max do
      xx2 = it * it

      for jt <- it + 1..nn_max do
        yy2 = jt * jt
        xxyy2 = xx2 + yy2
        dz = :math.sqrt(xxyy2)
        zz = round(dz)
        zz2 = zz * zz

        if xxyy2 == zz2 do
          add_proc(it, jt, zz, aax, ratios)
        end
      end
    end

    aax
  end
end

# ------------------------------------------------------------------
nn_max = 50
results = Pythagoras.calc_proc(nn_max)

count = Enum.count(results)
IO.puts("count = #{count}")

for i <- 0..count-1 do
  IO.inspect(Enum.at(results, i))
end

IO.puts "*** 終了 ***"

実行結果

$ ./pythagoras01.exs 
*** 開始 ***
warning: the result of evaluating operator '++'/2 is ignored (suppress the warning by assigning the expression to the _ variable)
  pythagoras01.exs:16

warning: the result of evaluating operator '++'/2 is ignored (suppress the warning by assigning the expression to the _ variable)
  pythagoras01.exs:20

count = 0
nil
nil
*** 終了 ***
1
0
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
0