摘要
解二次方程时,例如 x²+2x-3 ,往往需要对其进行因式分解。高中阶段之后,最常用的方法是十字相乘。对于 x²+bx+c 形式的二次函数,可通过一个公式直接得到因式分解结果。
核心内容
对于
x
2
+
b
x
+
c
=
0
x^2+bx+c=0
x2+bx+c=0
当符合以下条件时:
c
=
k
(
b
−
k
)
,
k
=
1
,
2
,
3....
c=k(b-k),k=1,2,3....
c=k(b−k),k=1,2,3....
可以进行十字相乘。
且实数根均为整数 -k 或 k-b
我们只需要试 k=1,2,3…的情况就可以了。
通常题目中给出的b和c都不会超过10,首先看b是否比c大1,这就代表了k=1的情况。若不是,则通过上述公式计算出k的值,若k是整数,则可直接得到结果。
附b和c在-10到10之内组成的所有可能。
竖b横c | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
-10 | ||||||||||||||||||||
-9 | (10, -1) | |||||||||||||||||||
-8 | (9, -1) | |||||||||||||||||||
-7 | (8, -1) | |||||||||||||||||||
-6 | (7, -1) | |||||||||||||||||||
-5 | (6, -1) | |||||||||||||||||||
-4 | (5, -1) | |||||||||||||||||||
-3 | (5, -2) | (4, -1) | ||||||||||||||||||
-2 | (4, -2) | (3, -1) | ||||||||||||||||||
-1 | (3, -2) | (2, -1) | ||||||||||||||||||
0 | (3, -3) | (2, -2) | (1, -1) | |||||||||||||||||
1 | (2, -3) | (1, -2) | ||||||||||||||||||
2 | (2, -4) | (1, -3) | (-1, 1) | |||||||||||||||||
3 | (2, -5) | (1, -4) | (-1, -2) | |||||||||||||||||
4 | (1, -5) | (-1, -3) | (-2, 2) | |||||||||||||||||
5 | (1, -6) | (-1, -4) | (-2, -3) | |||||||||||||||||
6 | (1, -7) | (-1, -5) | (-2, -4) | (-3, 3) | ||||||||||||||||
7 | (1, -8) | (-1, -6) | ||||||||||||||||||
8 | (1, -9) | (-1, -7) | ||||||||||||||||||
9 | (1, -10) | (-1, -8) |
附代码
# -*- encoding: utf-8 -*-
"""
PyCharm main
2022年08月18日
by littlefean
"""
import itertools
from typing import *
class QuadraticFunc:
def __init__(self, a, b, c):
"""
ax**2 + bx + c
"""
self.a = a
self.b = b
self.c = c
...
def __str__(self):
res = ""
if self.a != 0:
res += f"{coefficient(self.a)}x²"
if self.b != 0:
res += f"+{self.b}x" if self.b > 0 else f"-{-self.b}x"
if self.c != 0:
res += f"+{self.c}" if self.c > 0 else f"-{-self.c}"
return res
def haveSolution(self):
"""判断二次函数是否有实根"""
return self.b ** 2 - 4 * self.a * self.c >= 0
def normalSolution(self):
res1 = (-self.b + (self.b ** 2 - 4 * self.a * self.c) ** 0.5) / (self.a * 2)
res2 = (-self.b - (self.b ** 2 - 4 * self.a * self.c) ** 0.5) / (self.a * 2)
return res1, res2
def simple(self):
"""将这个二次方程因式分解"""
if not self.haveSolution():
return "不可分解"
aList = pair(self.a)
cList = pair(self.c)
for a1, a2 in aList:
for c1, c2 in cList:
if a1 * c2 + a2 * c1 == self.b:
return f"({coefficient(a1)}x {numStr(c1)})({coefficient(a2)}x {numStr(c2)})"
return "不可分解"
def getSolution(self):
"""
将这个二次方程因式分解,返回因式分解后解的结果
如果不能因式分解,返回None
"""
if not self.haveSolution():
return None
aList = pair(self.a)
cList = pair(self.c)
for a1, a2 in aList:
for c1, c2 in cList:
if a1 * c2 + a2 * c1 == self.b:
return -c1, -c2 if c1 != c2 else c1
return None
def coefficient(self) -> str:
if self == 1:
return ""
if self == -1:
return "-"
return str(self)
def numStr(n: int) -> str:
return f"+{n}" if n >= 0 else str(n)
def pair(n: int) -> list:
"""
把一个整数拆成所有 a*b 的整数对,并以列表形式返回
"""
if n >= 0:
return [(i, n // i) for i in range(1, n + 1) if n % i == 0]
n = abs(n)
return [(-i, n // i) for i in range(1, n + 1) if n % i == 0] + [(i, - n // i) for i in range(1, n + 1) if
n % i == 0]
def main():
# 从 -10 开始一直往后
size = 30
table = [[""] * size for _ in range(size)]
for b, c in itertools.product(range(-10, size - 10), range(-10, size - 10)):
f1 = QuadraticFunc(1, b, c)
if not f1.haveSolution():
continue
if f1.getSolution() is None:
table[b + 10][c + 10] = ""
else:
print(f1, "==", f1.getSolution())
table[b + 10][c + 10] = f1.getSolution().__str__()
# QuadraticFunc(-1, b, c)
for line in table:
print("\t".join(line))
return None
if __name__ == "__main__":
main()