-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgebra problem 1.py
More file actions
96 lines (74 loc) · 2.66 KB
/
algebra problem 1.py
File metadata and controls
96 lines (74 loc) · 2.66 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
###################################################################################
# Code from Stack Overflow to change colour in IDLE SHELL
#import sys
#
#try: color = sys.stdout.shell
#except AttributeError: raise RuntimeError("Use IDLE")
##color.write("Hi, are you called Miharu461? \n","KEYWORD")
##color.write("Yes","STRING")
##color.write(" or ","KEYWORD")
##color.write("No\n","COMMENT")
####################################################################################
import numpy
problem = '''
Marcia has just opened her new computer store. She makes $27 on every computer
she sells her monthly expenses are $10,000. What is the minimum number of computers
she needs to sell in a month to make a profit?
Solution
The minimum profit is breaking even (total income = expenses)
Equation is 27x = 10000
'''
print problem
# Initialize variables
guess1 = 0
# Guess and Check Method until condition met
while True:
if 27*guess1 >= 10000:
break
guess1 += 1
# Print the Result
print "Guess and check or Brute force method or Exhaustive numeration"
print "Answer to the nearest computer is: ", guess1, " and Total guesses were: ", guess1 + 1, " \n"
# Intialize variables
# Aproximation Method
expenses1 = 10000
epsilon1 = 1
guess2 = 0
increment = 1
numGuesses = 0
while expenses1 - (27*guess2) >= epsilon1 and (27*guess2) < 10000:
guess2 += increment
numGuesses += 1
# Print the Result
if expenses1 - (27*guess2) <= epsilon1:
print "Approximation Method"
print "Solution is",round(guess2)," computers and Total guesses:",numGuesses, " \n"
# Initialize variables
# Bisection Search Method
expenses = 10000
epsilon = 1
guessNum = 0
low = 0
high = expenses
guess = (high + low)/2.0
# Sytematic approach for finding guess
while abs(expenses - (27*guess)) >= epsilon and guess < expenses:
# Elimination of search space based on condition
if 27*guess > expenses:
high = guess
else:
low = guess
# Rebinding of high and low to new search space
guess = (high + low)/2.0
guessNum += 1
#Guess to the nearest computer
guess = guess + 1
print "Bisection search Mehod"
print "Guess is: ", round(guess), " and Number of guesses is: ", guessNum, " \n"
# Initialize variables
expenses3 = 10000.0
cost_of_each_computer = 27.0
# Print Result According to Soulution Equation
print "Direct Solution Method (Using python as a calculator)"
if ((expenses3/cost_of_each_computer) - (numpy.round((10000.0/27.0)))) > 0:
print (numpy.round((10000.0/27.0))) + 1," computers"