Skip to content

Commit 8ee79dd

Browse files
authored
A generator of quadratic polynomials with real roots
All quadratic polynomials generated with this tool have real roots, but due to the random nature, most are irrational.
1 parent da4916d commit 8ee79dd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

quadratics.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import random
2+
import math
3+
a = random.randrange(1,5,1)
4+
b = random.randrange(-15,15,3)
5+
c = random.randrange(-25,25,1)
6+
7+
def checkReal(a,b,c):
8+
if b**2-4*a*c >=0 :
9+
x1= (-b + math.sqrt(b**2-4*a*c))/(2*a)
10+
x2= (-b - math.sqrt(b**2-4*a*c))/(2*a)
11+
12+
h.write(f' {a}x^2 + {b}x + {c} = 0')
13+
h.write("\n\n\n")
14+
15+
j.write(f' {a}x^2 + {b}x + {c} = 0 : {x1} & {x2}')
16+
j.write("\n\n\n")
17+
else:
18+
a = random.randrange(1,5,1)
19+
b = random.randrange(-15,15,3)
20+
c = random.randrange(-25,25,1)
21+
checkReal(a,b,c)
22+
with open("quadraticPractice.txt", "w") as h:
23+
with open("quadratic_AnswerKey.txt", "w") as j:
24+
for i in range(1,20):
25+
a = random.randrange(1,5,1)
26+
b = random.randrange(-15,15,3)
27+
c = random.randrange(-25,25,1)
28+
checkReal(a,b,c)

0 commit comments

Comments
 (0)