Size: 2599
Comment:
|
Size: 408
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 5: | Line 5: |
#acl AdminGroup:read,write,delete,revert,admin TadashiIijima:read,write,delete,revert,admin IijimaStaffGroup: IijimaGroup: IijimaObogGroup: GuestGroup: Known: All: | #acl AdminGroup:read,write,delete,revert,admin TadashiIijima:read,write,delete,revert,admin IijimaStaffGroup: IijimaGroup: IijimaObogGroup: GuestGroup: Known: All:read |
Line 17: | Line 17: |
* 解答は基本的に,下記 2 行の関数です. * [[https://ja.wikipedia.org/wiki/最大公約数|最大公約数]]から求めることができます. * 正整数 {{{x}}}と{{{y}}}に対して,{{{gcd(x,y) × lcm( x, y ) == x × y}}}という関係がある * したがってgcdが分かれば,lcmを求めることができる {{{#!highlight python def lcm( x, y ): return( int( x * y / math.gcd( x, y ) ) ) }}} {{{#!highlight python #!/usr/bin/env python # -*- coding: utf-8 -*- # ============================================================================== # * Copyright (c) 2018 IIJIMA, Tadashi # * (IIJIMA Laboratory, Dept. of Science and Technology, Keio University). # ============================================================================== # ソフトウェア工学[02] 基本課題[02]-(007a) BP_02_007a_least_common_multiple.py # BP(Basic Problem) 02-007a: 【関数定義】 【関数定義】 最小公倍数 lcm ( least common multiple )を求める関数を定義する # 2018-10-03 飯島 正 (iijima@ae.keio.ac.jp) # ============================================================================== # ----- 数学関数を取り扱うためのmathモジュールをインポートする ------ import math # ============================================================================== # ===== 【関数定義】 最小公倍数 lcm ( least common multiple )を求める関数 ===== def lcm( x, y ): return( int( x * y / math.gcd( x, y ) ) ) # ============================================================================== # ===== 【メイン・プログラム】 ===== # ----- オープニングメッセージ ----- print( "最小公倍数数LCMを求めるを求める (二つの整数を入力してください): " ) # ----- パラメータの入力 ----- x = int( input( " 一つ目の整数を入力してください>>> " ) ) y = int( input( " 二つ目の整数を入力してください>>> " ) ) # ----- 結果の表示 ---- print( lcm( x, y ) ) # ============================================================================== }}} |
基本問題(7)
Contents
概要
*
ヒント
この課題で使うPythonの機能 (学習のヒント)
[ edit ]
この課題の解き方 (問題解決のヒント)
[ edit ]
- この課題の解き方 (問題解決のヒント) ....
最小公倍数lcmは,最大公約数から求めることができます.
正整数 xとyに対して,gcd(x,y) × lcm( x, y ) == x × yという関係があります
- したがってgcdが分かれば,lcmを簡単に求めることができます
- 関数lcm()を定義してください.
実行例
*
プログラム例: 本質的な部分 (授業中に順次公開します)
[ edit ]
- 解答は基本的に,下記 2 行の関数です.
最大公約数から求めることができます.
正整数 xとyに対して,gcd(x,y) × lcm( x, y ) == x × yという関係がある
- したがってgcdが分かれば,lcmを求めることができる
高度な話題 (授業中,もしくは授業後に順次公開します)
[ edit ]
⇒ 高度な話題へのリンク: 授業の流れを阻害しないように別ページにします
- (後日の回の授業内容にはなる可能性がありますが,この回の授業内容には含めません).
- に関するものです.
プログラム例: 配布コード (授業中に順次公開します)