d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python > Need Easy Code/programm Offer Fg
Add Reply New Topic New Poll
Member
Posts: 11,404
Joined: Jul 24 2013
Gold: 0.00
Jan 15 2015 12:41pm
hi, need easy program/code to calculate quadratic function with complex numbers. ofc offer fg

This post was edited by hehehehehe on Jan 15 2015 12:41pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 15 2015 04:58pm
post your code and what you need help with.
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Jan 15 2015 05:18pm
send fg ty

Code
#!/usr/bin/env python

import math

a = int(input("Enter the coefficients of a: "))
b = int(input("Enter the coefficients of b: "))
c = int(input("Enter the coefficients of c: "))

d = b**2-4*a*c # discriminant

if d < 0:
print ("This equation has no real solution")
elif d == 0:
x = (-b+math.sqrt(b**2-4*a*c))/2*a
print ("This equation has one solutions: "), x
else:
x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
print ("This equation has two solutions: ", x1, " or", x2)


This post was edited by 0n35 on Jan 15 2015 05:19pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 15 2015 05:21pm
Quote (0n35 @ Jan 15 2015 06:18pm)
send fg ty

#!/usr/bin/env python

import math

a = int(input("Enter the coefficients of a: "))
b = int(input("Enter the coefficients of b: "))
c = int(input("Enter the coefficients of c: "))

d = b**2-4*a*c # discriminant

if d < 0:
    print ("This equation has no real solution")
elif d == 0:
    x = (-b+math.sqrt(b**2-4*a*c))/2*a
    print ("This equation has one solutions: "), x
else:
    x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
    x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
    print ("This equation has two solutions: ", x1, " or", x2)


i think you missed the point
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 15 2015 05:46pm
Quote (carteblanche @ Jan 15 2015 06:21pm)
i think you missed the point


He's also duplicating the calculation of the discriminant 4 times.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 15 2015 05:53pm
Quote (Minkomonster @ Jan 15 2015 06:46pm)
He's also duplicating the calculation of the discriminant 4 times.


optimization is phase 3 (after get something working + fix what you broke)
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 15 2015 06:08pm
Quote (carteblanche @ Jan 15 2015 06:53pm)
optimization is phase 3 (after get something working + fix what you broke)


It's not even optimization though, when it compiles it will look as it does now. It's just ugly code.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 15 2015 06:35pm
Quote (Minkomonster @ Jan 15 2015 07:08pm)
It's not even optimization though, when it compiles it will look as it does now. It's just ugly code.


hmm i was just under the impression interpreted languages aren't very well optimized, but im not familiar with the internal working of python. i ran it through "dis" to dissemble the bytecodes and it still pops up

https://docs.python.org/2/library/dis.html

Code
>>> def quadform(a, b, c):
... d = b**2-4*a*c # discriminant
... if d < 0:
... print ("This equation has no real solution")
... elif d == 0:
... x = (-b+math.sqrt(b**2-4*a*c))/2*a
... print ("This equation has one solutions: "), x
... else:
... x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
... x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
... print ("This equation has two solutions: ", x1, " or", x2)
...
>>> dis.dis(quadform)
2 0 LOAD_FAST 1 (b)
3 LOAD_CONST 1 (2)
6 BINARY_POWER
7 LOAD_CONST 2 (4)
10 LOAD_FAST 0 (a)
13 BINARY_MULTIPLY
14 LOAD_FAST 2 (c)
17 BINARY_MULTIPLY
18 BINARY_SUBTRACT
19 STORE_FAST 3 (d)

3 22 LOAD_FAST 3 (d)
25 LOAD_CONST 3 (0)
28 COMPARE_OP 0 (<)
31 POP_JUMP_IF_FALSE 42

4 34 LOAD_CONST 4 ('This equation has no real solution')
37 PRINT_ITEM
38 PRINT_NEWLINE
39 JUMP_FORWARD 173 (to 215)

5 >> 42 LOAD_FAST 3 (d)
45 LOAD_CONST 3 (0)
48 COMPARE_OP 2 (==)
51 POP_JUMP_IF_FALSE 110

6 54 LOAD_FAST 1 (b)
57 UNARY_NEGATIVE
58 LOAD_GLOBAL 0 (math)
61 LOAD_ATTR 1 (sqrt)
64 LOAD_FAST 1 (b)
67 LOAD_CONST 1 (2)
70 BINARY_POWER
71 LOAD_CONST 2 (4)
74 LOAD_FAST 0 (a)
77 BINARY_MULTIPLY
78 LOAD_FAST 2 (c)
81 BINARY_MULTIPLY
82 BINARY_SUBTRACT
83 CALL_FUNCTION 1
86 BINARY_ADD
87 LOAD_CONST 1 (2)
90 BINARY_DIVIDE
91 LOAD_FAST 0 (a)
94 BINARY_MULTIPLY
95 STORE_FAST 4 (x)

7 98 LOAD_CONST 5 ('This equation has one solutions: ')
101 PRINT_ITEM
102 LOAD_FAST 4 (x)
105 PRINT_ITEM
106 PRINT_NEWLINE
107 JUMP_FORWARD 105 (to 215)

9 >> 110 LOAD_FAST 1 (b)
113 UNARY_NEGATIVE
114 LOAD_GLOBAL 0 (math)
117 LOAD_ATTR 1 (sqrt)
120 LOAD_FAST 1 (b)
123 LOAD_CONST 1 (2)
126 BINARY_POWER
127 LOAD_CONST 2 (4)
130 LOAD_FAST 0 (a)
133 LOAD_FAST 2 (c)
136 BINARY_MULTIPLY
137 BINARY_MULTIPLY
138 BINARY_SUBTRACT
139 CALL_FUNCTION 1
142 BINARY_ADD
143 LOAD_CONST 1 (2)
146 LOAD_FAST 0 (a)
149 BINARY_MULTIPLY
150 BINARY_DIVIDE
151 STORE_FAST 5 (x1)

10 154 LOAD_FAST 1 (b)
157 UNARY_NEGATIVE
158 LOAD_GLOBAL 0 (math)
161 LOAD_ATTR 1 (sqrt)
164 LOAD_FAST 1 (b)
167 LOAD_CONST 1 (2)
170 BINARY_POWER
171 LOAD_CONST 2 (4)
174 LOAD_FAST 0 (a)
177 LOAD_FAST 2 (c)
180 BINARY_MULTIPLY
181 BINARY_MULTIPLY
182 BINARY_SUBTRACT
183 CALL_FUNCTION 1
186 BINARY_SUBTRACT
187 LOAD_CONST 1 (2)
190 LOAD_FAST 0 (a)
193 BINARY_MULTIPLY
194 BINARY_DIVIDE
195 STORE_FAST 6 (x2)

11 198 LOAD_CONST 6 ('This equation has two solutions: ')
201 LOAD_FAST 5 (x1)
204 LOAD_CONST 7 (' or')
207 LOAD_FAST 6 (x2)
210 BUILD_TUPLE 4
213 PRINT_ITEM
214 PRINT_NEWLINE
>> 215 LOAD_CONST 0 (None)
218 RETURN_VALUE
>>>


not that i really know what it does anyway, heh. i remember reading the generated MSIL for a little while when i was reading one of the .NET books, but i lost interest within a few days.

This post was edited by carteblanche on Jan 15 2015 06:37pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 15 2015 07:24pm
Quote (carteblanche @ Jan 15 2015 07:35pm)
hmm i was just under the impression interpreted languages aren't very well optimized


Python isn't interpreted though. It is compiled to bytecode. The bytecode can then be interpreted, if that is your wish. But it isn't mandatory.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll