LoginSignup
0
1

More than 1 year has passed since last update.

平方完成の問題を作成する

Last updated at Posted at 2021-09-19

今回の概要

平方完成を練習する用の問題とその答えを作成する

作ろうと思ったきっかけ

私は個別授業の講師をしています
数学は積み重ねなのでこの計算が出来ないと多くの単元で躓く、というものが多くあります
平方完成もその1つなのでここは授業でも力を入れています
レベルで分けると問題集にあるものだけでは少ないと感じることも多く自作は時間が掛かってしまうため今回自動作成してくれるコードを書きました

平方完成とは

平方完成とは簡単に言うと2次関数
図1

図2
という形に変形することです

今回は関係ありませんが
図3
このような流れで変形していきます

難易度によって分ける

経験からaとpとqの値によってレベル分けをしました
レベル1:a=1、pとqは1桁の整数
レベル2:a=1以外の整数、pとqは1桁の整数
レベル3:aは分数、pとqは1桁の整数
レベル4:aは1以外の整数または分数、pは分数、qは整数または分数
aの整数や分数は絶対値3までとしています
理由としてはそれ以上の値の問題は共通試験レベルまでで殆ど見ないからです
qを1桁に限定しているのも同じ理由です

【それぞれのレベルの詳細】
レベル1は基礎
レベル2は標準
レベル3は応用(2次関数の利用などで出てくる)
レベル4は模試(模試は記号を含むものが多いが計算力でいうと模試レベル)

問題作成の関数

分数表示をする為Fraction関数を用いています

import numpy as np
from fractions import Fraction

pm = [-1,1]
n1 = [-3,-2,-1,2,3]
n2 = [-3,-2,2,3,Fraction(1,2),Fraction(1,3),-Fraction(1,2),-Fraction(1,3)]
n3 = list(range(1,10))


def completing_the_square(level,count):
  for i in range(count):
      if level == 1:
        a = ' '
        p = np.random.choice(n3[0:5])*np.random.choice(pm)
        q = np.random.choice(n3)*np.random.choice(pm)
        b = -2*1*p
        c = 1*p*2+q
        if b > 0:
           b = "+"+str(b)
        if c > 0:
           c = "+"+str(c)   
        elif c == 0:
            c = " "     
        if p > 0:
           p = "+"+str(p)
        if q > 0:
           q = "+"+str(q)   
        elif q == 0:
            q = " "        
        print("y =" +str(a)+ "x^2 " +str(b)+ "x "+str(c))
        print("  (y=" +str(a)+ "(x" +str(p)+ ")^2 "+str(q)+")")

      elif level == 2:
        a = np.random.choice(n1)
        p = np.random.choice(n3[0:4])*np.random.choice(pm)
        q = np.random.choice(n3)*np.random.choice(pm)
        b = -2*a*p
        c = a*p*2+q
        if a == -1:
           a = "-"        
        if b > 0:
           b = "+"+str(b)
        if c > 0:
           c = "+"+str(c)   
        elif c == 0:
            c = " "     
        if p > 0:
           p = "+"+str(p)
        if q > 0:
           q = "+"+str(q)   
        elif q == 0:
            q = " "               
        print("y = " +str(a)+ "x^2 "  +str(b)+ "x "+str(c))
        print("  (y=" +str(a)+ "(x"  +str(p)+ ")^2 "+str(q)+")")

      elif level== 3:
        a = np.random.choice(n2[5:9])
        p = np.random.choice(n3[0:4])*np.random.choice(pm)
        q = np.random.choice(n3)*np.random.choice(pm)
        b = -2*a*p
        c = a*p*2+q
        if a == -1:
           a = "-"
        if b > 0:
           b = "+"+str(b)
        if c > 0:
           c = "+"+str(c)   
        elif c == 0:
            c = " "     
        if p > 0:
           p = "+"+str(p)
        if q > 0:
           q = "+"+str(q)   
        elif q == 0:
            q = " "         
        print("y = " +str(a)+ "x^2 "  +str(b)+ "x "+str(c))
        print("  (y=" +str(a)+ "(x"  +str(p)+ ")^2 "+str(q)+")")


      elif level == 4:
        a = np.random.choice(n2)
        p = np.random.choice(n2[5:9])
        q = Fraction(np.random.choice(n3),np.random.choice(range(2,9)))
        b = -2*a*p
        c = a*p*2+q 
        if b > 0:
           b = "+"+str(b)
        if c > 0:
           c = "+"+str(c)   
        elif c == 0:
            c = " "     
        if p > 0:
           p = "+"+str(p)
        if q > 0:
           q = "+"+str(q)   
        elif q == 0:
            q = " "          
        print("y = " +str(a)+ "x^2 "  +str(b)+ "x "+str(c))
        print("  (y=" +str(a)+ "(x"  +str(p)+ ")^2 "+str(q)+")")

・+を表示する
・先頭の×1と最後の0は省略する
この2点はif文で処理をしましたがここはこうしたら短くなる、という書き方があれば教えてください

問題作成

それぞれのレベルの問題を3個ずつ作ります
第一引数がレベル、第二引数が作成したい問題数です

print(completing_the_square(1,3))
print(completing_the_square(2,3))
print(completing_the_square(3,3))
print(completing_the_square(4,3))     

出力結果

y = x^2 -2x +6
(y= (x+1)^2 +4)
y = x^2 -10x +3
(y= (x+5)^2 -7)
y = x^2 +2x -5
(y= (x-1)^2 -3)
None
y = -x^2 +6x -5
(y=-(x+3)^2 +1)
y = -3x^2 -6x -3
(y=-3(x-1)^2 -9)
y = 2x^2 +16x -7
(y=2(x-4)^2 +9)
None
y = 1/3x^2 -2/3x +5/3
(y=1/3(x+1)^2 +1)
y = -1/3x^2 +4/3x -28/3
(y=-1/3(x+2)^2 -8)
y = -1/2x^2 -1x +7
(y=-1/2(x-1)^2 +6)
None
y = 1/3x^2 -2/9x +14/9
(y=1/3(x+1/3)^2 +4/3)
y = 3x^2 +3x +1/2
(y=3(x-1/2)^2 +7/2)
y = -1/3x^2 -2/9x +11/9
(y=-1/3(x-1/3)^2 +1)
None

このようになりました
()の中の式が正解となっています

まとめ

今回は自分が使うように作ったので簡易なコードになっています
そのまま印刷して使える形にはなっていないので
他の単元のコードも作ったら
プリントとして使える仕組みを作るのもいいかなと思っています(その場合excelの方が作りやすい気がしますが)
拙い内容ではありますが最後まで見ていただきありがとうございました

0
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
0
1