다음은 초등학교에서 배우는 나눗셈 계산표를 만들어주는 Julia 소스 코드이다.
나눗셈 계산표를 완성하고 나서 약수, 배수 관계를 알려준다.


  1. ##  Filename: makeDivisionTable.jl
  2. ##
  3. ##  Purpose:  Make a division table in a handy written form.
  4. ##
  5. ##  Execute: julia makeDivisionTable.jl 12345 32
  6. ##           julia makeDivisionTable.jl 500210 61
  7. ##
  8. ##      Date:  2013. 3. 5.
  9. ##    Author:  pkim __AT__ scripts ((DOT)) pe ((DOT)) kr
  10. function printUsage()
  11.     # println("Using: julia makeDivisionTable.jl [numerator] [denominator]")
  12.     # println("Make a division table in a handy written form.")
  13.     println("사용법: julia makeDivisionTable.jl [피제수] [제수]")
  14.     println("손으로 작성한 형태의 나눗셈 표를 만들어준다.")
  15. end
  16. function simplify(v, width)
  17.     t = string(v)
  18.     slen = length(t)
  19.     if slen >=2 && t[slen-1:slen] == ".0"
  20.         t = t[0:slen - 2]
  21.     end
  22.     slen = length(t)
  23.     if slen < width
  24.         t = " " ^(width - slen) * t
  25.     end
  26.     return t
  27. end
  28. function getSuffix(v)
  29.     t = v % 10
  30.     suffix = "은"
  31.     if length(search("2459", "$t")) >= 1
  32.         suffix = "는"
  33.     end
  34.     return suffix
  35. end
  36. function makeTable(numer, denom, quotient)
  37.     strNumer = "$numer"
  38.     strDenom = "$denom" 
  39.     strQuotient = "$quotient"
  40.     lenN = length(strNumer)
  41.     lenD = length(strDenom)
  42.     lenQ = length(strQuotient)
  43.     offsetLeft = 3 + lenD + 3
  44.     spaces = "                                                                                 "
  45.     uline  = "_" ^ (lenN + 2)
  46.     sline  = "-" ^ lenN
  47.     bias = lenN - lenQ
  48.     println(spaces[1:offsetLeft] * spaces[1:bias] * "$quotient")
  49.     println(spaces[1:offsetLeft - 2] * uline)
  50.     print("   " * strDenom * " ) " * strNumer)
  51.     strTmpR = strNumer[1: bias + 1]
  52.     tmpR = int64(strTmpR)
  53.     tmpSub = int64(0) :: Int64
  54.     oneDigit = ""
  55.     for i = 1:lenQ
  56.         if strQuotient[i: i] == "0"
  57.             if i + 1 <= lenQ
  58.                 oneDigit = strNumer[bias + i + 1: bias + i + 1]
  59.                 print(oneDigit)
  60.                 strTmpR = strTmpR * oneDigit
  61.                 tmpR = int64(strTmpR)
  62.             end
  63.         else
  64.             println("")
  65.             tmpSub = int64(strQuotient[i: i]) * denom
  66.             println(spaces[1: offsetLeft] * simplify(tmpSub, bias + i))
  67.             println(spaces[1: offsetLeft] * sline)
  68.             tmpR = tmpR - tmpSub
  69.             if tmpR == 0 && i + 1 <= lenQ
  70.                 print(spaces[1: offsetLeft] * spaces[1: bias + i])
  71.             else
  72.                 print(spaces[1: offsetLeft] * simplify(tmpR, bias + i))
  73.             end
  74.             strTmpR = "$tmpR"
  75.             if i + 1 <= lenQ
  76.                 oneDigit = strNumer[bias + i + 1: bias + i + 1]
  77.                 if oneDigit != ""
  78.                     print(oneDigit)
  79.                     strTmpR = strTmpR * oneDigit
  80.                 end
  81.                 tmpR = int64(strTmpR)
  82.             end
  83.         end
  84.     end
  85.     println("")
  86.     return tmpR
  87. end
  88. if length(ARGS) < 2
  89.     printUsage()
  90.     exit(1)
  91. end
  92. a = 0
  93. b = 0
  94. try
  95.     a = int64(ARGS[1])
  96.     b = int64(ARGS[2])
  97. catch ex
  98.     println(ex)
  99.     @printf("피제수: %s, 제수: %s\n", ARGS[1], ARGS[2])
  100.     println("숫자 입력에 오류가 있습니다.")
  101.     exit(1)
  102. end
  103. if a <= 0
  104.     @printf("피제수: %d\n", a)
  105.     println("피제수는 양의 정수라야 합니다.")
  106.     exit(1)
  107. elseif b <= 0
  108.     @printf("제수: %d\n", b)
  109.     println("제수는 양의 정수라야 합니다.")
  110.     exit(1)
  111. end
  112. q = int64(floor(a / b))
  113. r = a % b
  114. @printf("나눗셈 %s ÷ %s 의 결과: ", a, b)
  115. @printf("몫: %d, ", q)
  116. @printf("나머지: %ld\n", r)
  117. println("")
  118. k = makeTable(a, b, q)
  119. if k == r
  120.     @printf("\n나머지: %d\n", k)
  121. end
  122. if k == 0
  123.     @printf("%d = %d x %d", a, b, q)
  124.     @printf("%d%s %d의 배수(mupltiple)이다.\n", a, getSuffix(a), b)
  125.     @printf("%d%s %d의 약수(divisor)이다.\n", b, getSuffix(b), a)
  126. else
  127.     @printf("%d = %d x %d + %d\n", a, b, q, r)
  128.     @printf("%d%s %d의 배수(mupltiple)가 아니다.\n", a, getSuffix(a), b)
  129. end
  130. # -*- encoding: ms949 -*-
  131. #  Filename: makeDivisionTable.py
  132. #
  133. #  Purpose:  Make a division table in a handy written form.
  134. #
  135. #  Execute: python makeDivisionTable.py 12345 32
  136. #           python makeDivisionTable.py 500210 61
  137. #
  138. #     Date:  2008/05/15
  139. #   Author:  PH Kim   [ pkim ((AT)) scripts.pe.kr ]
  140. import sys
  141. def printUsage():
  142.     # print("Using: python makeDivisionTable.py [numerator] [denominator]")
  143.     # print("Make a division table in a handy written form.")
  144.     print("사용법: python makeDivisionTable.py [피제수] [제수]")
  145.     print("손으로 작성한 형태의 나눗셈 표를 만들어준다.")
  146. def simplify(v, width):
  147.     t = "%ld" % v
  148.     if t[-2:] == ".0":
  149.         t = t[0:len(t) - 2]
  150.     slen = len(t)
  151.     if slen < width:
  152.         t = " " * (width - slen) + t
  153.     return t
  154. def getSuffix(v):
  155.     t = v % 10L
  156.     suffix = "은"
  157.     if "2459".find("%d" % t) >= 0:
  158.         suffix = "는"
  159.     return suffix
  160. def makeTable(numer, denom, quotient):
  161.     strNumer = "%ld" % numer
  162.     strDenom = "%ld" % denom
  163.     strQuotient = "%ld" % quotient
  164.     lenN = len(strNumer)
  165.     lenD = len(strDenom)
  166.     lenQ = len(strQuotient)
  167.     offsetLeft = 3 + lenD + 3
  168.     spaces = "                                                                                 "
  169.     uline  = "_" * (lenN + 2)
  170.     sline  = "-" * lenN
  171.     bias = lenN - lenQ
  172.     print(spaces[0:offsetLeft] + spaces[0:bias] + "%ld" % quotient)
  173.     print(spaces[0:offsetLeft - 2] + uline)
  174.     sys.stdout.write("   " + strDenom + " ) " + strNumer)
  175.     strTmpR = strNumer[0: bias + 1]
  176.     tmpR = long(strTmpR)
  177.     tmpSub = 0L
  178.     oneDigit = None
  179.     for i in range(0, lenQ):
  180.         if strQuotient[i: i + 1] == "0":
  181.             if i + 1 < lenQ:
  182.                 oneDigit = strNumer[bias + i + 1: bias + i + 2]
  183.                 sys.stdout.write(oneDigit)
  184.                 strTmpR = strTmpR + oneDigit
  185.                 tmpR = long(strTmpR)
  186.         else:
  187.             print
  188.             tmpSub = long(strQuotient[i: i + 1]) * denom
  189.             print(spaces[0: offsetLeft] + simplify(tmpSub, bias + i + 1))
  190.             print(spaces[0: offsetLeft] + sline)
  191.             tmpR = tmpR - tmpSub
  192.             if tmpR == 0L and i + 1 < lenQ:
  193.                 sys.stdout.write(spaces[0: offsetLeft] + spaces[0: bias + i + 1])
  194.             else:
  195.                 sys.stdout.write(spaces[0: offsetLeft] + simplify(tmpR, bias + i + 1))
  196.             strTmpR = "%ld" % tmpR
  197.             if i + 1 < lenQ:
  198.                 oneDigit = strNumer[bias + i + 1: bias + i + 2]
  199.                 if oneDigit != None:
  200.                     sys.stdout.write(oneDigit)
  201.                     strTmpR = strTmpR + oneDigit
  202.                 tmpR = long(strTmpR)
  203.     print
  204.     return tmpR
  205. if len(sys.argv) < 3:
  206.     printUsage()
  207.     exit(1)
  208. a = None
  209. b = None
  210. try:
  211.     a = long(sys.argv[1])
  212.     b = long(sys.argv[2])
  213. except ValueError:
  214.     print("피제수: %s, 제수: %s" % (sys.argv[1], sys.argv[2]))
  215.     print("숫자 입력에 오류가 있습니다.")
  216.     sys.exit(1)
  217. if a <= 0L:
  218.     print("피제수: %ld" % a)
  219.     print("피제수는 양의 정수라야 합니다.")
  220.     sys.exit(1)
  221. elif b <= 0L:
  222.     print("제수: %ld" % b)
  223.     print("제수는 양의 정수라야 합니다.");
  224.     sys.exit(1)
  225. q = a / b
  226. r = a % b
  227. sys.stdout.write("나눗셈 %ld ÷ %ld 의 결과: " % (a, b))
  228. sys.stdout.write("몫: %ld, " % q)
  229. print("나머지: %ld" % r)
  230. print
  231. k = makeTable(a, b, q)
  232. if k == r:
  233.     print("\n나머지: %ld" % k)
  234. if k == 0L:
  235.     print("%ld = %ld x %ld" % (a, b, q))
  236.     print("%ld%s %ld의 배수(mupltiple)이다." % (a, getSuffix(a), b))
  237.     print("%ld%s %ld의 약수(divisor)이다." % (b, getSuffix(b), a))
  238. else:
  239.     print("%ld = %ld x %ld + %ld" % (a, b, q, r))
  240.     print("%ld%s %ld의 배수(mupltiple)가 아니다." % (a, getSuffix(a), b))
  241. end


실행> julia makeDivisionTable.jl 500210 61

나눗셈 500210 ÷ 61 의 결과: 몫: 8200, 나머지: 10

          8200
      ________
   61 ) 500210
        488
        ------
         122
         122
        ------
            10

나머지: 10
500210 = 61 x 8200 + 10
500210은 61의 배수(mupltiple)가 아니다.




Posted by Scripter
,