-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquadratic_file_problem.py
More file actions
60 lines (49 loc) · 1.69 KB
/
Copy pathquadratic_file_problem.py
File metadata and controls
60 lines (49 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""Quadratic File Exploration
Euler-like challenge to take a file of quadratic
equations, solve them and provide the sum of their solutions
October 26, 2019"""
import random
from math import sqrt
def quad(a,b,c):
"""Solves quadratic equations and returns the sum
of the solutions."""
x1 = (-b + sqrt(b**2 - 4*a*c))/(2*a)
x2 = (-b - sqrt(b**2 - 4*a*c))/(2*a)
return x1,x2
def solve():
"""Reads file of quadratic coefficients,
returns sum of all roots"""
running_sum = 0
with open("quadtratics.txt",'r') as f:
for i in range(100):
g = f.readline().split()
a,b,c = int(g[0]),int(g[1]),int(g[2])
s1,s2 = quad(a,b,c)
running_sum += (s1+s2)
print(s1,s2,running_sum)
print(running_sum)
solve()
def generate(num):
"""Generates a given number of quadratic
expressions ax**2 + bx + c denoted by 3
numbers separated by spaces. Saves running
sum."""
coeffs = list(range(-10,0)) + list(range(1,11))
running_sum = 0
total = 0
with open("quadtratics.txt",'w') as f:
while total < num:
try:
#assign random integers to a,b,c
a = random.choice(coeffs)
b = random.choice(coeffs)
c = random.choice(coeffs)
#make sure solution is real
running_sum += quad(a,b,c)
#if no exception:
f.write(str(a)+' '+str(b)+' '+str(c)+'\n')
total += 1
except ValueError:
continue
print(running_sum) #-33.278968253968245
#generate(100)